


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of inheritance in classes, its types in c++, uml notation, accessing members, allocation in memory, constructors, and destructors. It includes examples and error handling.
Typology: Slides
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























► If a class B inherits from class A, then B contains all the characteristics (information structure and behavior) of class A
► Besides inherited characteristics, derived class may have its own unique characteristics
►There are three types of inheritance in C++
Public Private Protected
► IS A relationship is modeled with the help of public inheritance
► Syntax
class ChildClass : public BaseClass{ ... };
►Public members of base class become
public member of derived class
►Private members of base class are not
accessible from outside of base class, even in the derived class (Information Hiding)
class Person{
char *name; int age; ...
public:
const char *GetName() const; int GetAge() const; ...
};
void Student::Print()
{
cout << name << “ is in” << “ semester ” << semester;
}
void Student::Print()
{
cout << GetName() << “ is in semester ” << semester;
}
►The object of derived class is represented
in memory as follows
base member base member ...
derived member derived member ... docsity.com
►Every object of derived class has an
anonymous object of base class
Base class constructor initializes the anonymous object
Derived class constructor initializes the derived class object
base member base member ...
derived member
derived member
...
class Parent{
public:
Parent(){ cout << “Parent Constructor...”;}
};
class Child : public Parent{
public:
Child(){ cout << “Child Constructor...”;}
};
►If default constructor of base class does
not exist then the compiler will try to generate a default constructor for base class and execute it before executing constructor of derived class
►If the user has given only an overloaded
constructor for base class, the compiler will not generate default constructor for base class