130 likes | 142 Views
Learn about classes with protected, private, and public data and functions, overloading in C++, and inheritance examples.
E N D
Classes Class Class_name { protected: data and functions, ... private: data and functions, ... public: data and functions, ... };
Example #include <iostream.h> // Converts seconds to minutes and seconds. const int modules = 60; // Define Mod_Int class class Mod_Int { private: int v; public: Mod_Int(int i) { v = i%modulus;} void assign(int i) { v = i%modulus;} void print() {cout << v << “ ”;} } ; main() { int seconds = 400; Mod_Int z(seconds); cout << seconds << “ seconds equals “ << seconds/60 << “ minutes “; z.print; cout << “seconds\n”; } Output 400 seconds equals 6 minutes 40 seconds
A more complex example #include <iostream.h> struct listelem{ char data; listelem *next; }; class List { private: listelem *h; public: List () { h=0}; ~List () {release();} void prepend(char c); void append(char c); void del() { listelem *temph = h; h = h->next; delete temp; } listelem *first() {return(h);} void print(); void release(); }; void List::prepend(char c) { listelem *temp = new listelem; temp->next = h; temp->data = c; h = temp; }
A more complex example (contd.) void List::append(char c) { listelem *ntemp = new listelem; listelem *temp=h; while (temp->next !=0) temp = temp->next; temp->next=ntemp; ntemp->data=c; ntemp->next=0; } void List::print() { listelem *temp=h; while (temp!=0) { cout << temp->data << “->”; temp=temp->next; } cout << “\n###\n; } void List::release() { while (h!=0) del(); } main() { List w; w.prepend(‘B’); w.prepend(‘A’); w.print(); w.append(‘C’); w.print(); cout << w.first()->data << “\n|; cout << w.first->next->data << “\n” ; cout << w.first->next->next->data << “\n”; } Output A->B-> ### A->B->C-> ### A BC
Overloading #include <iostream.h> class A { private: int xx; public: A() {xx=0; cout << “A() called\n”;} A(int n) { xx = n; cout << “A(int “ << n << “) called\n”; } A (double y) { xx = y + 0.5; cout << “A(fl “ << y << “) called\n”;} ~A() { cout << “~A() called A::xx = “ << xx << “\n”;} }; main() { cout << “Enter main\n”; int x=14; float y = 17.3; A z(11), zz(11.5), zzz(0); A d[5] = {0, 1, 2, 3, 4}; cout << “\nOBJECT ALLOCATION LAYOUT\n”; cout << “\nx is at “ << &x << “\ny is at “ << &y; cout << “\nz is at “ << &z; cout <<“\nzz is at “ << &zz << “\nzzz is at “ << &zzz; cout << “\n-----------------------\n”; zzz = A(x); zzz = A(y); cout << “Exit main\n”; }
Overloading(contd.) OBJECT ALLOCATION LAYOUT x is at 0x7fffb848 y is at 0x7ffb84c z is at 0x7ffb850 zz is at 0x7ffb858 zzz is at 0x7ffb860 ----------------------- A(int 14) called ~A() called A::xx = 14 A(fl 17.3) called ~A() called A::xx = 17 exit main ~A() called A::xx = 4 ~A() called A::xx = 3 ~A() called A::xx = 2 ~A() called A::xx = 1 ~A() called A::xx = 0 ~A() called A::xx = 17 ~A() called A::xx = 12 ~A() called A::xx = 11 Output: Enter main A(int 11) called A(fl 11.5) called A(int 0) called A(int 0) called A(int 1) called A(int 2) called A(int 3) called A(int 4) called
This pointer main(){ C_pair a, b, c; a.init(‘A’); b.init(‘B’); c.init(‘D’); a.print(); cout << “ is at “ << a.where_am_i() << endl; b.print(); cout << “ is at “ << b.where_am_i() << endl; c.print(); cout << “ is at ” << c.where_am_i() << endl; c.increment().print(); cout << “ is at “ << c.where_am_i() << endl; c.print(); cout << “ is at “ << c.where_am_i() << endl; } #include<iostream.h> class C_pair { char c1, c2; public: void init(char b) { c2 = b; c1 = 1+b;} C_pair &increment() { c1++; c2++; return(*this);} void *where_am_i() {return this;} void print() { cout << c1 << c2 << “\t”;} }; Output BA is at 0x7fffb868 CB is at 0x7fffb870 ED is at 0x7fffb878 FE is at 0x7fffb878 FE is at 0x7fffb878
Inheritance #include<iostream.h> class Student { protected: int sid; int year; char name[13]; public: Student(char *nm, int id, int y) { strcpy(name, nm); sid = id; year = y;} void print (); }; class Grad_student: public Student { protected: float support; char thesis[30]; public: Grad_student(char *nm, int id, int y, float sup, char *th): Student(nm, id, y) { support = sup; strcpy(thesis, th);} void print(); }; void Student::print() { cout << “\n” << name << “ STUDENT NO. “ << sid << “ YEAR “ << year << endl;}
Inheritance(Contd.) void Grad_student::print() { cout << “\n” << name << “ STDUENT NO. “ << sid << “ YEAR “ << year << “ SUPPORT “ << support << “ THESIS “ << thesis << endl; } main() { Student s(“Joe Smith”, 1234, 1994), *ps=&s; Grad_student gs(“Johnny Smart”, 5678, 1999, 10000., “World Peace”), *pgs; ps->print(); ps = pgs = &gs; ps->print(); pgs->print(); } Output Joe Smith STUDENT NO. 1234 YEAR 1994 Johnny Smart STUDENT NO. 5678 YEAR 1999 Johnny Smart STUDENT NO. 56 78 YEAR 1999 SUPPORT 10000 THESIS World Peace
Multiple inheritance class Service: public Labor, public Parts { private: int num_parts; char name[20]; public: Service(int num, char *nm, int lc, int pc) : Labor(lc), Parts(pc) { num_parts = num; strcpy(name, nm); } int cost () { return (num_parts * (Parts::cost()+Labor::cost()));} void print() { cout << endl; cout << name << “ cost “ << Labor::c << “ dollars per install “ << endl; cout << name << “ cost “ << Parts::c << “ dollars per part “ << endl; cout << num_parts << “ “ << name << “ installed cost “ << cost() << “ dollars “ << endl; }; #include <iostream.h> class Labor { protected: int c; public: Labor(int cost) { c = cost; } int cost() { return c; } }; class Parts { protected: int c; public: Parts(int cost) { c = cost; } int cost() { return c;} }; int main() { Service computer(10, “widgets”, 50, 90); Service display(20, “tubes”, 500, 10); computer.print(); display.print(); } Output widgets cost 50 dollars per install widgets cost 90 dollars per part 10 widgets installed cost 1400 dollars tubes cost 500 dollars per install tubes cost 10 dollars per part 20 tubes installed cost 10200 dollars
Virtual functions #include <iostream.h> class A{ public: int i; virtual void print_i() { cout << i << “ inside A\n”;} }; class B: public A { public: void print_i() { cout << i << “ inside B\n”;} }; main() { A a; A *pa=&a; B f; f.i=1+(a.i=1); pa->print_i(); pa=&f; pa->print_i(); } Output 1 inside A 2 inside B
Templates template <class TYPE> class stack { TYPE *v; TYPE *p; int sz; public: stack(int s) {v=p=new TYPE[sz=s];} ~stack() { delete[] v;} void push (TYPE a) { *p++ = a; } TYPE pop() { return *--p; } int size const { return p-v; } }; main() { stack<char> sc(100); stack<int> si(100); stack<char*> stk_str(50); } template<class TYPE> void copy(TYPE a[], TYPE b[], int n) { for (int i=0; i<n; i++) a[i] = b[i]; } main() { double fl1[50], f2[50]; char c1[10], c2[20]; int i1[4], i2[4]; char *ptr1, *ptr2; copy(f1, f2, 50); copy(c1, c2, 10); copy(i1, i2, 3); copy(ptr1, ptr2, 30); }