Inheritance Essay

1. Inheritance

Inheritance is a manner to organize new categories utilizing categories that have already been defined. Inheritance is employed to assist recycle bing codification with small or no alteration. The new categories, known as Sub-classes, inherit properties and behaviour of the preexistent categories, which are referred to as Super-classes. Example:

include & lt ; iostream & gt ;

We will write a custom essay sample on
Inheritance Essay
or any similar topic only for you
Order now

usingnamespacestd ;

classBaseClass {

inti ;

populace:

voidsetInt ( intn ) ;

intgetInt ( ) ;

} ;

classDerivedClass: publicBaseClass {

intj ;

populace:

voidsetJ ( intn ) ;

intmul ( ) ;

} ;

voidBaseClass: :setInt ( intn )

{

i=n ;

}

intBaseClass: :getInt ( )

{

returni ;

}

voidDerivedClass: :setJ ( intn )

{

j=n ;

}

intDerivedClass: :mul ( )

{

returnj*getInt ( ) ;

}

intmain ( )

{

DerivedClassob ;

ob.setInt ( 10 ) ; //loadiinBaseClass

ob.setJ ( 4 ) ; //loadjinDerivedClass

cout & lt ; & lt ; ob.mul ( ) ; //displays40

return0 ;

}

2. Polymorphism

Polymorphism is a programming linguistic communication characteristic that allows values of different informations types to be handled utilizing a unvarying interface. The construct of parametric polymorphism applies to both informations types and maps. A map that can measure to or be applied to values of different types is known as a polymorphous map. A information type that can look to be of a generalised type ( e.g. , a list with elements of arbitrary type ) is designated polymorphous informations type like the generalised type from which such specialisations are made.

Examples:

// arrows to establish category

# include & lt ; iostream & gt ;

utilizing namespace venereal disease ;

category CPolygon {

protected:

int breadth, tallness ;

populace:

nothingness set_values ( int a, int B )

{ width=a ; height=b ; }

} ;

category CRectangle: public CPolygon {

populace:

int country ( )

{ return ( width * tallness ) ; }

} ;

category CTriangle: public CPolygon {

populace:

int country ( )

{ return ( width * height / 2 ) ; }

} ;

int chief ( ) {

CRectangle rect ;

CTriangle trgl ;

CPolygon * ppoly1 = & A ; rect ;

CPolygon * ppoly2 = & A ; trgl ;

ppoly1- & gt ; set_values ( 4,5 ) ;

ppoly2- & gt ; set_values ( 4,5 ) ;

cout & lt ; & lt ; rect.area ( ) & lt ; & lt ; endl ;

cout & lt ; & lt ; trgl.area ( ) & lt ; & lt ; endl ;

return 0 ;

}

3. Encapsulation

Encapsulation is the concealment of information in order to guarantee that informations constructions and operators are used as intended and to do the usage theoretical account more obvious to the developer. C++ provides the ability to specify categories and maps as its primary encapsulation mechanisms. Within a category, members can be declared as either populace, protected, or private in order to explicitly implement encapsulation. A public member of the category is accessible to any map. A private member is accessible merely to maps that are members of that category and to maps and categories explicitly granted entree permission by the category ( “ friends ” ) . A protected member is accessible to members of categories that inherit from the category in add-on to the category itself and any friends. Example:

category Exforsys

{

populace:

int sample ( ) ;

int illustration ( char *se )

int endfunc ( ) ;

… … …

… … … //Other member maps

private:

int ten ;

float sq ;

… … … .

… … … //Other informations members

} ;

In the above illustration, the informations members integer ten, float sq and other informations members and member maps sample ( ) , illustration ( char* Se ) , endfunc ( ) and other member maps are bundled and put inside a individual independent entity called category Exforsys. This exemplifies the construct of Encapsulation. This particular characteristic is available in object-oriented linguistic communication C++ but non available in procedural linguistic communication C. There are advantages of utilizing this encapsulated attack in C++ . One advantage is that it reduces human mistakes. The information and maps bundled inside the category take entire control of care and therefore human mistakes are reduced. It is clear from the above illustration that the encapsulated objects act as a black box for other parts of the plan through interaction. Although encapsulated objects provide functionality, the naming objects will non cognize the execution inside informations. This enhances the security of the application

4. Operation overloading

Specific instance of polymorphism in which some or all of operators like + , = , or == have different executions depending on the types of their statements. Sometimes the overloading is defined by the linguistic communication ; sometimes the coder can implement support for new types.

Operator overloading is utile because it allows the developer to plan utilizing notation closer to the mark sphere and allows user types to look like types built into the linguistic communication. It can easy be emulated utilizing map calls. For illustrations, the add-on operator is overloaded to let add-on on a user-defined type “ Time ” ( in C++ ) :

Time operator+ ( const Time & A ; lhs, const Time & A ; Rh factor )

{

A Time temp = luteinizing hormone ;

A temp.seconds += rhs.seconds ;

A if ( temp.seconds & gt ; = 60 )

A {

A temp.seconds -= 60 ;

A temp.minutes++ ;

A }

A temp.minutes += rhs.minutes ;

A if ( temp.minutes & gt ; = 60 )

A {

A temp.minutes -= 60 ;

A temp.hours++ ;

A }

A temp.hours += rhs.hours ;

A return temp ;

}

5. Constructor/Destructor

Constructor ( besides known as ctor ) in a category is a particular type of subprogram called when an object is created, either when it is declared statically constructed on the stack. Destructor is a method which is automatically invoked when the object is destroyed. Its chief intent is to clean up and to liberate the resources which were acquired by the object along its life rhythm and unlink it from other objects or resources annuling any mentions in the procedure

// illustration on builders and destructors

# include & lt ; iostream & gt ;

utilizing namespace venereal disease ;

category CRectangle {

int *width, *height ;

populace:

CRectangle ( int, int ) ;

~CRectangle ( ) ;

int country ( ) { return ( *width * *height ) ; }

} ;

CRectangle: :CRectangle ( int a, int B ) {

breadth = new int ;

tallness = new int ;

*width = a ;

*height = B ;

}

CRectangle: :~CRectangle ( ) {

delete breadth ;

delete tallness ;

}

int chief ( ) {

CRectangle rect ( 3,4 ) , rectb ( 5,6 ) ;

cout & lt ; & lt ; “ rect country: “ & lt ; & lt ; rect.area ( ) & lt ; & lt ; endl ;

cout & lt ; & lt ; “ rectb country: “ & lt ; & lt ; rectb.area ( ) & lt ; & lt ; endl ;

return 0 ;

}

×

Hi there, would you like to get such a paper? How about receiving a customized one? Check it out