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

Lecture 7

Inheritance is a mechanism in object-oriented programming that allows a new class (subclass) to derive properties and behaviors from an existing class (superclass). It promotes reusability of code and can take various forms, including single, multiple, hierarchical, multi-level, and hybrid inheritance. C++ also addresses issues like ambiguity in inheritance through the use of virtual base classes.

Uploaded by

shoumiksarker07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 7

Inheritance is a mechanism in object-oriented programming that allows a new class (subclass) to derive properties and behaviors from an existing class (superclass). It promotes reusability of code and can take various forms, including single, multiple, hierarchical, multi-level, and hybrid inheritance. C++ also addresses issues like ambiguity in inheritance through the use of virtual base classes.

Uploaded by

shoumiksarker07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Inheritance

1
Introduction to Inheritance

• This mechanism of deriving a new class from


existing/old class is called “inheritance”.
• The old class is known as “base” class,
“super” class or “parent” class”;
– and the new class is known as “sub”
Parent
class, “derived” class, or “child”
class.
Inherited
capability

Child

2
Introduction to Inheritance

• Reusability--building new components by utilising existing components- is


yet another important aspect of OO features.
• It is always good/“productive” if we are able to reuse something that is
already exists rather than creating the same all over again.
• This is achieve by creating new classes, reusing the properties of existing
classes.
Different forms of inheritance
• The different forms of inheritance are:

– Single inheritance (only one super class)


– Multiple inheritance (several super classes)
– Hierarchical inheritance (one super class, many sub
classes)
– Multi-Level inheritance (derived from a derived class)
– Hybrid inheritance (more than two types)
Forms of inheritance
B D B C

A A B C A

(a) Single Inheritance (b) Multiple Inheritance (c)Hierarchical Inheritance


D
C

B C
B

A A
(d) Multi-Level Inheritance (f) Hybrid Inheritance
Defining Derived Class
• class derived-class-name : visibility-mode base-class-name
• {………};

• class derived-class: access-specifier base-class { };

 Visibility mode is optional.(by default: private)

 Visibility mode specifies whether the features of the base class


are
privately derived or publicly derived.
Single Inheritance
when a single derived class is created from a single base
class then the inheritance is called as single inheritance.

class Base
{ .... ... .... };

class Derive : public Base


{ .... ... .... };

int main()
{… };
Single Inheritance
// Derived class
#include <iostream>
class Rectangle: public Shape
using namespace std;
{
// Base class
public:
class Shape
int getArea()
{
{
public: return (width * height);
void setWidth(int w) }
{ };
width = w; int main(void)
} {
void setHeight(int h) Rectangle Rect;
{ Rect.setWidth(5);
height = h; Rect.setHeight(7);
} // Print the area of the object.
protected: cout << "Total area: " <<
int width; Rect.getArea() << endl;
int height; return 0;
}
};
Inheritance: Private, Protected and Public

 Inside the class.


class alpha
{
private://optional
…………//visible inside this class only
protected://visible inside this class
…………//and its immediate derived class
public://visible to all
…………
};
Inheritance: Private, Protected and Public Visibility
mode
Multiple Inheritance
 when a derived class is created from more than one base
class then that inheritance is called as multiple
inheritance.
Or
 A C++ class can inherit members from more than one
class and here is the extended syntax:

class derived-class: access-specifier baseA, access baseB....


Multiple Inheritances Example
// Base class PaintCost int main(void)
class Shape{
public: class PaintCost { {
void setWidth(int w) public: Rectangle Rect;
{ int getCost(int area)
int area;
width = w; {
Rect.setWidth(5);
} return area * 70;
} Rect.setHeight(7);
void setHeight(int
h) }; area = Rect.getArea();
{ // Derived class // Print the area of the object.
height = h; class Rectangle: public cout << Rect.getArea() << endl;
} Shape, public PaintCost {
// Print the total cost of painting
protected: public:
int getArea() cout << Rect.getCost(area) <<
int width; endl;
int height; {
return (width * height); return 0;
};
} }
};
Hierarchical Inheritance
when more than one derived class are created from a single base class,
then that inheritance is called as hierarchical inheritance.

class base_classname
{…………….};

class derived_class1:visibility_mode base_classname


{……………………};

class derived_class2:visibility_mode base_classname


{………………….};
Hierarchical Inheritance Example:
class Rectangle /* Area class is derived from base
{ class Rectangle. */
protected:
float length, breadth; class Area : public Rectangle
public: {
Void Set() public:
{
float calc()
cout<<"Enter length: ";
{
cin>>length;
return length*breadth;
cout<<"Enter breadth: ";
cin>>breadth; }
} };
};
/* Perimeter class is derived from int main()
base class Rectangle. */ {
class Perimeter : public Rectangle cout<<"Enter first rectangle to find area.\n";
{
public: Area a;
float calc() a.Set();
{ cout<<"Area = "<<a.calc()<<"square meter";
return 2*(length+breadth); cout<<"Enter second rectangle to find perimeter";
}
}; Perimeter p;
p.Set()
cout<<"\nPerimeter = "<<p.calc()<<" meter";

return 0;
}
Multi Level Inheritance
when a derived class is created from another derived class,
then that inheritance is called as multi level inheritance.

class A
{ .... ... .... };
class B : public A
{ .... ... .... };
class C : public B
{ .... ... .... };
Multi Level Inheritance Example
#include <iostream> class B : public A
using namespace std; { };

class A class C : public B


{ { };
public:
int main()
void display() {
{ C c;
cout<<"Base class content."; c.display();
} return 0;
}; }
Hybrid Inheritance
Any combination of single, hierarchical and multi level
inheritances is called as hybrid inheritance.
Hybrid Inheritance
#include<iostream> class B :public A
using namespace std; {
class A //Base class public:
{ int b,c;
public: void l_into_b()
int l; {
void len() len();
{ cout<<"\n\nBreadth :::\t";
cout<<"\n\nLength :::\t"; cin>>b;
cin>>l; c=b*l;
} }
}; };
class C //Hybrid Inheritance Level
class D:public B,public C
{ {
public: public:
int res;
int h; void result()
void height() {
{ l_into_b();
height();
cout<<"\n\nHeight :::\t"; res=h*c;
cin>>h; cout<<"\n\nResult (l*b*h) :::\
t"<<res<<endl;
} }
}; };
int main()
{
D d1;
d1.result();
}
Constructors in Derived Class
Constructors in Derived Class
Outpu
t
Virtual Base Class
An ambiguity can arise when several paths exist to a class from the
same base class. This means that a child class could have duplicate
sets of members inherited from a single base class. This is called the
diamond shaped problem.

C++ solves this issue by introducing a virtual base class.


When a class is made virtual, necessary care is taken so
that the duplication is avoided regardless of the number
of paths that exist to the child class.
Virtual Base Class
Virtual Base Class
Virtual Base Class

#include<iostream> class D: public B, public C


using namespace std; {
class A public:
{ int sum;
public: };
int i;
}; int main()
{
D ob;
class B : virtual public A
ob.i = 10; //unambiguous since only one copy of i is inherited.
{
ob.j = 20;
public:
ob.k = 30;
int j;
ob.sum = ob.i + ob.j + ob.k;
}; cout << "Value of i is : "<< ob.i<<"\n";
cout << "Value of j is : "<< ob.j<<"\n";
class C: virtual public A cout << "Value of k is :"<< ob.k<<"\n";
{ cout << "Sum is : "<< ob.sum <<"\n";
public:
int k; return 0;
}; }

You might also like