Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
22 views
114 pages
Oops
Uploaded by
scorpiohalloween29
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
Download
Save
Save Oops For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
22 views
114 pages
Oops
Uploaded by
scorpiohalloween29
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
Carousel Previous
Carousel Next
Download
Save
Save Oops For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 114
Search
Fullscreen
© 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 the6 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 CONSTRUCTORINLINE 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 inlineUsing "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, wieint 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 ReferenceRue 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 changeFriend 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(); y2. 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 body3. 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 codeAssignment: 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 CODEWhat 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 [] pS 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 destructorCORRECT 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<
® INHERITANCE Inheritance is the capability of extending the features of an existing class ( Base class ) into another newly created class (Derived class) WHAT IS THE BENEFIT OF INHERITANCE ? ‘The most impt benefit offered by INHERITANCE is CODE REUSABILITY. The derived class programmer is. not required to redesign those functions again which have already been coded by the BASE CLASS. programmer and the derived class can directly use them. This saves time /effort on the part of dervied class programmer— os Person age name get() show() 3 Ax Tahetb Student \ pe toll Player |——marks __| Sew ‘admission() score giveexam() playgame() Raw chan peck Oo Actor movies_rel acting() signmovie()® 1. Single Inheritance + se ) ‘TYPES OF INHERITANCE 2. Multilevel Inheritance Hf i 3. Multiple Inheritance4, Hierarchial Inheritance 5. Hybrid Inheritance J\a 2" pe |_2eo SYNTAX OF INHERITING A CLASS class
{ 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 objcaller Function ae EARLY BINDING Box::show() Box “
Derv obj EARLY BINDINGLady Bibs \ fet = te Ptclass Box { Int i; int b; int hj public: void get() q cout<<"Enter I,,h:"; cin>>I>>b>>h; =D cout<
+ a-- > Fupetionn >} . queenclass 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
qRules: 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<
{ u class
: private
{ class
{ class
; t
Both are same and inheritance is being done in “private” mode1. All the public members of the base class will become PRIVATE members of the derived class. That Is they can be accessed from the member functions of the derived class but not directly from the object of the derived class. from outside the class. 2.All the protected members of the base class will become PRIVATE members of the derived class. That Is they can be accessed from the member functions of the derived class but not directly from the object of the derived class from outside the 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: void get() { cout<<"enter 1b, cin>>I>>b>>h; void show() cout<
>mat; + void display() { show(); cout<
void £10); int x; |____ public: void £1(); class B: private A class C:public B private: +] int x; void f1(); Not allowed to access 'x' or f1() , because in 8 class they have been declared as privateMULTILEVEL INHERITANCE class A Members Of A I class 8 Members Of A Members Of B I class C ‘Members of A Members of 8 Members of Cclass Num ‘ © protected: int a,b; public: void get() { cout<<"Enter 2 int: cin>>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
C++ Notes Complete
PDF
67% (6)
C++ Notes Complete
145 pages
Introducing Classes: Constructor Function
PDF
No ratings yet
Introducing Classes: Constructor Function
6 pages
Vivek C++
PDF
No ratings yet
Vivek C++
25 pages
Programming in C++
PDF
No ratings yet
Programming in C++
42 pages
Sem3 (OOP) UNIT - 3
PDF
No ratings yet
Sem3 (OOP) UNIT - 3
39 pages
Lecture 5
PDF
No ratings yet
Lecture 5
12 pages
OOP Week 06
PDF
No ratings yet
OOP Week 06
21 pages
Lecture - 8
PDF
No ratings yet
Lecture - 8
55 pages
Oop Unit Ii
PDF
No ratings yet
Oop Unit Ii
37 pages
21-22 Solution
PDF
No ratings yet
21-22 Solution
38 pages
Oops Unit 2 Notes
PDF
No ratings yet
Oops Unit 2 Notes
23 pages
Chap 3 (Encapsulation)
PDF
No ratings yet
Chap 3 (Encapsulation)
48 pages
C++ Final
PDF
No ratings yet
C++ Final
26 pages
Chapter 3 Constructors and Desctructors
PDF
No ratings yet
Chapter 3 Constructors and Desctructors
66 pages
Lecture # 14
PDF
No ratings yet
Lecture # 14
19 pages
C++ Oops Concepts: Oops (Object Oriented Programming System)
PDF
No ratings yet
C++ Oops Concepts: Oops (Object Oriented Programming System)
11 pages
C++ Oops Concepts: Oops (Object Oriented Programming System)
PDF
No ratings yet
C++ Oops Concepts: Oops (Object Oriented Programming System)
10 pages
Scope Resolution Operator:: (C++ Only)
PDF
No ratings yet
Scope Resolution Operator:: (C++ Only)
7 pages
OOP - UNIT2 - Student
PDF
No ratings yet
OOP - UNIT2 - Student
31 pages
2 C++
PDF
No ratings yet
2 C++
137 pages
CPP Notes
PDF
No ratings yet
CPP Notes
31 pages
C++ Assignments
PDF
No ratings yet
C++ Assignments
12 pages
Unit-3 1
PDF
No ratings yet
Unit-3 1
41 pages
Class and Objects New
PDF
No ratings yet
Class and Objects New
39 pages
History of C++
PDF
No ratings yet
History of C++
98 pages
Destructors:: "This" POINTER
PDF
No ratings yet
Destructors:: "This" POINTER
5 pages
18BCS33C U2
PDF
No ratings yet
18BCS33C U2
116 pages
CPP - Unit II - Constructor and Distructor
PDF
No ratings yet
CPP - Unit II - Constructor and Distructor
28 pages
WINSEM2024-25 BCSE102L TH VL2024250501598 2025-02-25 Reference-Material-I
PDF
No ratings yet
WINSEM2024-25 BCSE102L TH VL2024250501598 2025-02-25 Reference-Material-I
28 pages
Unit 2
PDF
No ratings yet
Unit 2
36 pages
Con As Tractor
PDF
No ratings yet
Con As Tractor
10 pages
Oosd Unit 5
PDF
No ratings yet
Oosd Unit 5
34 pages
C++ Part V Constructors and Destructors
PDF
No ratings yet
C++ Part V Constructors and Destructors
10 pages
Constructor
PDF
No ratings yet
Constructor
13 pages
OOPS With C++
PDF
No ratings yet
OOPS With C++
20 pages
C++ Object and Class
PDF
No ratings yet
C++ Object and Class
35 pages
25 March 2024 20:24: This Pointer
PDF
No ratings yet
25 March 2024 20:24: This Pointer
4 pages
Segment 2
PDF
No ratings yet
Segment 2
21 pages
C++ Slides - I: Objects and Classes: Structure in C and C++, Class Specification, Objects
PDF
No ratings yet
C++ Slides - I: Objects and Classes: Structure in C and C++, Class Specification, Objects
63 pages
Class and Object
PDF
No ratings yet
Class and Object
16 pages
Oops
PDF
No ratings yet
Oops
43 pages
Session 13
PDF
No ratings yet
Session 13
23 pages
Oops Presentation
PDF
100% (1)
Oops Presentation
58 pages
02.oops Concepts
PDF
No ratings yet
02.oops Concepts
25 pages
C++ Notes
PDF
No ratings yet
C++ Notes
12 pages
Module 1 C++editted
PDF
No ratings yet
Module 1 C++editted
24 pages
Unit II
PDF
No ratings yet
Unit II
80 pages
3rd Chapter
PDF
No ratings yet
3rd Chapter
12 pages
Constructorsand Destructors
PDF
No ratings yet
Constructorsand Destructors
12 pages
C++ Notes Complet
PDF
100% (1)
C++ Notes Complet
146 pages
OOP Lab Material
PDF
No ratings yet
OOP Lab Material
94 pages
Constructor in C++
PDF
No ratings yet
Constructor in C++
16 pages
C and C++ Answer Key ESE
PDF
No ratings yet
C and C++ Answer Key ESE
16 pages
C
PDF
No ratings yet
C
11 pages
OOPs-Unit 3
PDF
No ratings yet
OOPs-Unit 3
41 pages
Industrial Training File
PDF
No ratings yet
Industrial Training File
29 pages
Unit 2
PDF
No ratings yet
Unit 2
30 pages
Oosd Unit - 5
PDF
No ratings yet
Oosd Unit - 5
24 pages
C++ Notes Complet
PDF
No ratings yet
C++ Notes Complet
146 pages