1 / 24

Inheritance and Derived Classes

Inheritance and Derived Classes. Why do we need it?. It is one of the most powerful mechanisms offered by OOP. It enables potentially infinite reuse of resources. It can be used to enhance and improve the previous programs for the changing requirements.

mirra
Télécharger la présentation

Inheritance and Derived Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance and Derived Classes

  2. Why do we need it? • It is one of the most powerful mechanisms offered by OOP. • It enables potentially infinite reuse of resources. • It can be used to enhance and improve the previous programs for the changing requirements. • It also matches what we know about the real-world.

  3. An Example (from textbook) Consider the employees of a company. All of them have some common attributes, like SSN, name, employeeID, netPay, etc. But they may be paid differently. Some are paid hourly, while the other may be paid with a fixed wage (weekly/monthly) If we use two different classes to represent these two groups of employees who are paid differently, each of these classes will need to define those common attributes listed above. This is not very efficient in terms of coding. This situation can be more dramatic in the real cases of software development.

  4. An Example (from textbook) Consider the employees of a company. All of them have some common attributes, like SSN, name, employeeID, netPay, etc. But they may be paid differently. Some are paid hourly, while the other may be paid with a fixed wage (weekly/monthly) To address that, we use the inheritance!

  5. An Example (from textbook) We first define a class to implement the common properties and behaviors of all employees. #include <string> using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; };

  6. An Example (from textbook) Next, we define a new class to *inherit* these common attributes and behaviors. #include <string> using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; }; class HourlyEmployee : public Employee { public: // constructor list // new member functions private: double wageRate; double hours; };

  7. An Example (from textbook) Next, we define a new class to *inherit* these common attributes and behaviors. #include <string> using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; }; class HourlyEmployee : public Employee { public: // constructor list // new member functions private: double wageRate; double hours; }; derived class or child class In other words, we create a new class *based on* an existing class Base class or parent class

  8. How to use inheritance From this example, we see the inheritance is achieved in the following format class DerivedClass : public BaseClass { }; From this example, we can also learn that the base class typically provides a *general* definition (for all objects), while the individual child classes provide more *specific* definition (sub-sets of the objects).

  9. How to use inheritance A class DerivedClass : public BaseClass { }; B C D E …… This inheritance can be carried on during the development of the program, and eventually we obtain an inheritance relation of the classes much similar to a family tree. To represent this inheritance relation between classes and other relations including friend relationship, we will use UML!

  10. What benefits can inheritance provide? Derived class *inherits* all the public member variables and functions except the constructors of the base class. Derived class CANNOT access all the private member variables and functions of the base class.

  11. What benefits can inheritance provide? Derived class *inherits* all the public member variables and functions except the constructors of the base class. Derived class CANNOT access all the private member variables and functions of the base class. Derived class can define its own and new members that are for its specific purpose! A B So what do you think is the benefit here?

  12. What benefits can inheritance provide? Derived class *inherits* all the public member variables and functions except the constructors of the base class. Derived class DOES NOT inherits all the private member variables and functions of the base class. Derived class can define its own and new members that are for its specific purpose! A B So what do you think is the benefit here? Reduce the repeated work!

  13. More on Inheritance Derived class can *overwrite (or re-define)* the inherited member functions from the base class. This will NOT affect the previous definition in the base class. Derived class can NOT change the types of the inherited member variables. Again, derived class does NOT inherit the private members of the base class and its constructors. We typically use public access to inherit base class. “private” or “protected” inheritance is possible, but seldom used. class DerivedClass : publicBaseClass {} class DerivedClass : privateBaseClass {} All members of the base class are now private to the derived class!

  14. Constructor of Derived Class • class DerivedClass : public BaseClass • {… • public: • DerivedClass (argument list) • { • } • … • }; Or • class DerivedClass : public BaseClass • {… • public: • DerivedClass (argument list) : BaseClass (argument list) • { • } • … • }; In the initialization section

  15. The order of constructor calling In general, First, the constructor of the base class Second, the constructor of the object type of member variables Finally, the constructor of the derived class • class DerivedClass : public BaseClass • {… • public: • DerivedClass (argument list) • : BaseClass (argument list), A (initial value) • { • } • … • private: • A obj; //A is a pre-defined class! • };

  16. Redefinition • class BaseClass • { • public: • … • void method1(argument list){//implementation in base class} • }; • class DerivedClass : public BaseClass • {… • public: • void method1(same argument list!) • {//new implementation • } • void method1(different argument list) • {//new implementation • } • … • };

  17. Redefinition How to call the member function of the base class that are re-defined. • class BaseClass • { • public: • … • void method1(argument list){//implementation in base class} • }; • class DerivedClass : public BaseClass • {… • public: • void method1(same argument list!) • {//new implementation • } • … • }; • DerivedClass obj1; • obj.BaseClass::method1(input parameters);

  18. Multiple Inheritance • The new derived class can inherit from more than one base class class A { …… }; class B { …… }; Like single inheritance, the derived class will automatically inherit all the public members from all its parent classes. class C : public A, public B { …… };

  19. Multiple Inheritance • The new derived class can inherit from more than one base class class A { …… }; class B { …… }; Like single inheritance, the derived class will automatically inherit all the public members from all its parent classes. Be careful about the constructor class C : public A, public B { …… public: C (argument list) : A(arguments), B(arguments) {…} };

  20. Multiple Inheritance • Problem of multiple inheritance class A { …… public: intval; }; class B { …… public: intval; }; Ambiguity of inherited members! class C : public A, public B { …… }; C c_obj; c_obj.val=0; // which “val” from A or from B?

  21. Multiple Inheritance • Problem of multiple inheritance class A { …… public: intval; }; class B { …… public: intval; }; Solution – use scope resolution “::” class C : public A, public B { …… }; C c_obj; c_obj.A::val=0; // legal c_obj.B::val=2; // legal as well

  22. Overall, try to avoid multiple inheritance, as you may not know the ancestors of those base classes. The configuration to the right could happen! A A B C class A { public: void vf() { cout<<"I come from class A"<<endl; } }; class B: public A{}; class C: public A{}; class D: public B, public C{}; void main() { D d; d.vf (); // error, multiple copies of vf() } D

  23. There is indeed one possible solution to address that i.e. using virtual inheritance A A B C class A { public: void vf() { cout<<"I come from class A"<<endl; } }; class B: virtual public A{}; class C: virtual public A{}; class D: public B, public C{}; void main() { D d; d.vf (); // this OK now } D

  24. Another more complicated example class B1:virtual public B ,virtual public A{ public: B1(inti){ cout<<"Constructing B1"<<endl; } }; class B2: public A, virtual public B { public: B2(int j){ cout<<"Constructing B2"<<endl; } }; class D: public B1, public B2 { public: D(intm,int n): B1(m),B2(n){ cout<<"Constructing D"<<endl; } A a; }; void main(){ D d(1,2); }

More Related