Oop Unit II

Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

JAIHIND COLLEGE OF ENGINEERING,PUNE

AICTE APPROVED INSTITUTE AFFILIATED TO


SAVITRIBAI PHULE PUNE UNIVERSITY

Department of AI & DS
COURSE : Object oriented Programming
UNIT II : Inheritance & Pointer

SUBJECT TEACHER : PROF SINALKAR M. G.


Inheritance
• “The language mechanism by which one class acquires the properties (data and
operations) of another class
• Base Class (or superclass): the class being inherited from
• Derived Class (or subclass): the class that inherits

• -Existing class is known


as base class or parent
class or super class and

• new class is known as


derived class or child
class or sub class.
Inheritance
Advantages of inheritance

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

class Shape int main ()


{ {
protected: Rectangle rect;
float width, height; rect.set_data (5,3);
public: cout << rect.area() << endl;
void set_data (float a, float b) return 0;
{ }
width = a;
height = b;
}
};

class Rectangle: public Shape


{
public:
float area ()
{
return (width * height);
}
};
Multiple Inheritance

● In this type of inheritance a single derived class may


inherit from two or more than two base classes.

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

● In this type of inheritance the derived class inherits


from a class, which in turn inherits from some other
class. The Super class for one, is sub class for the other.

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

● In this type of inheritance, multiple derived classes


inherits from a single base class.

JCOE

Mech CSE civil


example

class A //Base Class class C :public A //Derived Class 2


{ {
public: public:
int a,b; void cube()
void getnumber() {
{ getnumber(); //Call Base class property
cout<<"\n\nEnter Number :::\t"; cout<<"\n\n\tCube of the number
cin>>a; :::\t"<<(a*a*a);
} }
};
};
int main()
class B : public A //Derived Class 1 {
{ clrscr();
public:
void square() B b1; //b1 is object of Derived class 1
{ b1.square(); //call member function of class B
getnumber(); //Call Base class property C c1; //c1 is object of Derived class 2
cout<<"\n\n\tSquare of the number :::\t"<<(a*a);c1.cube(); //call member function of class C
}
}; getch();
}
Hybrid Inheritance

● Hybrid Inheritance is combination of Hierarchical and


Multilevel Inheritance.
example
class minus
class arithmetic class result:public plus, public min
{ { {
protected: protected: public:
int num1, num2; int n1,n2,diff; void display()
public: public: {
void getdata() void sub() cout<<sum;
{ { cout<<diff;
cout<<"For Addition:"; cout<<"\nFor Subtraction:"; }
cout<<"\nEnter the first number: cout<<"\nEnter the first number: "; };
"; cin>>n1; void main()
cin>>num1; cout<<"\nEnter the second number: ";{
cout<<"\nEnter the second cin>>n2; clrscr();
number: "; diff=n1-n2; result z;
cin>>num2; } z.getdata();
} class plus:public arithmetic }; z.add();
};{ z.sub();
protected: z.display();
int sum; getch();
public: }
void add()
{
sum=num1+num2;
}
};

You might also like