0% found this document useful (0 votes)
46 views

3 - Inheritance

This document discusses inheritance in C++. It defines inheritance as allowing a new class to inherit attributes and behaviors from an existing class. The existing class is called the base or parent class, while the new class is the derived or child class. There are three types of inheritance based on access control: public, protected, and private. The document provides examples of single, multilevel, and multiple inheritance in C++ code. It also discusses accessing members of base classes from derived classes.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

3 - Inheritance

This document discusses inheritance in C++. It defines inheritance as allowing a new class to inherit attributes and behaviors from an existing class. The existing class is called the base or parent class, while the new class is the derived or child class. There are three types of inheritance based on access control: public, protected, and private. The document provides examples of single, multilevel, and multiple inheritance in C++ code. It also discusses accessing members of base classes from derived classes.

Uploaded by

Abdullah Rayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CHAPTER 3

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

Access Control in Inheritance

 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

Access Public Protected Private


Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no

MMH
Slide-6

Access Control in Inheritance


 Public base class: public members of the base class are public members of the
derived class. 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.

 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:

class Rectangle: public Polygon, public Output;


class Triangle: public Polygon, public Output;

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

 Tech Yourself C++, Herbert Schildt.


 C++ How To Program, Paul Deitel, Harvey Deitel.
 The C++ Complete Reference, Herbert Schildt.
 Thinking in C++, Volume One, Bruce Eckel.

You might also like