3 - Inheritance
3 - Inheritance
INHERITANCE
COURSE NAME
INTRODUCTION TO
PROGRAMMING
CSC 1102
MD.NAZMUL HOSSAIN
(UNDERGRADUATE)
LECTURER, CS, AIUB
Slide-2
Inheritance
Inheritance provides an opportunity to reuse the code functionality and fast
implementation time.
When creating a class, instead of writing completely new data members and member
functions, the programmer can designate that the new class should
inherit the members of an existing class.
The mechanism of deriving a new class from an old class/previous written class in
known as inheritance. Also known as “is a” or ”kind of” or ”is a kind of” relationship.
The class which is inherited is called base class/parent class/super class. The class that
inherits the base class is known as sub class/child class/derived class.
class derived-class: access-specifier base-class
MMH
Slide-3
Inheritance (ex1)
#include <iostream> // Derived class
using namespace std; class Rectangle: public Shape //default private
{
class Shape // Base class public:
{ int getArea() {
public: return (width * height);
void setValues(int h,int w) }
{ };
height = h;
width = w; int main(void){
} Rectangle r1;
protected:
int width; r1.setValues(5,7);
int height; cout <<“Rectangle area:"<<r1.getArea();
};
return 0; OUTPUT
} Rectangle area: 35
MMH
Slide-4
#include <iostream>
Inheritance (ex2)
class Rectangle: public Shape {
using namespace std;
public:
int getArea(){
class Shape // Base class
return (width * height);
{ public:
}
void setValues(int h,int w) child class can access member
};
{ from base class, but base class
int main(void) {
height = h; cannot access member from
Shape sh;
width = w; child class
Rectangle re;
}
re.setValues(5,7);
int getArea() {
cout <<"Shape area:“ <<sh.getArea();
return (width * height);
cout << “Rectangle area:”
}
<< re.getArea(); sh obj doesn’t have
protected: value of height & width
return 0;
int width; OUTPUT
}
int height; Shape area: garbage
}; Rectangle area: 35
MMH
Slide-5
A derived class can access all the non-private members of its base class. Thus base-
class members that should not be accessible to the member functions of derived
classes should be declared private in the base class.
Default access specifier of inheritance from base class to child class is Private
MMH
Slide-6
Protected base class: Both public and protected members of the base class
are protected members of the derived class. private members of the base class
remain private to the base class.
Private base class: Both public and protected members of the base class
are private members of the derived class. private members of the base class
remain private to the base class.
MMH
Slide-7
Multiple Inheritance
Deriving directly from more than one class is usually called multiple inheritance.
A class may inherit from more than one class by simply specifying more base classes,
separated by commas, in the list of a class's base classes (i.e., after the colon).
For example, if the program had a specific class to print on screen called Output, and
we wanted our classes Rectangle and Triangle to also inherit its members in addition
to those of Polygon we could write:
MMH
Slide-8
#include <iostream> Multiple Inheritance
using namespace std; class Rectangle: public Shape, public PaintCost{
public:
class Shape { int getArea(){
public: return (width * height);
void setWidth(int w) { }
width = w; };
}
void setHeight(int h){ int main(void){
Rectangle Rect;
height = h;
Rect.setWidth(5);
} Rect.setHeight(7);
protected: int area = Rect.getArea();
int width; int height;
}; cout<<"Total area:"<< area <<endl;
class PaintCost { cout<<"Total paint cost:$“
public: << Rect.getCost(area);
int getCost(int area) { return 0;
} OUTPUT
return area * 70; Total area: 35
} Total paint cost: $2450
}; MMH
Slide-9
Multilevel Inheritance
#include <iostream>
using namespace std;
class A int main()
{ {
public: C obj;
void display() obj.display();
{ return 0;
cout<<"Base class content."; }
}
};
class B : public A { }; OUTPUT
Base class Content
class C : public B { };
MMH
Slide - 20
REFERENCES