Download presentation
Presentation is loading. Please wait.
Published byElena Fifield Modified over 9 years ago
1
Inheritance zThe mechanism by which one class can inherit the properties of another. zIt allows a hierarchy of classes to be built, moving from the most general to the most specific.
2
Base Class, Derived Class zBase Class yDefines all qualities common to any derived classes. zDerived Class yInherits those general properties and adds new properties that are specific to that class.
3
Example: Base Class class base { int x; public: void setx(int n) { x = n; } void showx() { cout << x << ‘\n’ } };
4
Example: Derived Class // Inherit as public class derived : public base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘\n’;} };
5
Access Specifier: public zThe keyword public tells the compiler that base will be inherited such that: yall public members of the base class will also be public members of derived. zHowever, all private elements of base will remain private to it and are not directly accessible by derived.
6
Example: main() int main() { derived ob; ob.setx(10); ob.sety(20); ob.showx(); ob.showy(); }
7
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; } };
8
Access Specifier: private zIf the access specifier is private: ypublic members of base become private members of derived. ythese members are still accessible by member functions of derived.
9
Example: Derived Class // Inherit as private class derived : private base { int y; public: void sety(int n) { y = n; } void showy() { cout << y << ‘\n’;} };
10
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! }
11
Example: Derived Class class derived : private base { int y; public: // setx is accessible from within derived void setxy(int n, int m) { setx(n); y = m; } // showx is also accessible void showxy() { showx(); cout<<y<< ‘\n’;} };
12
Protected Members zSometimes you want to do the following: ykeep a member of a base class private yallow a derived class access to it zUse protected members! zIf no derived class, protected members is the same as private members.
13
Protected Members The full general form of a class declaration: class class-name { // private members protected: // protected members public: // public members };
14
3 Types of Access Specifiers zType 1: inherit as private BaseDerived private membersinaccessible protected membersprivate members public membersprivate members
15
3 Types of Access Specifiers zType 2: inherit as protected BaseDerived private membersinaccessible protected members public membersprotected members
16
3 Types of Access Specifiers zType 3: inherit as public BaseDerived private membersinaccessible protected members public members
17
Constructor and Destructor zIt is possible for both the base class and the derived class to have constructor and/or destructor functions. zThe constructor functions are executed in order of derivation. yi.e.the base class constructor is executed first. zThe destructor functions are executed in reverse order.
18
Passing arguments zWhat 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.
19
Example: Constructor of base class base { int i; public: base(int n) { cout << “constructing base \n”; i = n; } ~base() { cout << “destructing base \n”; } };
20
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”;} };
21
Example: main() int main() { derived o(10,20); return 0; } constructing base constructing derived destructing derived destructing base
22
Multiple Inheritance zType 1: base 1 derived 1 derived 2
23
Multiple Inheritance Type 2: base 1base 2 derived
24
Example: Type 2 // Create first base class class B1 { int a; public: B1(int x) { a = x; } int geta() { return a; } };
25
Example: Type 2 // Create second base class class B2 { int b; public: B2(int x) { b = x; } int getb() { return b; } };
26
// 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
27
Potential Problem zBase is inherited twice by Derived 3! Base Derived 1 Derived 2 Derived 3
28
Virtual Base Class zTo resolve this problem, virtual base class can be used. class base { public: int i; };
29
Virtual Base Class // Inherit base as virtual class D1 : virtual public base { public: int j; }; class D2 : virtual public base { public: int k; };
30
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; } };
31
Pointers to Derived Classes zA pointer declared as a pointer to base class can also be used to point to any class derived from that base. zHowever, only those members of the derived object that were inherited from the base can be accessed.
32
Example base *p; // base class pointer base B_obj; derived D_obj; p = &B_obj; // p can point to base object p = &D_obj; // p can also point to derived // object
33
Virtual Function zA virtual function is a member function ydeclared within a base class yredefined by a derived class (i.e. overriding) zIt can be used to support run-time polymorphism.
34
Example class base { public: int i; base (int x) { i = x; } virtual void func() {cout << i; } };
35
Example class derived : public base { public: derived (int x) : base (x) {} // The keyword virtual is not needed. void func() {cout << i * i; } };
36
Example int 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() }
37
Pure Virtual Functions zA pure virtual function has no definition relative to the base class. zOnly the function’s prototype is included. zGeneral form: virtual type func-name(paremeter-list) = 0
38
Example: area class area { public: double dim1, dim2; area(double x, double y) {dim1 = x; dim2 = y;} // pure virtual function virtual double getarea() = 0; };
39
Example: rectangle class rectangle : public area { public: // function overriding double getarea() { return dim1 * dim2; } };
40
Example: triangle class triangle : public area { public: // function overriding double getarea() { return 0.5 * dim1 * dim2; } };
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.