285 - OOPS Lecture Notes Complete
285 - OOPS Lecture Notes Complete
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
t
cout<< hours<
"hours and:
cout<
class sample
{
int a;
int bs
public:
void setvalue( ) { a=25;6-40;}
friend float mean( sample s);
3
Moat mean (sample s)
t
return (float(s.as.b)/2.0);
}
int main ()
{
PTCsample x;
x. setvalue( );
‘eout<<""mean value="<
class abe;
class xyz
t
int x;
public:
void setvalue(int x) {x-=T }
friend void max (xyz,abe);
Kh
class abe
{
int a;
public:
void setvalue( int i) {a-i; }
friend void max(xyz,abe);
b
void max( xyz m, abe n)
{
iffm.x>=na)
cout<
class class-
class class-1
{
56 PTCint value 1;
public:
void indata( int a) { value=a; }
void display(void) { cout<
class account];
class account2
t
private:
int balance;
public:
account2( ) { balance=567; }
void showace2( )
{
cout<<"balanceinaccount2 is:”<
class complex
{
float x;
float y;
public:
void input( float real, float imag)
{
xereal;
yeimags
}
friend complex sum( complex , complex);
void show (complex );
Kh
complex sum ( complex el, complex ¢2)
{
complex ¢3;
cB.x-cL.xte2.x;
Byrclyte2y;
return 3;}
void complex. :: show ( complex c)
t
cout< * ip;
cout<a;
‘The deferencing operator ->* is used as to accept a member when we use pointers to
both the object and the member. The dereferencing operator. .* is used when the object itself is used
with the member pointer. Note that * ip is used like a member name.
We can also design pointers to member functions which ,then can be invoked using
the deferencing operator in the main as shown below.
(object-name.* pointer-to-member function)
{pointer-to -object -> * pointer-to-member function)
The precedence of ( ) is higher than that of .* and ->* , so the parenthesis are
61
PTCDEREFERENCING OPERATOR:
#inelude=iostream.h>
class M
t
int x;
int y;
public:
void set_xy(int aint 6)
1
xa;
yb;
3
friend int sum(M);
b
int sum (Mm)
{
int M :: * px= &M :: x; //pointer to member x
int M =: * pys & m::yy//pointer to y
M * pm=&m;
int s=m.* px + pm-=py;
return(s);
}
int main ()
{
Mm;
void(M::*p{){int int)=&M::set-xy;//pointer to funetion set-xy (n*pf)(_ 10,20);
Finvokes set-xy
cout<<"sum=:"<* pi)(30,40); !/ invokes set-xy
couts<"'sum="<
#finelude
‘tbe used as a member of
class abe
{
private:
char nm[];
public:
abe ()
t
cout<<"enter your name:”;
cin>>nm;
}
void display( )
{
}
cout<
class integer
{
int mn;
public:
integer(int,int);
void display(void)
{
cout<<’m="<
‘#include
class abe
{
private:
char nm [30];
int age;
public:
abe (){ }i/ default
abe ( char x{], int y);
void get( )
t
cout<<"enter your name:”;
cin>>nm;
cout<<" enter your age:”;
cin>>age;
}
void display( )
{
cout<
#inelude
class sum
t
private;
int a;
int bs
int ¢;
float d;
double e;
public:
sum (){
cout<<"enter a;”;
cin>>a;
cout<<"enter b;”;
cin>>b;
cout<<"sum
}
sum(int a,int b);
sum(int a, float d,double ¢);
class code
{
int idpublic
code () { } constructor
code (int a) { id=; }//constructor
code(code &x)
{
Id=x.ids
t
void display( )
cout<
#include
class string
{
char *name;
int length;
public:
string ()iength-0;
name= new char [length+1]; /* one extra for \0 */
string( har 3) /leonstructor 2
{
Iength=strlen(s);
name=new char [length+1];
strepy(name,s);
}
void display(void)
{
cout<
int count=0;
class alpha
{
public:
alpha( )
t
count 11;
cout<<"'n no of object created :”<
int x=1;
class abe
{
public:
abe()
1
cout<<"construct the no”<
finelude
class stock2;
class stock!
{
int code,
float price;
public:
stockl (int a, int b, loat c)
t
code=a;
item=b;
price=c;
I
void disp()
cout<<"Items”<-
#inelude
class worker
{int age;
char name [10];
public:
void get ();
8
void worker : : get ()
{
cout <<"yout name please”
cin >> name;
"your age please” ;
age;
t
void worker =: show ( )
{
cout <<"In My name is :”*<>name>>age;
} Gf they were public )
void manager :: show ()
{
worker :: show (); _//ealling of base class ofp fn
cout <<"in No, of workers under me are: “<< now;
}
main ()
t
clrser ()
worker WI;
manager M1;
ML get ();
MLshow ( ):
}
If you input the following to this program:
‘Your name please
Ravinder
Your age please
2
‘number of workers under you
30‘Then the output will be as follows
My name is : Ravinder
My age is : 27
No. of workers under me are : 30
The following program shows the single inheritance by private derivation.
#include
Hinelude
class worker //Base class declaration
{
int age;
char name [10] ;
public:
void get ();
void show () ;
h
void worker : : get ()
{
cout << “your name please” ;
cin >> name;
cout << “your age please”;
cin >>age;
}
void worker : show ()
t
cout <<"“in my name is: “ <> now;
}
‘void manager : : show ()
{
worker : : show ();
cout <<"in no. of worker under me are : “<
#include
class worker ("Base class declaration
{ protected:
int age; char name [20];
public:
void get ():
void show ();
8
void worker :: get ()
{
cout >> “your name please”;
ein >> name;
cout << "your age please”;
cin >> age;
}
void worker :: show ()
{
cout <<“in my name is: “<< name << ‘in my age is “ <> name;
cout<< “please enter the age In”; //Directly inputting the data
‘age; members of base class
* please enter the no. of workers under you”;
cin >> now;
t
void manager : : show ()
{
cout « "your name is : "«name«" and age is : “wage;
cout «In no, of workers under your are : "«now;
main ()
clrser ()
‘manager ml;
mLget ();cout «"\n in";
ml.show ();
}
Making a Private Member Inheritable
Basically we have visibility modes to specify that in which mode you are deriving the another class
from the already existing base class. They are:
a
Private: when a base class is privately inherited by a derived class, ‘public
membets' of the base class become private members of the derived class and
therefore the public members of the base class can be accessed by its own
objects using the dot operator. The result is that we have no member of base
class that is accessible to the objects of the derived class.
Public: On the other hand, when the base class is publicly inherited, ‘public
members’ of the base class become ‘public members’ of derived class and
therefore they are accessible to the objects of the derived clas
Protected: C++ provides a third visibility modifier, protected, which serve a
little purpose in the inheritance. A member declared as protected is accessible
by the member functions within its class and any class immediately derived
from it, It cannot be accessed by functions outside these two classes.
‘The below mentioned table summarizes how the visibility of members undergo modifications when
they are inherited
Base Class Visibility | Derived Class Visibility
Public | Private Protected
Private x x Lx
Public Public | Private Protected
Protected Protected | Private Protected
‘The private and protected members of a class can be accessed by:
a,
b.
ce
A function ie. friend ofa class.
A member function of a class that is the friend of the class.
A member function of a derived class.
Student Activity
1. Define Inheritance. What is the inheritance mechanism in C++?
2, What are the advantage of Inheritance?
3, What should be the structure of'a class when it has to be a base for other classes?Multilevel Inheritance
‘When the inheritance is such that, the class A serves as a base class for a derived class B which in
‘turn serves as a base class for the derived class C. This type of inheritance is called ‘MULTILEVEL
INHERITENCE”. The class B is known as the INTERMEDIATE BASE CLASS’ since it provides a
link for the inheritance between A and C. The chain ABC is called ‘ITNHERITENCE*PATH’ for
eg.
A l Base class
Inheritance path B i Intermediate base
class
c I Derived class
‘The declaration for the same would be
Class. A
‘
iTbody
I
Class B : public A
‘
Tbody
}
Class C : public B
{
‘iTeody
I
This declaration will form the different levels of inheritance.
Following program exhibits the multilevel inheritance
#include
#include
class worker // Base class declaration
‘
int age;
char name [20] ;
public;
void get() ;void show();
void worker: get ()
{
cout << “your name please” ;
“your age please” ;
}
void worker : : show ()
{
t
class manager : public worker //Intermediate base class derived
{ Jipublicly from the base class
cout << "In my name is :*< nom;
}
void manager : : show ()
{
cout <<“In the no. of managers under me are: In”;
cout <<main ()
{
elrser ( )5
ceo cl
elget (); cout <<“inin”;
elshow ();
Worker
Private
int age;
char name[20];
Protected:
Private;
int age;
char name[20);
Manager: Worker
Private:
int now;
Protected:
Public:
void get()
void show()
worker ::get()
worker ::get()
Ceo: Manager
Public:
Protected:
Public:
All the inherited
membersMultiple Inheritances
‘A class can inherit the attributes of two or more classes. This mechanism is known as ‘MULTIPLE.
INHERITENCE”. Multiple inheritance allows us to combine the features
of several existing classes as a starring point for defining new classes. It is like the child inheriting
the physical feature of one parent and the intelligence of another. The syntax of the derived class is
as follows:
Class base! Class base?
{ {
Jibodyl H body2,
} t
+
Class derived : visibility basel, visibility base2
{
‘foody3
i
Where the visibility refers to the access specifiers ie. public, private or protected. Following
program shows the multiple inheritance
finclude
#inelude
class father 1iDeclaration of base classl
{
int age ;
char flame [20] ;
public:
void get ();
‘void show () ;
1
void father : : get ()
{
cout <<“your father name please”;
cin >> name;
cout <<"“Enter the age”
cin >> age;
}
void father : : show ()
{
cout<<“In my father’s name is: ‘<> name;
cout << “mother’s age please” << “in”;
cin >> age;
}
void show ( )
{
cout <<“In my mother’s name is: “<> std;
}
void daughter :: show ()
t
father :: show ();
nnfather :: show ()
cout << “In child’s name is : “ <You might also like
- Procedure Oriented Programming Language
No ratings yetProcedure Oriented Programming Language273 pages - Introduction To C++ and Object Oriented Concepts: Mrs. Deepashree Mehendale
No ratings yetIntroduction To C++ and Object Oriented Concepts: Mrs. Deepashree Mehendale274 pages - EContent_3_2025_01_30_19_48_10_Unit1_lntroductionpptxUpdatedpptx__2025_01_16_15_52_58
No ratings yetEContent_3_2025_01_30_19_48_10_Unit1_lntroductionpptxUpdatedpptx__2025_01_16_15_52_5868 pages - Introduction To Object Oriented Programming: UNIT-1
No ratings yetIntroduction To Object Oriented Programming: UNIT-130 pages - Introduction To Object Oriented Programming1
No ratings yetIntroduction To Object Oriented Programming1215 pages - By: Marc Conrad University of Luton Email: Marc - Conrad@luton - Ac.uk Room: D104
No ratings yetBy: Marc Conrad University of Luton Email: Marc - Conrad@luton - Ac.uk Room: D10421 pages - Introduction To C++ AND Object Oriented Programming: Objectives
No ratings yetIntroduction To C++ AND Object Oriented Programming: Objectives18 pages - Cryogenic Valves For Industrial Gas Applications
No ratings yetCryogenic Valves For Industrial Gas Applications70 pages - Day 2 - 0830-0915 - IECEx - Dubai - Area - Classif - Final - Leroux - P
No ratings yetDay 2 - 0830-0915 - IECEx - Dubai - Area - Classif - Final - Leroux - P42 pages - User and Installation Manual LNG Tanks2
No ratings yetUser and Installation Manual LNG Tanks2188 pages - Uswphazarea Hazardous Area Classification Considerations 01 2021 R 1
No ratings yetUswphazarea Hazardous Area Classification Considerations 01 2021 R 14 pages - Piping Material Specification Piping Class cs101b
0% (1)Piping Material Specification Piping Class cs101b10 pages