PPT6 Inheritance Part 1
PPT6 Inheritance Part 1
Inheritance Part 1
1
Inheritance
• Inherit Definition -
Derive quality and characteristics from parents or ancestors.
• Example:
“He/She had inherited the wisdom of his/her parents"
• Inheritance in Object Oriented Programming can be described as a
process of creating new classes from existing classes.
2
Inheritance (Cont…)
• New classes inherit some of the properties and behaviour of
the existing classes.
• The existing class that is "parent" of a new class is called a base
class.
• New class that inherits properties of the base class is called
a derived class(“child class”).
• Inheritance is a technique of code reuse.
3
Example: Insect Taxonomy
4
The "is a" Relationship
• Inheritance establishes an "is a" relationship between classes.
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
5
Base Class access control
• Derived class can be declared from a base
class with different access control, i.e., public
inheritance, protected inheritance or private
inheritance.
6
Protected Members and Class Access
7
Class Access Specifiers
public – object of derived class can be treated as object of base
class (not vice-versa)
protected – more restrictive than public, but allows derived
classes to know details of parents
private – prevents objects of derived class from being treated as
objects of base class.
8
Syntax
1. #include <iostream>
2. using namespace std;
3. class base
4. { .... ... .... };
5. class derived : access_specifier base
6. { .... ... .... };
12
Accessibility in Protected Inheritance
13
Accessibility in Private Inheritance
14
Inheritance vs. Access
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z
16
Inheritance vs. Access
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
protected class access, it public members:
Test(int, int);
looks like this: protected members:
void setScore(float);
float getScore();
float getLetter();
17
Inheritance vs. Access
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
private class access, it void setScore(float);
float getScore();
looks like this: float getLetter();
public members:
Test(int, int);
18
What Does a Child Have?
An object of the derived class has:
• all members defined in child class
• all members declared in parent class
19
Types of Inheritance
20
Thanks
21
Types of Inheritance
22
Single Inheritance
• Single Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from one base class.
23
Single Inheritance Syntax
24
Single Inheritance Example
#include <iostream.h>
using namespace std;
class Shape {
protected:
int width;
int height;
public:
void setWidth(int w) {
width = w; }
void setHeight(int h) {
height = h; } } ;
class Rectangle: public Shape {
public:
int getArea() {
return (width * height); } }
int main {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total area: " << Rect.getArea() << endl; // Print the area of the object.
return 0;
}
25
#include <iostream.h> int main()
using namespace std; {
class base { derived ob(3);
int i, j; ob.set(1, 2); // access member of base
public: ob.show(); // access member of base
void set(int a, int b){ ob.showk(); // uses member of derived class
i=a; j=b; } return 0;
void show() { }
cout << i << " " << j << "\n"; }
};
class derived : public base {
int k;
public:
derived(int x) {
k=x; }
void showk() {
cout << k << "\n"; }
};
26
// This program won't compile. public:
#include <iostream.h> derived(int x) { k=x; }
using namespace std; void showk()
class base { { cout << k << "\n"; }
int i, j; };
public: int main()
void set(int a, int b) { i=a; j=b; } {
void show() { cout << i << " " << j << derived ob(3);
"\n";} ob.set(1, 2);
}; // error, can't access set()
// Public elements of base are private ob.show();
in derived. // error, can't access show()
class derived : private base { return 0;
int k; }
27
Multiple Inheritance
• Multiple Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from multiple base class(es).
28
Syntax
29
Inheriting Multiple Base Classes
#include <iostream> // Inherit multiple base classes.
using namespace std; class derived: public base1, public base2 {
class base1 { public:
protected: void set(int i, int j) { x=i; y=j; }
int x; };
public: int main()
void showx() { cout << x << "\n"; } {
}; derived ob;
class base2 { ob.set(10, 20);
protected: // provided by derived
int y; ob.showx(); // from base1
Public: ob.showy(); // from base2
void showy() {cout << y << "\n";} return 0;
}; }
30
//EXAMPLE // Derived class
#include <iostream.h> class Rectangle: public Shape, public PaintCost {
using namespace std;
public:
// Base class Shape
class Shape { int getArea() {
public: return (width * height);
void setWidth(int w) { } };
width = w; } int main(void) {
void setHeight(int h) {
Rectangle Rect;
height = h; }
protected: int area;
int width; Rect.setWidth(5);
int height; Rect.setHeight(7);
}; area = Rect.getArea();
// Base class PaintCost
// Print the total cost of painting
class PaintCost {
public: cout << "Total paint cost: $" << Rect.getCost(area)
int getCost(int area) { << endl;
return area * 70; return 0; }
}};
31
Multilevel Inheritance
• Multilevel Inheritance: It is the inheritance
hierarchy wherein subclass acts as a base class
for other classes.
32
Syntax
33
Multi-level Inheritance
#include <iostream> //derived2 class
using namespace std; class derived2 : public derived
//Base class { public:
class base { void display3(){
public: cout << "\n2nd Derived class content.";
void display1() { } };
cout << "\nBase class content."; } int main()
}; {
//derived class derived2 D;
class derived : public base D.display3();
{ D.display2();
public: D.display1();
void display2() return(0);
{ }
cout << "1st derived class content."; } };
34
Hierarchical Inheritance
• Hierarchical Inheritance: It is the inheritance
hierarchy wherein multiple subclasses inherit
from one base class.
35
Hierarchical Inheritance
36
Hierarchical Inheritance
#include <iostream> void disp() {
#include <string.h> cout << "Age: " << age << endl; cout
using namespace std; << "Gender: " << gender << endl; }
//Base Class };
class member { //derived from member
char gender[10]; class stud : public member
int age; { char level[20];
public: public:
void get() void getdata() { member::get();
{ cout << "Age: "; cin >> age; cout << "Class: ";
cout << "Gender: "; cin >> gender; } cin >> level; }
Continue...
37
void disp2() void disp3() { member::disp();
{ member::disp(); cout << "Salary: Rs." << salary << endl;
cout << "Level: " << level; } };
} };
//staff class derived from member int main() {
class staff : public member //member M;
{ float salary; staff S;
public: stud s;
void getdata() { member::get(); s.getdata();
cout << "Salary: Rs."; s.disp();
cin >> salary; } S.getdata();
S.disp();
return(0); }
38
Hybrid Inheritance
• Hybrid Inheritance: The inheritance hierarchy
that reflects any legal combination of other
four types of inheritance.
39
Syntax
40
#include <iostream> class D : public B, public C
using namespace std; //D is derived from class B and class C
class A { { public:
public: int x; void sum()
}; { cout << "Sum= " << x + y; }
class B : public A { };
public: B() //constructor to initialize x int main()
in base class A { D obj1; //object of derived class D
{ x = 10; } obj1.sum();
}; return 0;
class C { }
public: int y; C() //constructor to initialize y Output
{ y = 4; }
};
41
9/11/2024 UTA009 Inheritance 42
Thanks
43