260 likes | 415 Views
Classes - inheritance. Object-oriented programming provides a facility called inheritance. Under inheritance, a class can inherit the properties of an existing class. Inheritance makes it possible to define a variation of a class without redefining the new class from scratch.
E N D
Classes - inheritance • Object-oriented programming provides a facility called inheritance. • Under inheritance, a class can inherit the properties of an existing class. • Inheritance makes it possible to define a variation of a class without redefining the new class from scratch. • Shared properties are defined only once, and reused as often as desired.
Classes - inheritance • Inheritance is supported by derived classes. • A derived class is like an ordinary class, except that its definition is based on one or more existing classes, called base classes. • Derived class doesn’t make changes to the definition of any of its base classes.
Classes - inheritance • A derived class is also called a subclass, because it becomes a subordinate of the base class in the hierarchy. • A base class may be called a superclass, because from it many other classes may be derived.
Classes - inheritance • A derived class can itself be the base class of another derived class. • The inheritance relationship between the classes of a program is called a class hierarchy.
Classes - inheritance • the syntax used to declare a class: class <classname> [<:baselist>] { <memberlist> };
Classes - inheritance class point {protected: int xVal, yVal; public: //... }; class circle : public point { int radius; public: // . . . };
Classes - inheritance A base class may be specified to be private or public. Unless so specified, the base class is assumed to be private: class A { private: int x; void Fx (void); public: int y; void Fy (void); protected: int z; void Fz (void); }; class B : A {…}; // A is a private base class of B class C : private A {…}; // A is a private base class of C class D : public A {…}; // A is a public base class of
Classes - inheritance • Base class access inheritance rules. • OBS: private inheritance is useful only if you want to hide part of the functionality of the base class.
Classes - inheritance • A member initialization list may be used for initializing any data member of a class. • It is always placed between the constructor header and body. • A colon is used to separate it from the header. • It should consist of a comma-separated list of data members whose initial value appears within a pair of brackets.
Classes - inheritance class point { protected: int xVal, yVal; public: point( int x=0, int y=0) { xVal=x; yVal=y; } void move(int x, int y) { xVal+=x; yVal+=y;} void print(void); }; class circle : public point { int radius; public: circle(int x=0, int y=0, int r=0):point(x,y) { radius =r; } double area(void); void changeRadius(int r) { radius += r; } void print(void); };
Classes - inheritance void point::print(void) { cout<< "\nx="<< xVal; cout<< "\ny="<< yVal; } void circle::print(void) { cout<< "\nx="<< xVal; cout<< "\ny="<< yVal; cout<< "\nradius="<<radius; } double circle::area(void) {return ( 3.14 * radius * radius ); } void main() { circle c1(5,10,20); c1.move(1,2); c1.print(); cout<<"\nArea="<<c1.area(); }
Classes - inheritance • Type Conversion • For any derived class there is an implicit type conversion from the derived class to any of its public base classes. This can be used for converting a derived class object to a base class object, be it a proper object, a reference, or a pointer: • Such conversions are safe because the derived class object always contains all of its base class objects.
Classes - inheritance void main(void) { // . . . circle c1(5,10,20); point p1, *pp; p1 = c1; p1.print(); *pp= &c1; pp->print(); point &rp = c1; rp.print(); // . . . }
Classes - inheritance Note: A base class object cannot be assigned to a derived class object unless there is a type conversion constructor in the derived class defined for this purpose. // . . . circle c1(5,10,20), * pc; point p1; c1 = p1; // illegal *pc= &p1; // illegal circle &rc = p1; // illegal // . . .
Classes - inheritance Multiple Inheritance • It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. • Under multiple inheritance, a derived class inherits all of the members of its base classes. As before, each of the base classes may be private, public, or protected. The same base member access principles apply. class B1 {// . . .}; class B2{ // . . .}; class D : public B1, public B2 { // . . .};
Classes- Inheritance Since the base classes have constructors that take arguments, the constructor for the derived class should invoke these in its member initialization list constructorName (parameterList) : initialisationList { <statements> } The order in which the base class constructors are invoked is the same as the order in which they are specified in the derived class header. The destructors are applied in the reverse order. 16
Classes- Inheritance class point { // . . .}; class circle : public point{// . . .}; class rectangle : public point {// . . .}; class figure : public circle, public rectangle {// . . .}; The class figure has two member xVal, and two members yVal. The problem is overcome by making virtual base classes. A base class is made virtual by placing the keyword virtual before its name in the derived class header: class circle : virtual public point{// . . .}; class rectangle : virtual public point {// . . .}; 17
Classes- Inheritance Overloaded Operators Except for the assignment operator, a derived class inherits all the overloaded operators of its base classes. An operator overloaded by the derived class itself hides the overloading of the same operator by the base classes (in exactly the same way member functions of a derived class hide member functions of base classes). Memberwise initialization and assignment extend to derived classes. For any given class Y derived from X, memberwise initialization is handled by an automatically-generated (or user-defined) constructor of the form: Y::Y (const Y&); Memberwise assignment is handled by an automatically-generated (or user-defined) overloading of the = operator: Y& Y::operator = (Y&) Memberwise initialization (or assignment) of a derived class object involves the memberwise initialization (or assignment) of its base classes as well as its class object members. 18
Classes- Inheritance Virtual Functions Connecting a function call to a function body is called binding. When binding is performed before the program is run (by the compiler and linker), it’s called early binding. If the binding occurs at runtime, it is called late binding (dynamic binding or runtime binding); it is based on the type of the object. When a language implements late binding, there must be some mechanism to determine the type of the object at runtime and call the appropriate member function. To cause late binding to occur for a particular function, C++ requires that you use the virtual keyword when declaring the function in the base class. virtual void print(void); 19
Classes - inheritance Note: • If a function is declared as virtual in the base class, it is virtual in all the derived classes. You don’t need to repeat the keyword virtual in any of the derived-class function redefinitions.
Classes - inheritance Multiple Inheritance • It is possible to derive a new class from more than one base class • Under multiple inheritances, a derived class inherits all of the members of its base classes. • each of the base classes may be private, public, or protected.
Classes - inheritance class B1 {// . . .}; class B2 { // . . .}; class D : public B1, public B2 { // . . .}; • Since the base classes have constructors that take arguments, the constructor for the derived class should invoke these in its member initialization list constructorName (parameterList) : initialisationList { <statements> }
Classes - inheritance • It is possible that B1 and B2 are derived from the some base class, certain members are double. For example, class point { // . . .}; class circle : public point{//. . .}; class rectangle : public point {// . . .}; class figure : public circle, public rectangle {// . . .};
Classes - inheritance For example, class point { // . . .}; class circle : public point{// . . .}; class rectangle : public point {// . . .}; class fig : public circle, public rectangle {// . . .}; The class fig has two member xVal, and two members yVal.
Classes - inheritance • The problem is overcome by making virtual base classes. A base class is made virtual by placing the keyword virtual before its name in the derived class header: class circle : virtual public point {// . . .}; class rectangle : virtual public point {// . . .};