Oop Unit II
Oop Unit II
Oop Unit II
Department of AI & DS
COURSE : Object oriented Programming
UNIT II : Inheritance & Pointer
When a class inherits from another class, there are three benefits:
(1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new methods
(3) You can modify the existing class by overloading its methods with your own
implementations
Inheritance
• There is no limit on the depth of inheritance allowed in C++ (as far as it
is within the limits of your compiler)
• It is possible for a class to be a base class for more than one derived class
• Polymorphism:
• Any code you write to manipulate a base class will also work with any
class derived from the base class.
• C++ general rule for passing objects to a function:
“the actual parameters and their corresponding formal parameters
must be of the same type”
• With inheritance, C++ relaxes this rule:
“the type of the actual parameter can be a class derived from the class
of the formal parameter”
class classname
{
private: //visible to member functions within its class
. . . . //but not in derived class
protected: //visible to member functions within its class
. . . . //and its derived class
public: //visible to member functions within its class,
. . . . //derived classes and through object
};
Access Specifier
● Publicly inherited.
1. Private members of the base class cannot be inherited.
2. Public members of the base class become public to derived class.
3. Protected members of the base class become protected to derived class.
Therefore they are accessible to the member functions of the derived class. And
also further inheritance is possible.
● Privately inherited
1. public members of base class become private to derived class. Therefore the
public members of the base class can only be accessed by the members of the
derived class.
2. Private members of the base class cannot be inherited to the derived class.
3. Protected members of the base class also become private to the derived class. they
can have access to the member functions of the derived class.
● protected mode.
1. Public and protected members of the base class become protected to the
derived class.
Syntax & EXAMPLE
Syntax : class Subclass_name : access_mode Superclass_name
Access Mode is used to specify, the mode in which the properties of superclass will be
inherited into subclass, public, private or protected.
EXAMPLE:
class Animal // base class
{ public:
int legs = 4;
};
class Dog : public Animal // derived class
{ public:
int tail = 1;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
return 0;
}
Single inheritance
● In this type of inheritance one derived class inherits from only one
base class. It is the most simplest form of Inheritance.
base class:
class A
{
//members of
A
}
Derived class syntax:
class B: public A
{
//members of B
};
Example
father mother
child
class student Example
{
protected:
class statement:public student,public sports
int rno,m1,m2;
{
public:
int tot,avg;
void get()
public:
{
void display()
cout<<"Enter the Roll no :";
{
cin>>rno;
tot=(m1+m2+sm);
cout<<"Enter the two marks
avg=tot/3;
:";
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal
cin>>m1>>m2;
: "<<tot;
}
cout<<"\n\tAverage : "<<avg;
};
}
class sports
};
{
void main()
protected:
{
int sm; // sm = Sports mark
clrscr();
public:
statement obj;
void getsm()
obj.get();
{
obj.getsm();
cout<<"\nEnter the sports mark :";
obj.display();
cin>>sm;
getch();
}
}
};
Note:- ambiguity resolution in multiple inheritance same function name is used in
more than one base class ambiguity occurs which is to be executed
- Use scope resolution operator.
class Z: public
class X
{ X,public Y
public: {
void display() public:
{ void display()
cout<<“\n class X”; {
} X::
}; display();
} Output:
class Y class X
{ };
Class Y
public:
void display() Int main()
{ {
cout<<“\n class Y”; Z z;
} z.display();
}; or z.X::display();
z Y:: display();
return 0;
}
Multilevel Inheritance
Grand
father
father
son
Multilevel Inheritance
class A
{
public:
void display()
{
cout<<"Base class content.";
} int main()
}; {
C c;
c.display();
class B : public A return 0;
{ }
};
class C : public B
{
};
Hierarchical Inheritance
JCOE