Inheritance by RMK

31
INHERITANCE By Prof. Manikandan Dept of Computer Application QMC, Chennai. [email protected]

description

C++ Inheritance Notes

Transcript of Inheritance by RMK

INHERITANCE

Inheritance By Prof. ManikandanDept of Computer ApplicationQMC, [email protected]

DefinitionThe mechanism of deriving a new class from an old one is called inheritance (or derivation).

The old class is referred to as the base class and the new one is called the derived class or subclass.

The reuse of a class that has already been tested, debugged and used many times can the effort of developing and testing the same again.

DEFINING BASE & DERIVED CLASSES Base class:The parent class of a derived class.Classes may be used to crate other classes.Its super class.

Derived class:can be defined by specifying its relationship with the base class in addition own details. Having own properties as well as Base class property.

syntaxc1ass derived class-name : visibility-mode base-c1ass-name { // // members of derived class // };

The colon indicates that the derived-class-name is derived from the base-class-name.visibility-mode is optional and, if present, may be either private or public. The default visibility, mode is private.

Exampleclass ABC: private XYZ // private derivation { members of ABC } ; class ABC: public XYZ // public derivation { members of ABC } ; class ABC: XYZ // private derivation by default { members of ABC }; TYPES OF INHERITANCESingle InheritanceMultilevel InheritanceMultiple InheritanceHierarchical InheritanceHybrid Inheritance

Single Inheritance

Single Inheritance is method in which a derived class has only one base class.

#include#include class emp{ public: int eno; char name[20],des[20]; void get() { couteno; coutname; coutdes; }};

class salary:public emp //Single derivation{ float bp,hra,da,pf,np; public: void get1() { coutbp; couthra; coutda; coutpf; }

void calculate() { np=bp+hra+da-pf; } void display() { cout