classes - inheritance

25
1 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.

Upload: flavia-miranda

Post on 30-Dec-2015

41 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Classes - inheritance

1

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.

Page 2: Classes - inheritance

2

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.

Page 3: Classes - inheritance

3

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.

Page 4: Classes - inheritance

4

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.

Page 5: Classes - inheritance

5

Classes - inheritance• the syntax used to declare a class:

class <classname> [<:baselist>]

{

<memberlist>

};

Page 6: Classes - inheritance

6

Classes - inheritanceclass point {protected:

int xVal, yVal;public:

//...};

class circle : public point{ int radius;

public:// . . .

};

Page 7: Classes - inheritance

7

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 Bclass C : private A {…}; // A is a private base class of Cclass D : public A {…}; // A is a public base class of

Page 8: Classes - inheritance

8

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.

Base Class Private Derived

Public Derived

Protected Derived

Private Member

private private private

Public Member

private public protected

Protected Member

private protected protected

Page 9: Classes - inheritance

9

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.

Page 10: Classes - inheritance

10

Classes - inheritanceclass 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);

};

Page 11: Classes - inheritance

11

Classes - inheritancevoid 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();

}

Page 12: Classes - inheritance

12

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.

Page 13: Classes - inheritance

13

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();

// . . .

}

Page 14: Classes - inheritance

14

Classes - inheritanceNote: 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

// . . .

Page 15: Classes - inheritance

15

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 { // . . .};

Page 16: Classes - inheritance

1616

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.

Page 17: Classes - inheritance

1717

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 {// . . .};

Page 18: Classes - inheritance

1818

Classes- InheritanceOverloaded 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.

Page 19: Classes - inheritance

1919

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);

Page 20: Classes - inheritance

20

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.

Page 21: Classes - inheritance

21

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.

Page 22: Classes - inheritance

22

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>}

Page 23: Classes - inheritance

23

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 {// . . .};

Page 24: Classes - inheritance

24

Classes - inheritanceFor 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.

Page 25: Classes - inheritance

25

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

{// . . .};