CPP
CPP
• Scope
• Macro
• Operator Overloading
• Exception Handling
• Association
• Composition
• Aggregation
• Inheritance
• Types of Inheritance
• Assignment operator( = )
• Subscript / Index operator( [] )
• Function Call operator[ () ]
• Arrow / Dereferencing operator( -> )
class Person //Parent class In C++ Parent class is called as Base class and child class is
{ }; called as derived class. To create derived class we should use
class Employee : public Person // Child class colon(:) operator. As shown in this code, public is mode of
{ }; inheritance.
class Person //Parent class If we create object of derived class, then all the non- static
{ char name[ 30 ]; int age; }; data member declared in base class & derived class get space
class Employee : public Person //Child class inside it i.e. non-static static data members of base class
{ int empid; float salary; }; inherit into the derived class.
int main( void )
{
Person p;
cout<<sizeof( p )<<endl;
Employee emp;
cout<<sizeof( emp )<<endl;
return 0;
}
• Using derived class name, we can access static data member declared in base class i.e. static data
member of base class inherit into derived class.
class Base{ class Derived : public Base int main( void )
protected: { {
static int number; int num3; Derived d;
static int num4;
}; d.setNum1(10);
public:
int Base::number = 10; d.setNum3(30);
void setNum3( int num3 )
class Derived : public Base{ Derived::setNum2(20);
{ this->num3 = num3; }
public: Derived::setNum4(40);
static void setNum4( int num4 )
static void print( void ) return 0;
{ Derived::num4 = num4; }
{ cout<<Base::number<<endl; } }; }
}; int Derived::num4;
int main( void ){
Derived::print();
return 0;
}
Sunbeam Infotech www.sunbeaminfo.com
Except following functions, including nested class, all the members of base
class, inherit into the derived class
• Constructor
• Destructor
• Copy constructor
• Assignment operator
• Friend function.