Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 44 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
44
Dung lượng
82,5 KB
Nội dung
Multiple Inheritance and Polymorphism Session Object Oriented Programming with C++/ Session / of 44 Session Objectives s Describe Multiple Inheritance • Constructors under Multiple Inheritance • Ambiguity in Multiple Inheritance • Multiple Inheritance with a Common Base s Describe Virtual Base Classes • Constructors and Destructors s Use Pointers to Objects to access Member Functions Object Oriented Programming with C++ / Session / of 44 Session Objectives(Contd.) s Describe Virtual functions s Describe Polymorphism s Describe Dynamic binding s Describe Pure Virtual Functions s Describe Abstract classes s Describe Virtual destructors Object Oriented Programming with C++ / Session / of 44 Multiple Inheritance s Multiple inheritance is the process of creating a new class from more than one base class • The derived class inherits the properties of two or more base classes s s Multiple inheritance can combine the behaviour of many base classes in a single class A multiple inheritance hierarchy represents a combination of its base classes Object Oriented Programming with C++ / Session / of 44 Multiple Inheritance (Contd.) Base Teacher class Student Teaching assistant Base class Derived class Object Oriented Programming with C++ / Session / of 44 Multiple Inheritance (Contd.) s The syntax for multiple inheritance is similar to that for single inheritance class Teacher { }; class Student { }; class Teach_asst: public Teacher, public Student • The names of both the base classes are provided separated by a comma • The rules of inheritance and access for multiple inheritance are the same as for single inheritance Object Oriented Programming with C++ / Session / of 44 Constructors class Teacher{ private: int x; public: Teacher(){x =0;} Teacher(int s){x = }; class Student{ private: int y; public: Student(){y = 0;} Student(int a){y = }; //constructors s;} //constructor a;} Object Oriented Programming with C++ / Session / of 44 Constructors (Contd.) class Teach_asst: public Teacher,public Student { private: int z; public: Teach_asst():Teacher(),Student() //constructor {z = 0;} Teach_asst(int s,int a,int b): Teacher(s),Student(a) {z = b;} }; Object Oriented Programming with C++ / Session / of 44 Constructors (Contd.) s s Constructors have to be defined to initialise the data members of all classes The names of the base class constructor follow the colon and are separated by commas: Teach_asst():Teacher(),Student(); s If the constructors had arguments then: Teach_asst(int s, arg for Teacher class int a, arg for Student class int b): arg for this class Teacher(s), call Teacher constructor Student(a) call Student constructor {z = b;} set own data member Object Oriented Programming with C++ / Session / of 44 Constructors and Destructors s General order for calling Constructors: • Base classes as they appear in the list of base classes: Teacher, Student • If there are member objects in the class, they are initialised next, in the order they appear in the derived class declaration • The object itself (using the code in its constructor) s General order for calling Destructors: • The destructor of the class is called first, then those of member objects, and then the base classes Object Oriented Programming with C++ / Session / 10 of 44 Points to be noted Virtual function should be declared in the base class and cannot be redeclared in a derived class s Return type of a member function must follow the keyword virtual s If a hierarchy of derived classes is used, the virtual function has to be declared in the top-most level s Object Oriented Programming with C++ / Session / 30 of 44 Points to be noted (Contd.) A virtual function must be defined for the class in which it was first declared s Redefined function in the derived class must have the same parameters (same number and data type), otherwise the compiler thinks you want to overload the virtual function The return type does not have to match s Object Oriented Programming with C++ / Session / 31 of 44 Polymorphism s s s s "poly" means "many" in Greek and "morphism" means "form" "polymorphism" means "many forms" It is the process of defining a number of objects of different classes in a group and using different function calls to carry out the operations of the objects It means, "to carry out different processing steps by functions having the same messages" Object Oriented Programming with C++ / Session / 32 of 44 Polymorphism (Contd.) • For example: Class mammals: Different mammals have different responses to the function EAT • The object will perform the action that is most appropriate for it • Objects are polymorphic if they have some similarities but are still somewhat different s The facility to invoke an appropriate function from any of the given classes using the pointer of the base class is a form of polymorphism E.g Using virtual functions Object Oriented Programming with C++ / Session / 33 of 44 Dynamic binding s s s Non-virtual member functions are selected statically (at compile-time) based on the type of the pointer (or reference) to the object Virtual member functions are resolved dynamically (at run-time) i.e., member function is selected at run-time based on the type of the object, not the type of the pointer/reference to that object Dynamic binding : The address of the code in a member function invocation is determined at the last possible moment, based on the dynamic type of the object at run time Object Oriented Programming with C++ / Session / 34 of 44 Dynamic binding (Contd.) Requires some overhead in processing but provides increased power and flexibility in programming s Dynamic binding is a result of virtual functions s If the keyword virtual is used with the function declaration in the base class, the system will use dynamic binding else static binding will be used s Object Oriented Programming with C++ / Session / 35 of 44 Pure Virtual functions s Some classes such as class Shapes, represent abstract concepts for which objects cannot exist • It is not possible to provide concrete definitions for its virtual functions that will actually create a Shape object s s s Alternative: Declare the virtual function of the class Shape as a pure virtual function A pure virtual function has only a function declaration It is declared by assigning the value of zero to the function as in, virtual void getdata() = 0; Object Oriented Programming with C++ / Session / 36 of 44 Pure Virtual functions (Contd) s s s Every derived class must include a function definition for each pure virtual function that is inherited from the base class if it has to be used to create an object This assures that there will be a function available for each call Cannot create an object of any class, which contains one or more pure virtual functions, because there is nothing to answer if a function call is sent to the pure virtual method Object Oriented Programming with C++ / Session / 37 of 44 Abstract Classes A class containing one or more pure virtual functions cannot be used to define an object-called an abstract class s Only useful as a base class to be inherited into a useable derived class s No objects of an abstract class can be created s Abstract class can only be used as a base for another class s Object Oriented Programming with C++ / Session / 38 of 44 Abstract Classes - Example class Shapes{ public: virtual void draw() = 0; //pure virtual function virtual void rotate(int) = 0; //pure virtual function }; class circle: public Shapes{ private: int radius; public: circle(int r); void draw(); void rotate(int){ } }; Object Oriented Programming with C++ / Session / 39 of 44 Abstract Classes(Contd.) s If a class inherits an abstract class without providing a definition for the pure virtual function, then it too becomes an abstract class and cannot be used to define an object • Important use of abstract classes is to provide an interface without exposing any implementation details • Used in many commercially available libraries and in application frameworks Object Oriented Programming with C++ / Session / 40 of 44 Virtual destructors Destructor of derived class is not invoked to free the memory storage that was allocated by the constructor of the derived class s This is because destructors are non-virtual and the message will not reach the destructors under dynamic binding s Better to have a virtual destructor function to free memory space effectively under dynamic binding s Object Oriented Programming with C++ / Session / 41 of 44 Example class Alpha{ private: char* alpha_ptr; public: Alpha() //constructor cannot be virtual {alpha_ptr = new char[5];} virtual void fn(); //virtual member function virtual ~Alpha() //virtual destructor {delete[] alpha_ptr;} }; Object Oriented Programming with C++ / Session / 42 of 44 Example (Contd.) class Beta: public Alpha{ private: char* ptrderived; public: Beta() {ptrderived = new char[100];} ~Beta() {delete[] ptrderived;} }; void main() { Alpha *ptr = new Beta; delete ptr; } Object Oriented Programming with C++ / Session / 43 of 44 Virtual destructors (Contd.) s Virtual functions bind to the code associated with the class of the object, rather than with the class of the pointer/reference • When you say delete ptr, and the base class has a virtual destructor, the destructor that gets invoked is the one associated with the type of the object *ptr, rather than the one associated with the type of the pointer s As a derived class instance always contains a base class instance, it is necessary to invoke destructors of both the classes in order to ensure that all the space on the heap is released Object Oriented Programming with C++ / Session / 44 of 44 .. .Session Objectives s Describe Multiple Inheritance • Constructors under Multiple Inheritance • Ambiguity in Multiple Inheritance • Multiple Inheritance with a Common Base s... class Derived class Object Oriented Programming with C++ / Session / of 44 Multiple Inheritance (Contd.) s The syntax for multiple inheritance is similar to that for single inheritance class Teacher... separated by a comma • The rules of inheritance and access for multiple inheritance are the same as for single inheritance Object Oriented Programming with C++ / Session / of 44 Constructors class