0% found this document useful (0 votes)
6 views18 pages

PCC UNIT 3 NOTES+Question Bank

The document provides comprehensive notes on inheritance in C++, covering definitions, types of inheritance (single, multiple, hierarchical, multilevel, hybrid), access specifiers, ambiguity in multiple inheritance, virtual inheritance, constructors and destructors, abstract classes, friend classes, nested classes, and pointers. It includes programming examples for each concept to illustrate their implementation and usage. Additionally, it discusses memory management with new and delete operators, the this pointer, and provides example programs for single, multiple, and multilevel inheritance.

Uploaded by

rucha4039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

PCC UNIT 3 NOTES+Question Bank

The document provides comprehensive notes on inheritance in C++, covering definitions, types of inheritance (single, multiple, hierarchical, multilevel, hybrid), access specifiers, ambiguity in multiple inheritance, virtual inheritance, constructors and destructors, abstract classes, friend classes, nested classes, and pointers. It includes programming examples for each concept to illustrate their implementation and usage. Additionally, it discusses memory management with new and delete operators, the this pointer, and provides example programs for single, multiple, and multilevel inheritance.

Uploaded by

rucha4039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PCC UNIT 3 NOTES+QUES BANK

Inheritance in C++

 Definition of Inheritance: Inheritance allows a class to derive properties and characteristics


from another class, promoting reusability in Object Oriented Programming (OOP)

Base Class and Derived Class

 Base Class: The parent class that contains common attributes and methods for reuse. It can
have members with different access levels:

 Public: Accessible anywhere.

 Protected: Accessible by derived classes.

 Private: Accessible only within the base class

 Derived Class: The child class that inherits properties from the base class. It can add new
attributes or override existing ones

Types of Inheritance

 Single Inheritance: One derived class inherits from one base class

 Multiple Inheritance: A derived class inherits from multiple base classes

 Hierarchical Inheritance: Multiple derived classes inherit from a single base class

 Multilevel Inheritance: A chain of inheritance where one derived class becomes the base
class for another

 Hybrid Inheritance: A combination of multiple and hierarchical inheritance

Access Specifiers in Inheritance

 Access specifiers (public, protected, private) determine how members of the base class can
be accessed by derived classes

Ambiguity in Multiple Inheritance

 Ambiguity arises when a derived class inherits from multiple base classes with methods or
attributes of the same name. This can be resolved using:

 Scope Resolution Operator: Explicitly specify which base class's member to access

Method Overriding: The derived class's method can override the base class's method

 Virtual Inheritance: Prevents multiple copies of a base class when inherited by


multiple derived classes

Constructors and Destructors

 Constructors initialize objects, while destructors release resources. The base class's
constructor is called before the derived class's constructor, and vice versa for destructors
Abstract Classes

 An abstract class cannot be instantiated and contains at least one pure virtual function,
which must be implemented by derived classes

Friend Classes

 A friend class can access the private and protected members of another class, establishing a
one-way relationship .

Nested Classes

 A nested class is defined within another class and can encapsulate related functionality

Pointers in C++

 A pointer stores the address of another variable. Pointers can be used for dynamic memory
management, allowing for efficient memory allocation and deallocation
using new and delete

Pointer to Objects

 Pointers can also point to objects, allowing access to an object's members using the arrow
operator (->)

The this Pointer

 The this pointer refers to the current instance of a class, allowing access to its members

Arrays vs Pointers

 Arrays are static and allocated at compile time, while pointers are dynamic and can be
allocated or freed at runtime

Q. Define Inheritance. Explain Base class and Derived Class


with programming Example.
Inheritance is the capability of a class to derive properties and behaviors (attributes and
methods) from another class. It enables reusability of code by allowing a new class to inherit
members of an existing class.

Base Class- The parent class (or superclass) that contains common attributes and methods to be
inherited.

Derived Class- The child class (or subclass) that inherits from the base class and can add or
override members.

 Access levels in Base Class:

 Public: Accessible anywhere.

 Protected: Accessible by derived classes.

 Private: Accessible only within the base class.


 Programming Example
 #include <iostream>
 using namespace std;

 // Base class
 class Base {
 public:
 void displayBase() {
 cout << "This is the base class." << endl;
 }
 };

 // Derived class inherits from Base
 class Derived : public Base {
 public:
 void displayDerived() {
 cout << "This is the derived class." <<
endl;
 }
 };

 int main() {
 Derived obj;
 obj.displayBase(); // Access method from
Base class
 obj.displayDerived(); // Access method from
Derived class
 return 0;
 }

 Output:
 This is the base class.
 This is the derived class.

Q.List types of inheritance. Explain any one with the


programming Example.
Types of Inheritance in C++

 Single Inheritance: One derived class inherits from one base class.

 Multiple Inheritance: One derived class inherits from multiple base classes.

 Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
 Multilevel Inheritance: A derived class inherits from another derived class, forming a chain.

 Hybrid Inheritance: Combination of two or more types of inheritance.

Explanation & Example of Single Inheritance

Single Inheritance is the simplest form where one class inherits from one base class.

Syntax:

class Base {
// base class members
};
class Derived : public Base {
// derived class members
};

Example:

#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "Base class method." << endl;
}
};
class Derived : public Base {
public:
void display() {
cout << "Derived class method." << endl;
}
};
int main() {
Derived obj;
obj.show(); // Inherited from Base class
obj.display(); // Defined in Derived class
return 0;
}
Output:

Base class method.


Derived class method.

Q. Explain ambiguity in multiple Inheritances with


programming Example.
Ambiguity in Multiple Inheritance

Ambiguity occurs when a derived class inherits from multiple base classes that have members
(methods or variables) with the same name. The compiler cannot decide which base class
member to use, causing ambiguity.

How Ambiguity Happens

If both base classes have a method with the same name, and the derived class calls that method
without specifying which base class it belongs to, the compiler throws an ambiguity error.

Example of Ambiguity

#include <iostream>
using namespace std;
class Base1 {
public:
void show() {
cout << "Base1 show()" << endl;
}
};
class Base2 {
public:
void show() {
cout << "Base2 show()" << endl;
}
};
class Derived : public Base1, public Base2 {
// Inherits show() from both Base1 and Base2
};
int main() {
Derived obj;
// obj.show(); // Ambiguous: compiler doesn't know which
show() to call
obj.Base1::show(); // Resolving ambiguity using scope
resolution
obj.Base2::show(); // Resolving ambiguity using scope
resolution
return 0;
}

Output:

Base1 show()
Base2 show()

Q. Explain Virtual Inheritance in detail with programming


Example.
Virtual Inheritance is used in multiple inheritance to prevent duplicate copies of a common base
class when it is inherited indirectly through multiple paths. This solves the diamond problem.

Diamond Problem

 Class A is the base class.

 Classes B and C inherit from A.

 Class D inherits from both B and C.

 Without virtual inheritance, D gets two copies of A's members (one via B and one via C),
causing ambiguity and inefficiency.

By declaring inheritance as virtual, C++ ensures only one shared copy of the base class A is
inherited by the final derived class D.

Syntax

class B : virtual public A { ... };


class C : virtual public A { ... };
class D : public B, public C { ... };

Programming Example

#include <iostream>
using namespace std;

// Common Base Class


class Base {
public:
void displayBase() {
cout << "This is the Base class." << endl;
}
};
// Derived Class 1 with virtual inheritance
class Derived1 : virtual public Base {
public:
void displayDerived1() {
cout << "This is Derived1 class derived from Base." <<
endl;
}
};
// Derived Class 2 with virtual inheritance
class Derived2 : virtual public Base {
};
// Final Derived Class inherits from Derived1 and Derived2
class FinalDerived : public Derived1, public Derived2 {
public:
void displayFinalDerived() {
cout << "This is the FinalDerived class derived from
Derived1 and Derived2." << endl;
}
};
int main() {
FinalDerived obj;
obj.displayBase(); // Only one copy of Base's method
obj.displayDerived1();
obj.displayFinalDerived();
return 0;
}

Output:

This is the Base class.


This is Derived1 class derived from Base.
This is the FinalDerived class derived from Derived1 and
Derived2.

Summary

 Virtual inheritance ensures only one instance of the base class is shared.

 Prevents ambiguity and redundancy in multiple inheritance.

 Essential for solving the diamond problem in complex inheritance hierarchies

Q. Explain Constructor and Destructor in Derived class in


detail with programming Example.
Constructor

 A constructor is a special member function automatically called when an object is created.

 It initializes the object.

 In inheritance, base class constructor is called first, then the derived class constructor.

Destructor

 A destructor is a special member function automatically called when an object is destroyed


or goes out of scope.

 It cleans up resources.

 In inheritance, derived class destructor is called first, then the base class destructor.

Order of Calls in Inheritance

Event Order of Calls

Object Creation Base class constructor → Derived class constructor

Object
Derived class destructor → Base class destructor
Destruction

Programming Example

#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base class constructor called" << endl;
}
~Base() {
cout << "Base class destructor called" << endl;
}
};

class Derived : public Base {


public:
Derived() {
cout << "Derived class constructor called" << endl;
}
~Derived() {
cout << "Derived class destructor called" << endl;
}
};
int main() {
Derived obj; // Creating an object of Derived class
return 0; // Object destroyed automatically here
}

Output:

Base class constructor called


Derived class constructor called
Derived class destructor called
Base class destructor called

Q. Explain hybrid Inheritance in detail with programming


Example.
Hybrid Inheritance is a combination of two or more types of inheritance. It is designed to
leverage the features of multiple inheritance forms such as single, multiple, hierarchical, or
multilevel inheritance in a single program.

Hybrid inheritance helps in building complex relationships between classes and improves code
reusability. It can sometimes lead to the diamond problem, which is solved using virtual
inheritance in C++.
Programming Example
#include <iostream>
using namespace std;
class A {
public:
void displayA() {
cout << "This is class A" << endl;
}
};
class B : public A {
public:
void displayB() {
cout << "This is class B" << endl;
}
};
class C : public A {
public:
void displayC() {
cout << "This is class C" << endl;
}
};
class D : public B, public C {
public:
void displayD() {
cout << "This is class D" << endl;
}
};
int main() {
D obj;
// obj.displayA(); // Ambiguity, must specify path
obj.B::displayA(); // Solves ambiguity
obj.displayB();
obj.displayC();
obj.displayD();
return 0;
}
Output
This is class A
This is class B
This is class C
This is class D

Q. Explain Pointer in detail with programming Example.


A pointer is a variable that stores the memory address of another variable. Instead of storing a
value directly, a pointer “points” to the location in memory where the value is stored.

Declaration:
int *ptr; → declares a pointer to an integer.

Initialization:
ptr = &x; → assigns the address of variable x to pointer ptr.

Dereferencing:
*ptr gives the value stored at the memory address pointed by ptr.

& (Address-of) Operator:


Returns the address of a variable.

*** (Dereference) Operator:**


Accesses the value stored at the address.

Programming Example

#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr; // Declaring pointer
ptr = &num; // Storing address of num in ptr
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Pointer ptr stores: " << ptr << endl;
cout << "Value at address ptr points to: " << *ptr << endl;
return 0;
}

Output:

Value of num: 10
Address of num: 0x61ff08 // (actual address may vary)
Pointer ptr stores: 0x61ff08 // (same as above)
Value at address ptr points to: 10

Uses of Pointers:

1. Dynamic memory allocation

2. Arrays and strings manipulation

3. Passing variables by reference to functions

4. Building complex data structures like linked lists, trees

5. Efficient memory usage

Q. Define memory management. Explain New operator and


Delete operator with programming Example.
Memory management refers to the process of allocating and deallocating memory during a
program’s execution. In C++, this is done dynamically using new and delete operators.

Dynamic Memory Allocation in C++

1) new Operator:

 Allocates memory at runtime.

 Returns the address of the memory block.

 Syntax: pointer = new data_type;

2) delete Operator:

 Frees dynamically allocated memory.

 Prevents memory leaks.

 Syntax: delete pointer;

Example

#include <iostream>
using namespace std;
int main() {
int *ptr;
// Allocating memory
ptr = new int;
*ptr = 25;
cout << "Value: " << *ptr << endl;
// Deallocating memory
delete ptr;
return 0;
}

Output:

Value: 25

Advantages of Dynamic Memory Management:

1. Efficient memory usage

2. Useful for unknown data sizes at compile time

3. Enables creation of dynamic data structures

4. Helps manage heap memory

5. Reduces program size when large memory isn't always required

Q. Explain this Pointer in detail with programming Example.


 The this pointer is an implicit pointer available in all non-static member functions of a class.

 It points to the current object (the object that invoked the member function).

 Useful when local variables have the same name as data members.

 Helps in returning the current object, often used in method chaining.

Key Points:

1. Always available inside non-static member functions.

2. Holds the address of the calling object.

3. Helps distinguish between class members and local variables.

4. Used for constructor overloading, operator overloading, and fluent interfaces.

Example
#include <iostream>
using namespace std;
class Student {
private:
int id;
public:
void setId(int id) {
this->id = id; // Using 'this' to distinguish between
member and parameter
}
void display() {
cout << "Student ID: " << this->id << endl;
}
};
int main() {
Student s1;
s1.setId(101);
s1.display();
return 0;
}
Output
Student ID: 101

Why use this pointer?

1. To resolve naming conflicts.

2. To return current object (return *this;).

3. Required in operator overloading.

4. Helps in method chaining.

5. Clarifies code readability when accessing members.

6. Used in constructors for setting values.

7. Makes code safer and consistent in object-oriented programming.

Q. Write a C++ program to implement Single Inheritance.


#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "Base class method." << endl;
}
};
class Derived : public Base {
public:
void display() {
cout << "Derived class method." << endl;
}
};
int main() {
Derived obj;
obj.show(); // Inherited from Base
obj.display(); // From Derived
return 0;
}
Output
Base class method.
Derived class method

Q .Write a C++ program to implement Multiple Inheritance.


#include <iostream>
using namespace std;
class Base1 {
public:
void show() {
cout << "Base1 class method." << endl;
}
};
class Base2 {
public:
void display() {
cout << "Base2 class method." << endl;
}
};
class Derived : public Base1, public Base2 { };
int main() {
Derived obj;
obj.show(); // From Base1
obj.display(); // From Base2
return 0;
}

Output

Base1 class method.


Base2 class method.

Q. Write a C++ program to implement Multilevel Inheritance.


#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "Base class method." << endl;
}
};
class Intermediate : public Base {
public:
void display() {
cout << "Intermediate class method." << endl;
}
};
class Derived : public Intermediate {
public:
void print() {
cout << "Derived class method." << endl;
}
};
int main() {
Derived obj;
obj.show(); // From Base
obj.display(); // From Intermediate
obj.print(); // From Derived
return 0;
}

Output

Base class method.


Intermediate class method.
Derived class method.

Q. Write a C++ program to implement Hierarchical Inheritance.


#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "Base class method." << endl;
}
};
class Derived1 : public Base {
public:
void display1() {
cout << "Derived1 class method." << endl;
}
};
class Derived2 : public Base {
public:
void display2() {
cout << "Derived2 class method." << endl;
}
};
int main() {
Derived1 obj1;
obj1.show();
obj1.display1();
Derived2 obj2;
obj2.show();
obj2.display2();
return 0;
}

Output

Base class method.


Derived1 class method.
Base class method.
Derived2 class method.

You might also like