inheritance
DESCRIPTION
Inheritance. The mechanism by which one class can inherit the properties of another. It allows a hierarchy of classes to be built, moving from the most general to the most specific. Base Class, Derived Class. Base Class Defines all qualities common to any derived classes. Derived Class - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/1.jpg)
InheritanceThe mechanism by which one class
can inherit the properties of another.
It allows a hierarchy of classes to be built, moving from the most general to the most specific.
![Page 2: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/2.jpg)
Base Class, Derived ClassBase Class
Defines all qualities common to any derived classes.
Derived Class Inherits those general properties and
adds new properties that are specific to that class.
![Page 3: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/3.jpg)
Example: Base Class
class base {int x;
public:void setx(int n) { x = n; }void showx() { cout << x << ‘\n’ }
};
![Page 4: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/4.jpg)
Example: Derived Class
// Inherit as publicclass derived : public base {
int y;public:
void sety(int n) { y = n; }void showy() { cout << y << ‘\n’;}
};
![Page 5: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/5.jpg)
Access Specifier: publicThe keyword public tells the compiler
that base will be inherited such that: all public members of the base class
will also be public members of derived.
However, all private elements of base will remain private to it and are not directly accessible by derived.
![Page 6: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/6.jpg)
Example: main()
int main() {derived ob;ob.setx(10);ob.sety(20);ob.showx();ob.showy();
}
![Page 7: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/7.jpg)
An incorrect example
class derived : public base {int y;
public:void sety(int n) { y = n; }/* Error ! Cannot access x, which is private member of base. */void show_sum() {cout << x+y; }
};
![Page 8: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/8.jpg)
Access Specifier: privateIf the access specifier is private:
public members of base become private members of derived. these members are still accessible by member functions of derived.
![Page 9: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/9.jpg)
Example: Derived Class
// Inherit as privateclass derived : private base {
int y;public:
void sety(int n) { y = n; }void showy() { cout << y << ‘\n’;}
};
![Page 10: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/10.jpg)
Example: main()
int main() {derived ob;ob.setx(10); // Error! setx() is private.ob.sety(20); // OK!ob.showx(); // Error! showx() is private.ob.showy(); // OK!
}
![Page 11: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/11.jpg)
Example: Derived Classclass derived : private base {int y;public:// setx is accessible from within derivedvoid setxy(int n, int m) { setx(n); y = m; }// showx is also accessiblevoid showxy() { showx(); cout<<y<< ‘\n’;}};
![Page 12: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/12.jpg)
Protected MembersSometimes you want to do the following:
keep a member of a base class private allow a derived class access to it
Use protected members!If no derived class, protected
members is the same as private members.
![Page 13: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/13.jpg)
Protected Members
The full general form of a class declaration:class class-name {// private membersprotected:// protected memberspublic:// public members};
![Page 14: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/14.jpg)
3 Types of Access SpecifiersType 1: inherit as private
Base Derived
private members inaccessible
protected members private members
public members private members
![Page 15: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/15.jpg)
3 Types of Access SpecifiersType 2: inherit as protected
Base Derived
private members inaccessible
protected members protected members
public members protected members
![Page 16: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/16.jpg)
3 Types of Access SpecifiersType 3: inherit as public
Base Derived
private members inaccessible
protected members protected members
public members public members
![Page 17: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/17.jpg)
Constructor and DestructorIt is possible for both the base class
and the derived class to have constructor and/or destructor functions.
The constructor functions are executed in order of derivation. i.e.the base class constructor is executed
first.The destructor functions are executed
in reverse order.
![Page 18: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/18.jpg)
Passing arguments What if the constructor functions of
both the base class and derived class take arguments?
1. Pass all necessary arguments to the derived class’s constructor.
2. Then pass the appropriate arguments along to the base class.
![Page 19: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/19.jpg)
Example: Constructor of baseclass base {
int i;public:
base(int n) {cout << “constructing base \n”; i = n; }~base() { cout << “destructing base \n”; }
};
![Page 20: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/20.jpg)
Example: Constructor of derived
class derived : public base {int j;
public:derived (int n, int m) : base (m) {cout << “constructing derived\n”;j = n; }~derived() { cout << “destructing derived\n”;}
};
![Page 21: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/21.jpg)
Example: main()int main() {
derived o(10,20);return 0;
}constructing baseconstructing deriveddestructing deriveddestructing base
![Page 22: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/22.jpg)
Multiple InheritanceType 1:
base 1
derived 1
derived 2
![Page 23: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/23.jpg)
Multiple Inheritance
• Type 2:
base 1 base 2
derived
![Page 24: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/24.jpg)
Example: Type 2
// Create first base classclass B1 {
int a;public:
B1(int x) { a = x; }int geta() { return a; }
};
![Page 25: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/25.jpg)
Example: Type 2
// Create second base classclass B2 {
int b;public:
B2(int x) { b = x; }int getb() { return b; }
};
![Page 26: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/26.jpg)
// Directly inherit two base classes.class D : public B1, public B2 {int c;public:D(int x, int y, int z) : B1(z), B2(y) {c = x; }void show() {cout << geta() << getb() << c;}} ;
Example: Type 2
![Page 27: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/27.jpg)
Potential Problem
Base is inherited twice by Derived 3!
Base Base
Derived 1 Derived 2
Derived 3
![Page 28: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/28.jpg)
Virtual Base ClassTo resolve this problem, virtual base class can be used.
class base {public:
int i;};
![Page 29: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/29.jpg)
Virtual Base Class// Inherit base as virtualclass D1 : virtual public base {public: int j; };class D2 : virtual public base {public: int k; };
![Page 30: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/30.jpg)
Virtual Base Class
/* Here, D3 inherits both D1 and D2. However, only one copy of base is present */class D3 : public D1, public D2 {public:
int product () { return i * j * k; }};
![Page 31: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/31.jpg)
Pointers to Derived ClassesA pointer declared as a pointer to
base class can also be used to point to any class derived from that base.
However, only those members of the derived object that were inherited from the base can be accessed.
![Page 32: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/32.jpg)
Example
base *p; // base class pointerbase B_obj;derived D_obj;p = &B_obj; // p can point to base object p = &D_obj; // p can also point to derived // object
![Page 33: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/33.jpg)
Virtual FunctionA virtual function is a member
function declared within a base class redefined by a derived class (i.e.
overriding)
It can be used to support run-time polymorphism.
![Page 34: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/34.jpg)
Example
class base {public:
int i;base (int x) { i = x; }virtual void func() {cout << i; }
};
![Page 35: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/35.jpg)
Example
class derived : public base {public:
derived (int x) : base (x) {}// The keyword virtual is not needed.void func() {cout << i * i; }
};
![Page 36: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/36.jpg)
Exampleint main() {base ob(10), *p;derived d_ob(10);
p = &ob;p->func(); // use base’s func()p = &d_ob;p->func(); // use derived’s func()}
![Page 37: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/37.jpg)
Pure Virtual FunctionsA pure virtual function has no definition relative to the base class. Only the function’s prototype is included.General form:
virtual type func-name(paremeter-list) = 0
![Page 38: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/38.jpg)
Example: area
class area {public:
double dim1, dim2; area(double x, double y) {dim1 = x; dim2 = y;}// pure virtual functionvirtual double getarea() = 0;
};
![Page 39: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/39.jpg)
Example: rectangle
class rectangle : public area {public:// function overriding
double getarea() {return dim1 * dim2;
}};
![Page 40: Inheritance](https://reader035.vdocuments.site/reader035/viewer/2022062316/5681683a550346895dde02e3/html5/thumbnails/40.jpg)
Example: triangle
class triangle : public area {public:// function overriding
double getarea() {return 0.5 * dim1 * dim2;
}};