You are on page 1of 22

INHERITENCE

Week-7 (Part 1 & 2)

Object Oriented Programming


CSSE-2133 Object Oriented Programming 2

Introduction
• Inheritance is one of the key feature of object-oriented
programming including C++ which allows user to create a
new class(derived class) from a existing class(base class)
• The derived (child) class inherits all feature from a base
(parent) class and it can have additional features of its
own
CSSE-2133 Object Oriented Programming 3

Introduction Cont..
CSSE-2133 Object Oriented Programming 4

Advantages
• Saves memory space
• Saves time
• Increases reliability of the code
• Saves the developing and testing efforts
CSSE-2133 Object Oriented Programming 5

Syntax
class drivedClass_name :
access_mode baseClass_name
e.g.
Rectangle : Public Shape
CSSE-2133 Object Oriented Programming 6

Inheritance Visibility Mode


• Depending on Access modifier used while inheritance, the
availability of class members of parent class in the child
class changes
• It can either be
• Private
• Protected
• Public
CSSE-2133 Object Oriented Programming 7

Private Inheritance
• In private mode, the protected and public members of
super class become private members of derived class
• Syntax is
class Subclass : private Superclass OR
class Subclass : Superclass // Default
CSSE-2133 Object Oriented Programming 8

Protected Inheritance
• In protected mode, the public and
protected members of Super class
becomes protected members of Sub
class
• Syntax is
class Subclass : protected
Superclass
CSSE-2133 Object Oriented Programming 9

Inheritance Visibility Mode


CSSE-2133 Object Oriented Programming 10

Example
class Shape {
protected:
float length;
float width;
};
Class Rectangle : public Shape{
public:
float getArea ();
} ;
CSSE-2133 Object Oriented Programming 11

Example Cont..
•Implement a Polygon class that contains length and width
data members. Derive two classes Triangle and Rectangle
class from Polygon class and calculate area (Area of
rectangle is length x width and area of triangle is (length x
width)/2)
CSSE-2133 Object Oriented Programming 12

Example Cont..
class CPolygon { class CTriangle: public
protected: CPolygon {
int width, height; public:
public: int area (){ return (width *
void set_values (int a, int height / 2); }
b) };
{ width=a; height=b;} void main () {
}; CRectangle rect;
class CRectangle: public
CTriangle trgl;
CPolygon {
public: rect.set_values (4,5);
int area () trgl.set_values (4,5);
{ return (width * height); } cout << rect.area() << endl;
}; cout << trgl.area() << endl;
}
CSSE-2133 Object Oriented Programming 13

Order of Constructor and Destructor Execution

• Base class constructors are always


executed first
• Destructors are executed in exactly
the reverse order of constructors
CSSE-2133 Object Oriented Programming 14

Order of Constructor and Destructor Execution


Cont..
class Base{
public:
Base ( )
{cout << "Inside Base constructor" << endl;}
~Base ( )
{ cout << "Inside Base destructor" << endl;}
}; Output
class Derived : public Base{
public:
Inside Base
constructor
Derived ( )
Inside Derived
{cout<< "Inside Derived constructor\n";}
constructor
~Derived ( )
Inside Derived
{cout<<"Inside Derived destructor\n";}
destructor
}; Inside Base
void main( ){ destructor
Derived x;}
CSSE-2133 Object Oriented Programming 15
CSSE-2133 Object Oriented Programming 16
CSSE-2133 Object Oriented Programming 17

Inheriting Constructors
• If the base class constructor takes no parameters then the
inheritance is implicit - you don’t need to do anything!
• If the base class constructor takes parameters then each
derived class needs to declare a constructor with the
same parameters
• You can pass the arguments given to the derived class constructor
to the constructor for the base class
CSSE-2133 Object Oriented Programming 18

Inheriting Constructors Cont..


class Person{
protected: class Student: public Person{
int age; public:
void setAge(int a)
public: { age = a; }
Person() };
{age = -5;}
Person(int a) int main(){ Default
{age = a;} Student p; constructor
cout<<p.getAge();
int getAge() Student p1(15);
{return age;} }
}; Parameterized constructor
can’t be inherited !!
Compilation error
CSSE-2133 Object Oriented Programming 19

Inheriting Constructors Cont..


class Student: public Person{
public:
Student(int age):Person(age){
}
void setAge(int a){ age = a; }

In the implementation of the


constructor for the derived class,
};
the parameter passed to the
derived class constructor is passed
down to the base class constructor
CSSE-2133 Object Oriented Programming 20

Inheriting Constructors Cont..

derived class constructor base class constructor

Shape(int side) : Rectangle(side,side)

derived base
constructor constructor
parameter parameters
CSSE-2133 Object Oriented Programming 21

Inheriting Constructors Cont..


class base{ class derv1 : public base{
private: private:
int x; int y;
public: public:
base (int x1) derv1 (int yy, int xx) : base
{ (xx){
cout<<"Base's cout << " Derv1's constructor
constructor executed"; executed";
x = x1; y = yy;
cout<<x<<endl; cout<<y<<endl;
} }
}; };
void main ( ){
derv1 d (20, 30);} Base’s constructor executed 30
Derv1’s constructor executed 20
Output:
class ABC
{
private: int x,y;
public:
ABC ()
{ x = y = 0; }
ABC(int a)
{ x = y = a; }
ABC(int a,int b)
{
x = a*0;
y = b*20;
}
void display()
{ cout << "x = " << x << " \t " << "y = " << y << endl; }
};

void main()
{
ABC cc1, cc2(10), cc3(10,20);
cc1.display();
cc2.display();
cc3.display();
}

You might also like