Inheritance Concept class Rectangle Polygon Rectangle private int

  • Slides: 28
Download presentation
Inheritance Concept class Rectangle{ Polygon Rectangle private: int num. Vertices; float *x. Coord, *y.

Inheritance Concept class Rectangle{ Polygon Rectangle private: int num. Vertices; float *x. Coord, *y. Coord; public: void set(float *x, float *y, int n. V); float area(); Triangle }; class Polygon{ private: int num. Vertices; float *x. Coord, *y. Coord; public: void set(float *x, float *y, int n. V); }; class Triangle{ private: int num. Vertices; float *x. Coord, *y. Coord; public: void set(float *x, float *y, int n. V); float area(); 1 };

Inheritance Concept Polygon Rectangle Triangle class Rectangle : public Polygon{ public: float area(); };

Inheritance Concept Polygon Rectangle Triangle class Rectangle : public Polygon{ public: float area(); }; class Polygon{ protected: int num. Vertices; float *x. Coord, float *y. Coord; public: void set(float *x, float *y, int n. V); }; class Rectangle{ protected: int num. Vertices; float *x. Coord, float *y. Coord; public: void set(float *x, float *y, int n. V); float area(); 2 };

Inheritance Concept Polygon Rectangle Triangle class Triangle : public Polygon{ public: float area(); };

Inheritance Concept Polygon Rectangle Triangle class Triangle : public Polygon{ public: float area(); }; class Polygon{ protected: int num. Vertices; float *x. Coord, float *y. Coord; public: void set(float *x, float *y, int n. V); }; class Triangle{ protected: int num. Vertices; float *x. Coord, float *y. Coord; public: void set(float *x, float *y, int n. V); float area(); 3 };

Inheritance Concept x y Point Circle 3 D-Point x y r x y z

Inheritance Concept x y Point Circle 3 D-Point x y r x y z class Circle : public Point{ private: double r; }; class Point{ protected: int x, y; public: void set (int a, int b); }; class 3 D-Point: public Point{ private: int z; }; 4

Inheritance Concept • Augmenting the original class Polygon Rectangle Point Circle Triangle 3 D-Point

Inheritance Concept • Augmenting the original class Polygon Rectangle Point Circle Triangle 3 D-Point • Specializing the original class Complex. Number real Real. Number real imag Imaginary. Number imag 5

Why Inheritance ? Inheritance is a mechanism for • building class types from existing

Why Inheritance ? Inheritance is a mechanism for • building class types from existing class types • defining new class types to be a – specialization – augmentation of existing types 6

Define a Class Hierarchy • Syntax: class Derived. Class. Name : access-level Base. Class.

Define a Class Hierarchy • Syntax: class Derived. Class. Name : access-level Base. Class. Name where – access-level specifies the type of derivation • private by default, or • public • Any class can serve as a base class – Thus a derived class can also be a base class 7

Class Derivation Point 3 D-Point Sphere class 3 D-Point : public Point{ private: double

Class Derivation Point 3 D-Point Sphere class 3 D-Point : public Point{ private: double z; …… }; class Point{ protected: int x, y; public: void set (int a, int b); }; class Sphere : public 3 D-Point{ private: double r; …… }; Point is the base class of 3 D-Point, while 3 D-Point is the base class of Sphere 8

What to inherit? • In principle, every member of a base class is inherited

What to inherit? • In principle, every member of a base class is inherited by a derived class – just with different access permission 9

Access Control Over the Members • Two levels of access control over class members

Access Control Over the Members • Two levels of access control over class members – class definition – inheritance type class Point{ protected: int x, y; public: void set(int a, int b); }; class Circle : public Point{ …… }; 10

Access Rights of Derived Classes Type of Inheritance Access Control for Members private -

Access Rights of Derived Classes Type of Inheritance Access Control for Members private - protected - public - protected public private protected public • The type of inheritance defines the access level for the members of derived class that are inherited from the base class 11

Class Derivation class mother{ protected: int m. Proc; public: int m. Publ; private: int

Class Derivation class mother{ protected: int m. Proc; public: int m. Publ; private: int m. Priv; }; private/protected/public class daughter : ----- mother{ private: double d. Priv; public: void d. Foo m. Foo((); ); }; void daughter : : d. Foo ( ){ m. Priv = 10; //error m. Proc = 20; }; class grand. Daughter : public daughter { private: double g. Priv; public: void g. Foo ( ); }; int main() { /*…. */ } 12

What to inherit? • In principle, every member of a base class is inherited

What to inherit? • In principle, every member of a base class is inherited by a derived class – just with different access permission • However, there are exceptions for – constructor and destructor – operator=() member – friends Since all these functions are class-specific 13

Constructor Rules for Derived Classes The default constructor and the destructor of the base

Constructor Rules for Derived Classes The default constructor and the destructor of the base class are always called when a new object of a derived class is created or destroyed. class A { public: A() {cout<< “A: default”<<endl; } A (int a) {cout<<“A: parameter”<<endl; } }; class B : public A { public: B (int a) {cout<<“B”<<endl; } }; output: B test(1); A: default B 14

Constructor Rules for Derived Classes You can also specify an constructor of the base

Constructor Rules for Derived Classes You can also specify an constructor of the base class other than the default constructor Derived. Class. Con ( derived. Class args ) : Base. Class. Con ( base. Class args ) { Derived. Class constructor body } class A { public: A() {cout<< “A: default”<<endl; } A (int a) {cout<<“A: parameter”<<endl; } }; class C : public A { public: C (int a) : A(a) {cout<<“C”<<endl; } }; output: C test(1); A: parameter C 15

Define its Own Members The derived class can also define its own members, in

Define its Own Members The derived class can also define its own members, in addition to the members inherited from the base class Point x y r x y Circle class Circle : public Point{ private: double r; public: void set_r(double c); }; class Point{ protected: int x, y; public: void set(int a, int b); }; class Circle{ protected: int x, y; private: double r; public: void set(int a, int b); void set_r(double c); }; 16

Even more … • A derived class can override methods defined in its parent

Even more … • A derived class can override methods defined in its parent class. With overriding, – the method in the subclass has the identical signature to the method in the base class. – a subclass implements its own version of a base class method. class A { protected: int x, y; public: void print () {cout<<“From A”<<endl; } }; class B : public A { public: void print () {cout<<“From B”<<endl; } }; 17

Access a Method class Point{ protected: int x, y; public: void set(int a, int

Access a Method class Point{ protected: int x, y; public: void set(int a, int b) {x=a; y=b; } void foo (); void print(); }; Point A; A. set(30, 50); // from base class Point A. print(); // from base class Point class Circle : public Point{ private: double r; public: void set (int a, int b, double c) { Point : : set(a, b); //same name function call r = c; } void print(); }; Circle C; C. set(10, 100); // from class Circle C. foo (); // from base class Point C. print(); // from class Circle 18

Putting Them Together Time Ext. Time • Time is the base class • Ext.

Putting Them Together Time Ext. Time • Time is the base class • Ext. Time is the derived class with public inheritance • The derived class can – inherit all members from the base class, except the constructor – access all public and protected members of the base class – define its private data member – provide its own constructor – define its public member functions – override functions inherited from the base class 19

class Time Specification // SPECIFICATION FILE ( time. h) class Time{ public : void

class Time Specification // SPECIFICATION FILE ( time. h) class Time{ public : void Set ( int h, int m, int s ) ; void Increment ( ) ; void Write ( ) const ; Time ( int init. H, int init. M, int init. S ) ; // constructor Time ( ) ; // default constructor protected : int int hrs ; mins ; secs ; }; 20

Class Interface Diagram Time class Set Increment Protected data: hrs Write Time mins secs

Class Interface Diagram Time class Set Increment Protected data: hrs Write Time mins secs Time 21

Derived Class Ext. Time // SPECIFICATION FILE ( exttime. h) #include “time. h” enum

Derived Class Ext. Time // SPECIFICATION FILE ( exttime. h) #include “time. h” enum Zone. Type {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ; class Ext. Time : public Time { // Time is the base class and use public inheritance public : void Set ( int h, int m, int s, Zone. Type time. Zone ) ; void Write ( ) const; //overridden Ext. Time (int init. H, int init. M, int init. S, Zone. Type init. Zone ) ; Ext. Time (); // default constructor private : Zone. Type zone ; }; // added data member 22

Class Interface Diagram Ext. Time class Set Increment Write Protected data: hrs mins Ext.

Class Interface Diagram Ext. Time class Set Increment Write Protected data: hrs mins Ext. Time secs Time Private data: zone 23

Implementation of Ext. Time Default Constructor Ext. Time : : Ext. Time ( )

Implementation of Ext. Time Default Constructor Ext. Time : : Ext. Time ( ) { zone = EST ; } The default constructor of base class, Time(), is automatically called, when an Ext. Time object is created. Ext. Time et 1; et 1 hrs = 0 mins = 0 secs = 0 zone = EST 24

Implementation of Ext. Time Another Constructor Ext. Time : : Ext. Time (int init.

Implementation of Ext. Time Another Constructor Ext. Time : : Ext. Time (int init. H, int init. M, int init. S, Zone. Type init. Zone) : Time (init. H, init. M, init. S) // constructor initializer { zone = init. Zone ; } Ext. Time *et 2 = new Ext. Time(8, 30, 0, EST); et 2 6000 5000 ? ? ? 5000 hrs = 8 mins = 30 secs = 0 zone = EST 25

Implementation of Ext. Time void Ext. Time : : Set (int h, int m,

Implementation of Ext. Time void Ext. Time : : Set (int h, int m, int s, Zone. Type time. Zone) { Time : : Set (hours, minutes, seconds); // same name function call zone = time. Zone ; } void Ext. Time : : Write ( ) const // function overriding { string zone. String[8] = {“EST”, “CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”} ; Time : : Write ( ) ; cout <<‘ ‘<<zone. String[zone]<<endl; } 26

Working with Ext. Time #include “exttime. h” …… int main() { Ext. Time this.

Working with Ext. Time #include “exttime. h” …… int main() { Ext. Time this. Time ( 8, 35, 0, PST ) ; that. Time ; // default constructor called that. Time. Write( ) ; // outputs 00: 00 EST that. Time. Set (16, 49, 23, CDT) ; that. Time. Write( ) ; // outputs 16: 49: 23 CDT this. Time. Increment ( ) ; this. Time. Write ( ) ; // outputs 08: 35: 02 PST } 27

Take Home Message • Inheritance is a mechanism for defining new class types to

Take Home Message • Inheritance is a mechanism for defining new class types to be a specialization or an augmentation of existing types. • In principle, every member of a base class is inherited by a derived class with different access permissions, except for the constructors 28