0% found this document useful (0 votes)
22 views114 pages

Oops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
22 views114 pages

Oops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 114
© int main() Int a=10; { 2 Int b=20; cout<>age>>name>>sal; + void Emp: :show() { cout< |_istrearm carom ignore() Testream |strear_withassign al ‘The object "cin" be| class jongs to this ‘The object "cout" belongs to this class ® CONSTRUCTOR 1. They are special member functions of a class having the same name as that of the class 2. They are called implicitly by the compiler as soon as the object of a class gets created. 3. They are called in the order in which objects are created. CONSTRUCTOR V/s DESTRUCTOR DESTRUCTOR 1. It is also a special member function of a class having the same name as that of the class but prefixed with a symbol of tilde. 2. It is also implicitly by the compiler but just before the object of a class is to be destroyed 3. It is called in the reverse order of creation of the 6 CONSTRUCTOR 4. They can be parametrized 5, Since they can be parametrized so we can overload them and thus a class can have multiple constructor 6. If we do not define any constructor in our class then the C++ compiler automatically provides two contructors to our class called as THE DEFAULT CONSTRUCTOR and THE DEFAULT COPY CONSTRUCTOR, DESTRUCTOR 4. A destrcutor never accepts any argument 5, Since they do not accept any argument , so we cannot ‘overload them and thus a class. can have only one destructor 6. If we do not define any destructor in our class then the C++ compiler automatically provides only one destructor to our class class called as THE DEFAULT CONSTRUCTOR INLINE MEMBER FUNCTION Explicit ° Implicit These are those member functions which are declared inside the class body but are defined outside the class body prefixed with the keyword inline ‘These are those member functions which are defined INSIDE the class body and even if we do not use the keyoword inline , still the compiler will treat these member functions as inline. Moreover all the member functions generated by the compiler automatically for our class are always implicit inline Using "inline" Functions int square(int n) return na; J Overheads , the compiler has to face when it solves a function call Xt ae 1, Prototype Checking int a=10,b=20; int ¢; 2. Argument Passing « V3. Pushing The Address In Stack cou c= Sons cout<<"Sq of "< class Student Sy int roll a char grade; - we float per Ne public ‘ void get() cout<<"Enter roll grade and per cin>>roll>>grade>>per; } Void show(); Yi ae inlin€ Void Emp: :show() cout< at: maint) using namespace std; Student S,P; class Student: Aa : [Sse int roll; ¥ Fats | Sree) char grade; & a set \ float per} - "S.show();, au Pyke TEROWOANY, 0 ced ble: : void get(); : Arron void show(); N we : O % 7 qu retumo; ee 4 } void Student::get() & be 1 t we cout<<"enter roll,grade and per: cin>>roll>>grade>>per; + void Student: :show() { cout< using namespace std; class Student int main() int roll; © cudent SP; char grade; S.get(); float per; S.show(); public: P.get(); P.show( void get(); return 0; void show(); + y baad Student::get() cout<<"Enter roll,grade and per:"; cin>>roll>>grade>>per; Vold Student: :show() cout<<"roll="<roll<<",grade="< grade<<",per="<per<b=b; return 0; this->h=h; + + Box::Box(Box &P) void Box::show() t cout<| i KO - [Eisen return + © suppose we do npt want to allow show() to achange the values of "a" and declare show() as CONSTANT MEMBER FUNCTION: {80 in tis case we will have to ‘member_fun>() const We use const at the end , when we wamt ot declare a member functjon as "const" ® int main() int rad=10; float ar,circ; const float cout<<"Area="< ——— ot 8 Ranson oenecs value of the variable fo which it ot CO oaattt ‘epi pointing nt 2.A ponte canbe relnaized or OP ou to anew address, wie int main() a int a=10,b=50; p=ab; ~~ const int" foutecac<";"< Qn Do we have any other way of declaring pointer to const ? ‘Ans. We can declare pointer to const in two ways: 1. const * ; 2. const * ; For example: wk cael ieee Int const *p; Int main() { int a=10,0=50; p=8b; const int * coutc +p-30, Y coute cout< comparevoi(S) { Int x=1*b*h; y nt main() { Box B1; Box B2; Bi.get(); B2.get(); me) Whenever we require more than 1 object in a member function , then that member function will be called by obe of the objects , while other objects will be passed as argument. In simple words we can say that if a member function works on 'n’ objects then it requir. " objects as argument. ® What are different ways using which we can pass an object as argument ? Passing Object By Value Passing Object By Address Passing Object By Reference Rue Vows SSpassINe OBJECT BY VALUE: Int main() = = Box B1,82; #include Using namespace std; x B1.get(); B2.get(); class Box % { % B1.show(); private int I,b,h; else if(x>y) 82 show(); . retumi; | & |S public ase yo get() retum-1;( © K ns=B1 comparevol(B2); cout<<"Enter |,b,h: Ms if(ans==0) cin>>I>>b>>h; cout<<"Equal Vols"; } else if(ans==1) Void show() cout<<"Vol of 81 is gr"; £ else cout< z { using namespace std; Xx (ge4] * box 81,82; class Box * >b*P->h; Bi.get(); ~ Pe B2.get(); private int I,b,h; ue we return 0; B1.show(); public : RK else if(x>y) B2.show(); void get() \N return 1} pi else int ans; cout<<"Enter |,b,h:"; return -1; ans=B1.comparevol(&B2); cin>>1>>b>>h; 3 } if(an: void show() cout<<"Equal Vols"; { else if(ans==1) cout< t { using namespace std; Box 61,82; class Box B1.get(); B2.get(); private int i,b,h; B1.show(); public : else if(x>y) B2.show(); void get() return 1; { else int ans; cout<<"enter |,b,h:"; return -1; ans=B1.comparevol(2); cin>>I>>b>>h; } if(ans==0) ‘void show() cout<<"Equal Vols"; else If(ans==1) coute using namespace std; < private int I,b,h; public: void get() < cout<<"Enter I,b,h:"; cin>>I>>b>>h; void show() ‘cout< int comparevol(con: will make our code more reliable and will prevent any accidental change Friend Functions Friend functions are those functions which are not declared as member functions of the class , but still they can access the private data members of the class Properties Of Friend Functions: 1, To make a function friend of a class , we must declare it within the body of the class , prefixed with the keyword friend. class A ft public: void f1(); friend void £2(); y 2. Whenever we define a friend function , neither the name of the class , nor the scope resolution operator appears In its definition.Moreover the keyword friend also does not appear void Az:f1() { If body void f2() /I body 3. Whenever we call a friend function , neither the name of the object nor the dot operaotr appears towards its left. It may however accept the object as argument whose data it wants to access int main() ? A obj; obj.f1(); // OK 0bj.f2();// ERROR £20); // OK return 0; + ‘A Program To Demonstrate How To Use A Friend Function ? a= Tx a +#include vee show(Student # : ba Ws int main() t std; 3 Ss, ef eS cout< show(s); int roll Sas etch), char grade; return 0; float per; ~_ > public: void get(); 5 friend void show(Student); y cout<<"Enter roll,grade and per:" cin>>roll>>grade>> per; 3. Whenever we call a friend function , neither the name of the object nor the dot operaotr appears towards its left. It may however accept the object as argument whose data it wants to access int main() { A obj; obj.F1(); // OK 0bj.f2();// ERROR £2(obj); // OK return 0; pe + 4. It doesn’t matter in which section of the class , we have declared a friend function as we can always access it from anywhere in our code Assignment: Redesign the previous code by making get() as friend function and show() as member function #include using namespace std; main() “Etudent ; class Student s uw get(S); ; - Sishow(); ECON No] return 0; char grade; ricat ee af ne public: ea a friend void get(Student); i ee void show() . a void Student::show() cout< int main() using namespace std; class Student vS cw get(S); a 5show(); int roll; ~ | return 0; char grade; ny float per; public: ’ friend void get(Student &); void show() _ H _ Tea, ae kp G gost - void get(Student & p) ee -- oe { cout<<"Enter roll,grade and per:"; ~1 I> > P.grade>>P.per; ae void Student::show() cout< operator (); | | DEPENDS IT WILL DEPENDS, ON ALWAYS. ON THE REMAIN THE cope SAME CODE What about this main() ? How the previous code will behave if we write main() as follows ? int main() t Counter €1(10); Counter C2; C1.show(); C2.show(); How the code will behave on ee this call & why ? c2=++C1; CL.show(); C2.show(); return 0; +#include using namespace std; ® (@id)Counter::operator++() int main() class Counter t { + teount; Counter €1(10); int count; } This line Counter €2; public: wil generate Counter(int c) error C1.show(); < count=c; c2.show(); i a C2xCtoperator++(); ¢__‘eesqaciy count= c1.show(); ACTUAL OUTPUT: Void show() Ct.show(); cout< Overloading Post Increment Operator As Member Function using namespace std; class Counter @) Counter Counter: :operator++(int) int main() t Counter Temp; { int count; v Temp.count=count; Counter C1(10); count+ +; Counter C2; public: return Temp; Qu Counter(int ¢) ci.show(); —+ 10 a. wt C2:show();—s count=¢; } CeCe Counter() { C1.show(); count=0; c2'show(); void show() t return 0; cout<; Will allocate space for 1 For ex: aan integer i.e. 4 Bytes , in HEAP new int area, and return its base address as oR int * new char Will allocate space for 1 integer i.e. 1 Byte , in HEAP area, and return its base address as char * @ 2. Allocating memory for an array : syntax: new (size] Par ye fy we oe & a ~ new int [10] ~ Will allocate space for 10 integer array (i.e 40 Bytes) continuously and will return its base address © 2. Deallocating memory for an array Syntax: delete [ ] ; x: delete [] p S Wa? to create a class called MyArray having the following data members: 1. an int*p: For pointing to the dynamic array 2. a variable n: For storing size of the array 3. a variable sum: For storing sum of all the elements of the dynamic array Also provide following member functions: 1. A constructor to create dynamic array of integers of size n , where n should be taken from the user. 2. A member function called get() , for accepting values from the user and stroing them in dynamic array. 3. A member function called calculate() for calculating sum of array elements 4. A member function show() , for display the dynamic array as well as it sum 5. An appropriately coded destructor CORRECT VERSION © class Carton: public Box i fae class Box < char mat[20]; Carton ob); protected: int |; public: sa int b; void set() ue int hi 7 obj.show(); public cout<<"Enter material." . void get() cin>>mat; obj. display(); } : cout<<"Enter I,b,h:"; servers: cin>>1>>b>>h; void display() } 7 { void show() cout< { Tae Re ean? ¢ Une coe a { he © class Box c int Int by int hy public: void get() “% cout<<"Enter |,b,| cin>>I>>b>>h; + VW Void show() { EXAMPLE OF SINGLE INHERITANCE IN PUBLIC MODE A A rial foot 75 lh ay |b +4 |i Re cout<>mat; } void display() { cout<>|>>b>>h; vold show() cout<>mat; } void display() { cout<>I>>b>>h; 3 Vorr SHOWTT Pigs cout<Derv obj caller Function ae EARLY BINDING Box::show() Box “Derv obj EARLY BINDING Lady Bibs \ fet = te Pt class Box { Int i; int b; int hj public: void get() q cout<<"Enter I,,h:"; cin>>I>>b>>h; =D cout< + a-- > Fupetionn >} . queen class Box wm { Int; int b; int h; public: void get() { cout<. cin>>|>>b>>h; > void show() { + cout< Da vl a cout cma ) he ‘< { class : protected q Rules: 1. All the public members of the base class will become PROTECTED members of the derived class . That Is they can be accessed from the member functions of the derived class but not directly from the objecte of the dervied class from outside the derived class. 2. All the protected members of the base class also become protected members of the dervied class That is , they also can only be accessed from the member functions of the derived class but not directly from the object of the derived class from outsdie the dervoed class. 3.All the private members of the "base" class remain private to thelr own class and thus they can neither be accessed by the MEMBER FUNCTIONS nor directly by the object of the derived class. ® class Box protected: int |,b,h; public: (< get() { cout<<"enter |,b,h:"; cin>>1>>b>>h; cout< public: { Tod csplay() { protected Box int main() Carton obj; cout<<"Enter mat: cin>>mat; 7 Cbivoumety cout<>|>>b>>h; void show() cout<>mat bj. display(); a 0bj.volume(); void dispfay() t : show(); , oa cout<>I>>b>>h; void show() cout<>mat; + void display() { show(); cout<>a>>b; Void show() coute>a>>b; void show() { cout< void add() Void aliff() +b; + Void display() & show(); cout<<"Sum="< Using namespace st; F @ protect i dina pute void get() i conte dnensons ofthe Shave > Jae stow ‘ eutcimi ce"

You might also like