Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in C++ Chapter 6 Inheritance.

Similar presentations


Presentation on theme: "Object Oriented Programming in C++ Chapter 6 Inheritance."— Presentation transcript:

1 Object Oriented Programming in C++ Chapter 6 Inheritance

2 Introduction In this section we will examine why we use inheritance base classes and derived classes protected members casting of derived class references to base class references casting of base class pointers to derived class pointers use of member functions redefinition of base class members public, protected and private base classes direct and indirect base class classes constructors and destructors in derived classes implicit conversion relationships multiple inheritance

3 Why do we use Inheritance? We use inheritance because it promotes software reuse by allowing the defining of a heirarchy of classes with successive refinement of behavior additional behavior restrictions on behaviour additional data members allows the construction of software by using existing class libraries and tailoring them to the current situation better supports the abstraction of the essentials of the problem

4 Base Classes and Derived Classes Derived classes inherit from their base class Derived classes obtain their own copies of all the base class’s data members and member functions (albeit with restrictions) Each instance of a derived class is also an instance of the base class (the inverse is not true) Derived classes can themselves become base classes, allowing successive refinement of an abstraction process and of the behavior

5 Inheritance syntax If we wished to implement this inheritance pattern, given the Student base class, we might write class Postgrad : public Student {... }; This is called public inheritance. Protected and private inheritance are also possible but are far less common. Post Grad Student Student

6 An Inheritance Hierarchy

7 Terminology Inheritance (Derivation) - Producing new classes by inheriting methods and data from an existing class or classes Superclass Base class Derived class Sub-class

8 Private and Protected Members When we use public inheritance Private members (either data or functions) of a base class are not accessible to derived classes, except through public member functions of the base class. Public members of a base class are accessible to the derived class. There is an intermediate category of access protection called protected. Protected members of the base class can only be accessed by members and friends of the base class and of any derived class. Protected data violates data encapsulation, in that changes to the base class protected members may require changes to all derived classes.

9 Protected Members not accessible Private Public Protected Accessible to derived classes only Derived Class

10 Constructors and Destructors in Derived Classes A base class initialiser can be called in the derived class’s constructor, if not the default constructor will be called. Base class constructors are called before the derived class constructor is called. Destructors are called in the reverse order of construction, i.e. derived class before base class.

11 Example class Buffer { int* ptr; int size; public: Buffer(int sz) { ptr = new int[ size=sz]; } int& operator[](int i) { return ptr[i]; } boolean inRange(int); ~Buffer() { delete [] ptr; } };

12 A Derived Class class Stack : public Buffer { int top; public: Stack(int sz) : Buffer(sz) { top = -1; } void push(int x); int pop( ); boolean empty( ); boolean full( ); };

13 Compatibility Between Base and Derived Classes An object of a derived class can be treated as an object of its base class. The reverse is not true.

14 Example: main() { Stack stk(30); int i, for( i=0; i<10; ++i) stk.push(i); fun1(stk); fun2(stk);... void fun1(Buffer bf) { for( int i=0; i<10; i++) cout << bf[i] << ‘ ‘ ; cout << ‘\n’ ; } void fun2(Stack st) { while( !st.empty() ) cout << st.pop() << ‘ ‘ ; cout << ‘\n’ ; }

15 Casting of References and Pointers It is possible to cast a reference to a derived class to that of the base class. This is common, as it allows you to get at the base classes functionality. It is also possible to cast a pointer of a derived class to that of the base class, and again this is common. It is also possible to cast a pointer of a base class to that of the derived class. This is perilous (but not illegal).

16 Example class Student { friend ostream &operator<< (ostream &, const Student &); friend istream &operator>> (istream &, Student &); public: Student(); private: char name[30]; char degree[15]; }; class Postgrad : public Student { friend ostream & operator<< (ostream &, const Postgrad &); friend istream & operator>> (istream &, Postgrad &); public: Postgrad(); private: char Supervisor[20]; char Thesis[20]; };

17 Student class I/O operators ostream &operator<< (ostream &output, const Student &s) { output << "Name : " << s.name << endl; output << "Degree : " << s.degree << endl; return output; } istream &operator>> (istream &input, Student &s) { input >> s.name >> s.degree; return input; }

18 Post Grad class I/O operators ostream &operator<< (ostream &output, const Postgrad &p) { output p; output << "Supervisor : " << p.Supervisor << endl; output << "Thesis Title : " << p.Thesis << endl; return output; } istream &operator>> (istream &input, Postgrad &p) { cout << "Please input Name then Degree : "; input >> static_cast p; cout << "Please input Supervisor and Title : " ; input >> p.Supervisor >> p.Thesis; return input; }

19 Test program main() { Student s, *s1; Postgrad p, *p1; cout << "Please input Name then Degree :"; cin >> s; cout << "You input " << s << endl; cout << "Postgraduate Student Entry\n"; cin >> p; cout << "You input\n" << p; p1 = static_cast &s; s1 = static_cast &p; cout << *s1; cout << *p1; return 0; } Casting of pointers, the first of the base class to the derived class, the next of the derived class to the base class

20 The Output Please input Name then Degree :a b You input Name : a Degree : b Postgraduate Student Entry Please input Name then Degree : c d Please input Supervisor and Title : e f You input Name : c Degree : d Supervisor : e Thesis Title : f Name : c Degree : d Name : a Degree : b Supervisor : -_n_ Thesis Title : orland C++ - Copyright 1991 Borland Intl.

21 Public, Protected and Private Base Classes private inheritance public inheritance protected inheritance Base Class Specifer public protected private public in derived class protected in derived class hidden hidden private in derived class hidden

22 Direct and Indirect Base Classes If a class inherits explicitly from a base class, then this is called a direct base class. If, however, a class inherits from 2 or more levels up the heirarchy, and not explicitly, this is called an indirect base class Person Indirect Base Class Employee Direct Base Class Manager Derived Class

23 What is and is not inherited Inherited Member functions Data members Not inherited Constructors & Destructors Friendship New data members or member functions may be added Inherited functions may be redefined Inherited members cannot be removed

24 Inheritance Benefits Software reusability Code sharing Consistency of interface Software components Rapid prototyping Polymorphism Information hiding Costs Execution speed Program size Message passing overhead Program complexity


Download ppt "Object Oriented Programming in C++ Chapter 6 Inheritance."

Similar presentations


Ads by Google