Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Inheritance and Memory Allocation in Object-Oriented Programming (OOP) using C++, Slides of Object Oriented Programming

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

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

115 documents

1 / 34

Toggle sidebar

Related documents


Partial preview of the text

Download Inheritance and Memory Allocation in Object-Oriented Programming (OOP) using C++ and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 22 docsity.com Inheritance in Classes ► If a class B inherits from class A, then B contains all the characteristics (information structure and behavior) of class A ►The parent class is called base class and the child class is called derived class ►Besides inherited characteristics, derived class may have its own unique characteristics docsity.com “IS A” Relationship ► IS A relationship is modeled with the help of public inheritance ► Syntax class ChildClass : public BaseClass{ ... }; docsity.com Example class Person{ ... }; class Student: public Person{ ... }; docsity.com Accessing Members ►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) docsity.com Example void Student::Print() { cout << name << “ is in” << “ semester ” << semester; } ERROR docsity.com Example void Student::Print() { cout << GetName() << “ is in semester ” << semester; } docsity.com Example int main(){ Student stdt; stdt.semester = 0;//error stdt.name = NULL; //error cout << stdt.GetSemester(); cout << stdt.GetName(); return 0; } docsity.com Constructors ►The anonymous object of base class must be initialized using constructor of base class ►When a derived class object is created the constructor of base class is executed before the constructor of derived class docsity.com Constructors Base class constructor initializes the anonymous object Derived class constructor initializes the derived class object base member1 base member2 ... derived member1 derived member2 ... docsity.com class Parent{ public: Parent(){ cout << “Parent Constructor...”;} }; class Child : public Parent{ public: Child(){ cout << “Child Constructor...”;} }; Example docsity.com Constructor ►If the user has given only an overloaded constructor for base class, the compiler will not generate default constructor for base class docsity.com class Parent{ public: Parent(int i){} }; class Child : public Parent{ public: Child(){} } Child_Object; //ERROR Example docsity.com Base Class Initializer ►C++ has provided a mechanism to explicitly call a constructor of base class from derived class ►The syntax is similar to member initializer and is referred as base-class initialization docsity.com Base Class Initializer ►User can provide base class initializer and member initializer simultaneously docsity.com class Parent{ public: Parent(){…} }; class Child : public Parent{ int member; public: Child():member(0), Parent() {…} }; Example docsity.com Base Class Initializer ►The base class initializer can be written after member initializer for derived class ►The base class constructor is executed before the initialization of data members of derived class. docsity.com Example class Student: public Person{ private: int semester; ... public: Student(int a):age(a) { //error } }; docsity.com Reason ►It will be an assignment not an initialization docsity.com Destructors ►Destructors are called in reverse order of constructor called ►Derived class destructor is called before the base class destructor is called docsity.com