Documente Academic
Documente Profesional
Documente Cultură
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
Employee
Engine
Vehical
Vehicle
Person
Containership
class string { private : char str[ 100 ] ; public : string ( char *s = " " ) { } void print( ) { } // functions };
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
protected
public
Base b
Derived d
a :: f2( ) ;
cout << "Adieu" ;
} };
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; }
Types of Inheritance - I
class Base private protected public protected and public class Base private protected class Base private
protected
public protected
public
private
Types of Inheritance - II
private 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 ()
int lcolor ;
public : void set( ) { cout << " set - color" ;
void set( )
{ cout << "set shape" ; } };
} };
void set( )
{ shape :: set( ) ;
color :: set( ) ;
cout << "set - circle" ; } };
rectangle r ;
c.set( ) ; r.set( ) ;
The class derived has been derived from base1 and base2.
Which f( ) would get called in the following code? derived d ; d.f( ) ;
d.base1 :: f( ) ; d.base2 :: f( ) ;