Sunteți pe pagina 1din 19

Objectives

Ways to reuse existing code. Functions. Containership reuse mechanism. Inheritance reuse mechanism. What are templates ?

Reuse classification
CStringList CString
Object code level Source code level Reuse

CPerson
Containership Inheritance Templates

CEmployee

Also Known as Has a relationship

Also Known as Like a or Kind of relationship

Identify the relationship ?


Person Car

Employee
Engine

Vehical

Director Board of Directors

Vehicle

Person

Board of Directors Car


Engine Director Employee Directors

Containership
class string { private : char str[ 100 ] ; public : string ( char *s = " " ) { } void print( ) { } // functions };

class stringlist { private : string s[ 50 ] ; int c ;

public :
stringlist( ) { c = 0 ; } void add ( string t ) { s[ c ] = t ; c++ ; } void printall( ) { // code } };

Inheritance
class index { private is not private : protected: accessible outside the class. int count ; public : index( ) { count = 0 ; } void display( ) { cout << count ; } void operator ++ () { ++count ; } void operator ++ ( int n) { count ++ ; } };

class index1 : public index { public : void operator - -( int n) { count - - ; } }; void main( ) { index1 i ; i.display( ) ; i++ ; i.display( ) ; i--; i.display( ) ; }
Create object of index 1 instead of index.

Size of objects
class Base { private : int i ; protected : int j ; public : int k ; }; class Derived : public Base { private : int x ; protected : int y ; public : int z ; }; void main( ) { Base b ; Derived d ; cout << sizeof ( b ) << sizeof ( d ) ; }

b j

12

24

class b

Access

private protected public

class d : public b private

protected
public

Base b

Derived d

Which Gets Called ?


# include <iostream.h> class a { public : void f1( ) { cout << "Hello" ; } void f2( ) { cout << "Bye" ; } }; class b : public a { public : void f2( ) { cout << "Hi" ; }
void f3( ) { cout << "GM" ; } void f4( ) { Cases - Use Existing - Override -New - Combination

a :: f2( ) ;
cout << "Adieu" ;

} };

void main( ) { b z; z . f1( ) ; z . f2( ) ; z . f3( ) ; z . f4( ) ; z . a::f2( ) ; }

Hello Hi GM ByeAdieu Bye

Calls Differ
# include <iostream.h> class a { public : void f1( ) { cout << "Hi" ; } }; class b : public a { public : void f2( ) { cout << "Bye" ; a :: f1( ) ; } }; void main( ) { b z; z.f2( ) ; }

# include <iostream.h> class a { public : a( ) { cout << "a's constructor " ; } }; class b : public a { public : b( ) : a( ) { cout << "b's constructor" ; } }; void main( ) { b z; }

Call to 1 argument constructor


# include <iostream.h> class a { public : a( ) { cout << "a's constructor" ; } }; # include <iostream.h> class a { Error in this private : program int i ; public : a ( int j ) { i=j; } }; class b : public a { public : b ( int k ) : a ( k ) { } }; void main( ) { b z ( 15 ) ; }

class b : public a { public : b( ) : a( ) { cout << "b's constructor" ; } };


void main( ) { b z; }

Types of Inheritance - I
class Base private protected public protected and public class Base private protected class Base private

protected
public protected

public

private

class Derived : public Base private protected public

class Derived : protected Base private protected public

class Derived : private Base private protected public

Types of Inheritance - II
private protected public private private protected public

Multiple levels of inheritance

protected public

private

private protected public

protected
public

private protected public

Multiple inheritance

Multiple Inheritance
Shape int x, y ; void Draw () Colour int iColour ; void Draw ()

Circle int radius ; void Draw ()

Rectangle int ht, wd ; void Draw ()

Example Multiple Inheritance


# include <iostream.h> class shape { private : int x, y ; public : class color { private :

int lcolor ;
public : void set( ) { cout << " set - color" ;

void set( )
{ cout << "set shape" ; } };

} };

Example Multiple Inheritance


class circle : private shape, private color { private : int radius ; public : class rectangle : private shape, private color { private : int wd, ht ; public : void set( ) { shape :: set( ) ; color :: set( ) ; cout << "set - rect." ; } };

void set( )
{ shape :: set( ) ;

color :: set( ) ;
cout << "set - circle" ; } };

Example Multiple Inheritance


void main( ) { circle c ; set - shape set - color set - circle

rectangle r ;
c.set( ) ; r.set( ) ;

set - shape set - color set - rectangle

Multiple Inheritance - Problem


Suppose base1 and base2 are base classes. Both base classes contain the function f( ).

The class derived has been derived from base1 and base2.
Which f( ) would get called in the following code? derived d ; d.f( ) ;

Error due to ambiguity.

d.base1 :: f( ) ; d.base2 :: f( ) ;

C++ Programming DAY 3

S-ar putea să vă placă și