C++ Programming Slides From INFOSYS 03
C++ Programming Slides From INFOSYS 03
Recap of Day 2
• Dynamic Memory Allocation
• Inline Functions
• Static Members
• Const Members
• Polymorphism
• ‘this’ pointer
• Aggregation
• Cin & Cout
• The existing class is referred to as the base class or super class and
the new class is called the derived class or subclass.
• All the objects in this world come under some kind of classification.
Inheritance allows the creation of hierarchical classification.
};
• Here, the visibility mode is optional and , if present, may be either private or
public or protected.
• Visibility mode specifies whether the features of the base class are privately
derived or publicly derived.
• The ‘private’ members cannot be accessed outside the scope of the class.
• Sometimes, we want the derived class to access the private members of the
base class but at the same time, we don’t want other classes or functions to
access those members thru the objects.
• In other words, protected members are private to all, but public to its derived
classes.
Trainee oT1;
Employee oE1;
oE1.m_fBaisic // Invalid
Advantages:
• Derived classes can modify values of members of base directly
• Slight increase in performance; Avoids set/get function call overhead
Disadvantages:
• No validity checking
- Derived class can assign illegal values
• Implementation dependent
- Derived class member functions more likely dependent on base class
implementation
- Base class implementation changes may result in derived class modifications
resulting in Fragile (brittle) software
a) public Inheritance
b) protected Inheritance
c) private Inheritance
MyBase MyDerived
Public Members Public Members
MyBase MyDerived
Public Members Protected Members
MyBase MyDerived
Public Members Private Members
Private
member Protected Private Protected
of Base member Public member member member Public member
class of Base Class of Base class of Base class of Base class of Base class
• When you create object of Derived, Base class default constructor is executed
first and then derived class constructor is executed
• If you want to execute a specific Base class constructor during derived class
object creation, you can specify that in derived class constructor header.
class Employee {
private: int m_iEmpId; float m_fBasic, m_fSal;
public:
Employee(int iEmp,float fBasic) {
// Base Constructor
}
};
Class Trainee : public Employee {
private:
float m_fTPI;
public:
Trainee(int iEmp,float fBasic,float fTPI) : Employee(iEmp,fBasic) {
// Derived Constructor
}
};
oE1.CalcSal()
oT1.CalcSal();
• Here binding function call with the function definition happens during program
execution. This is called as Dynamic Binding.
poE1->CalcSal(); poE1->CalcSal();