Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Composition Reusing the code and functionality Unit - 04.

Similar presentations


Presentation on theme: "Inheritance and Composition Reusing the code and functionality Unit - 04."— Presentation transcript:

1 Inheritance and Composition Reusing the code and functionality Unit - 04

2 Unit Introduction This unit covers composition and inheritance

3 Unit Objectives After covering this unit you will understand… Composition Composition Inheritance Inheritance Inheritance & constructor call sequence Inheritance & constructor call sequence Multiple inheritance Multiple inheritance Upcasting & inheritance Upcasting & inheritance C++ casting operators C++ casting operators Different types of Inheritance Different types of Inheritance Inheritance vs. composition Inheritance vs. composition

4 Composition (Aggregation) Creating and using objects of other class within a different class Creating and using objects of other class within a different class Also known as embedded object (or sub-object) Also known as embedded object (or sub-object)

5 Example: Composition class X { private: private: int m_Data; // data member int m_Data; // data member public: public: X() {} // default constructor SetX (int k) // member function { m_Data = k; m_Data = k;}}; class Y { private: private: int m_Data;

6 Example: Composition (contd.) public: public: X x; // composition X x; // composition Y() {} //default constructor }; void main() { Y y; // creating object of class Y y.x.SetX(20); // Access/sets the embedded object }

7 Inheritance Parent class is called the base class Parent class is called the base class The class which inherits the base class is called the derived class; and has all the features of the base class, plus its own features The class which inherits the base class is called the derived class; and has all the features of the base class, plus its own features

8 Example: Inheritance class X { int m_Data; int m_Data; public: public: X() {} X() {}}; class Y : public X // class Y publicly inherits class X { int m_Data; int m_Data; public: public: Y() {} Y() {}};

9 Constructor and Inheritance The constructor and destructor calls are automatic for the base class, in case the derived class object is created The constructor and destructor calls are automatic for the base class, in case the derived class object is created The order of base and derived class constructors and destructors are: The order of base and derived class constructors and destructors are: base constructor base constructor derived constructor derived constructor derived destructor derived destructor base destructor base destructor

10 Multiple Inheritance There could be two or more base class for a derived class There could be two or more base class for a derived class It can cause ambiguity and is not recommended in normal cases It can cause ambiguity and is not recommended in normal cases There are ways to have single inheritance used instead of multiple inheritance to achieve the goal There are ways to have single inheritance used instead of multiple inheritance to achieve the goal class Z : public X, public Y { // class definition }

11 Example: Multiple Inheritance class A { public: public: void SomeFunction(void) void SomeFunction(void){ // function code }}; class B { public: public: void SomeFunction(void) void SomeFunction(void){ // function code }};

12 Example: Multiple Inheritance (contd.) class C : public A, public B { public: public: void MyFunction(void) void MyFunction(void){ SomeFunction(); // Ambiguous as the compiler // does not know whether to //choose SomeFunction() of A or B }};

13 Upcasting and Downcasting Casting from derived to base class is known as Upcasting (implicit) Casting from derived to base class is known as Upcasting (implicit) Upcasting lose the derived class properties Upcasting lose the derived class properties Casting from base to derived class is known as Downcasting Casting from base to derived class is known as Downcasting Downcasting makes derived class’s properties available Downcasting makes derived class’s properties available

14 Example: Upcasting and Downcasting class Person {private: int Age; char* Name; char Gender; public: // accessor methods for member variables }; class Employee : public Person {private: char* Department; int EmployeeCode; public: // accessor methods for member variables // accessor methods for member variables};

15 Example: Upcasting and Downcasting (contd.) void main() { Person* pPerson = new Employee();// upcast, implicit pPerson->GetName();// fine pPerson->GetDepartment();// error Employee* pEmployee = (Employee*)pPerson;// downcast pEmployee->GetDepartment();// fine now }

16 C++ Casting Operators Replace the traditional, parenthetic casting technique Replace the traditional, parenthetic casting technique Explicit Explicit Self documenting Self documenting Signal a type conversion Signal a type conversion Less risk of illegal cast and data corruption Less risk of illegal cast and data corruption

17 C++ Casting Operators (contd.) static_cast static_cast Compile-time cast Compile-time cast Casting classes that provide static polymorphism (non-virtual classes) Casting classes that provide static polymorphism (non-virtual classes) dynamic_cast dynamic_cast Run-time cast, to cast classes that provide dynamic polymorphism (virtual classes) Run-time cast, to cast classes that provide dynamic polymorphism (virtual classes) const_cast const_cast To remove const-ness To remove const-ness reinterpret_cast reinterpret_cast For conversion of unrelated types For conversion of unrelated types

18 Example: static_cast // Note that Base is not a virtual class class Base{ }; class Derived : public Base { }; void f(Base* pBase) { Derived* pDerived = static_cast pBase } // Also, enum fruit{apple=0, orange=1, banana=2}; int x = 2; fruit f1 = static_cast (x); // converts an int to enum

19 Example: dynamic_cast // Note that Base is a virtual class class Base{ virtual void f(); }; class Derived : public Base { void f (){} }; void f(Base* pBase) { //Derived* pDerived = static_cast pBase // error // use dynamic_cast instead Derived* pDerived = dynamic_cast pBase // fine }

20 Example: const_cast double f(double& d);// f accepts reference to type double void g(const double& d) { // while calling f() the const-ness of d is removed double val = f( const_cast (d) ); }

21 Example: reinterpret_cast // a practical example could returns hash code based on an // address unsigned short Hash(void* p) { // reinterpret casting a pointer to integral value // reinterpret casting a pointer to integral value unsigned int val = reinterpret_cast (p); unsigned int val = reinterpret_cast (p); val = val & 16; // hash code formula val = val & 16; // hash code formula return (unsigned short)val; // cast to the returned type return (unsigned short)val; // cast to the returned type} void main() { int a[10]; int a[10]; for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++) { cout << Hash(a + i) << endl;// using Hash() cout << Hash(a + i) << endl;// using Hash() }}

22 Inheritance Types Inheritance has three types: Inheritance has three types: public public private private protected protected The following chart gives the accessibility details of the inheritance types: The following chart gives the accessibility details of the inheritance types:

23 Inheritance Types (contd.) Protected Function in the base class is treated as Private Function in the child class (taking the example of the value in bold), when inherited using the private keyword Protected Function in the base class is treated as Private Function in the child class (taking the example of the value in bold), when inherited using the private keyword class X : public Y, private Z, protected W { // class definition }

24 Inheritance vs. Composition Composition is generally used when you want the features of an existing class inside your new class, but not its interface Composition is generally used when you want the features of an existing class inside your new class, but not its interface Use inheritance for sub-typing, where you want your new type to have exactly the same interface as the existing type (plus any other member functions you want to add) Use inheritance for sub-typing, where you want your new type to have exactly the same interface as the existing type (plus any other member functions you want to add)

25 Unit Summary In this unit you have covered … Composition Composition Inheritance Inheritance Multiple inheritance Multiple inheritance Upcasting & inheritance Upcasting & inheritance C++ casting operators C++ casting operators Types of inheritance Types of inheritance Inheritance vs. composition Inheritance vs. composition


Download ppt "Inheritance and Composition Reusing the code and functionality Unit - 04."

Similar presentations


Ads by Google