Unit 4 All Programs
Unit 4 All Programs
#include <iostream>
class Complex {
private:
public:
// Constructor
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c3 = c1 + c2;
c2.display();
c3.display();
return 0;
Explanation:
1. Constructor: Initializes the real and imaginary parts of the complex number.
2. Operator Overloading:
o It creates and returns a new Complex object with the sum of the real and imaginary
parts.
4. Main Function:
Output:
c1 = 3.4 + 5.2i
c2 = 1.6 + 2.8i
c1 + c2 = 5 + 8i
Operator Overloading Using Friend Functions
#include <iostream>
class Complex {
private:
public:
// Constructor
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c3 = c1 + c2;
c1.display();
cout << "c2 = ";
c2.display();
c3.display();
return 0;
Explanation:
1. Constructor: Initializes the real and imaginary parts of the complex number.
2. Friend Function:
o This allows the function to access private members (real and imag) of the Complex
class.
o The function takes two const Complex& arguments and returns a new Complex
object containing the sum of the operands.
4. Main Function:
OUTPUT:
c1 = 3.5 + 2.7i
c2 = 1.4 + 3.3i
c1 + c2 = 4.9 + 6i
• Enables operator overloading where the left-hand operand is not an object of the class.
• Simplifies the access to private and protected members without needing additional getter
methods.
INHERITANCE
Single Inheritance: A class inherits from one base class.
#include <iostream>
// Base class
class Parent {
public:
void displayParent() {
};
// Derived class
public:
void displayChild() {
};
int main() {
Child obj;
obj.displayParent();
obj.displayChild();
return 0;
OUTPUT:
#include <iostream>
class Base1 {
public:
void displayBase1() {
} };
class Base2 {
public:
void displayBase2() {
};
// Derived class
public:
void displayDerived() {
cout << "This is the Derived class (Multiple Inheritance)." << endl;
};
int main() {
Derived obj;
obj.displayBase1();
obj.displayBase2();
obj.displayDerived();
return 0;
}
OUTPUT:
#include <iostream>
// Base class
class Parent {
public:
void displayParent() {
};
public:
void displayChild1() {
};
public:
void displayChild2() {
} };
int main() {
Child1 obj1;
Child2 obj2;
obj1.displayParent();
obj1.displayChild1();
obj2.displayParent();
obj2.displayChild2();
return 0;
OUTPUT:
#include <iostream>
// Base class
class Grandparent {
public:
void displayGrandparent() {
};
// Intermediate class
public:
void displayParent() {
public:
void displayChild() {
cout << "This is the Child class (Multilevel Inheritance)." << endl;
};
int main() {
Child obj;
obj.displayGrandparent();
obj.displayParent();
obj.displayChild();
return 0;
OUTPUT:
#include <iostream>
// Base class
class Base {
public:
void displayBase() {
cout << "This is the Base class." << endl;
};
public:
void displayDerived1() {
};
public:
void displayDerived2() {
};
public:
void displayFinal() {
cout << "This is the Final class (Hybrid Inheritance)." << endl;
};
int main() {
Final obj;
obj.displayDerived1();
obj.displayDerived2();
obj.displayFinal();
return 0;
OUTPUT:
#include <iostream>
using namespace std;
// Base class
class Base {
public:
void displayBase() {
cout << "This is the Base class." << endl;
}
};
OUTPUT:
This is the Base class.
This is the Final class (Multipath Inheritance).
This following program just for reference and not for exam
1 program with 5 inheritance
#include <iostream>
// Base Class
class Base {
public:
void showBase() {
};
// 1. Single Inheritance
public:
void showSingle() {
};
// 2. Multiple Inheritance
class ClassA {
public:
void showA() {
};
class ClassB {
public:
void showB() {
};
public:
void showMultiple() {
};
// 3. Hierarchical Inheritance
public:
void showHierarchical1() {
};
void showHierarchical2() {
};
// 4. Multilevel Inheritance
public:
void showIntermediate() {
cout << "This is Intermediate class (part of Multilevel Inheritance)." << endl;
};
public:
void showMultilevel() {
};
// 5. Hybrid Inheritance
public:
void showHybrid() {
};
int main() {
singleObj.showBase();
singleObj.showSingle();
MultipleDerived multipleObj;
multipleObj.showA();
multipleObj.showB();
multipleObj.showMultiple();
HierarchicalDerived1 hierarchicalObj1;
hierarchicalObj1.showBase();
hierarchicalObj1.showHierarchical1();
HierarchicalDerived2 hierarchicalObj2;
hierarchicalObj2.showBase();
hierarchicalObj2.showHierarchical2();
MultilevelDerived multilevelObj;
multilevelObj.showBase();
multilevelObj.showIntermediate();
multilevelObj.showMultilevel();
HybridDerived hybridObj;
hybridObj.showBase();
hybridObj.showA();
hybridObj.showHybrid();
return 0;
Explanation:
1. Single Inheritance:
2. Multiple Inheritance:
3. Hierarchical Inheritance:
4. Multilevel Inheritance:
5. Hybrid Inheritance:
OUTPUT:
// Base class
class Base {
public:
void displayBase() {
};
public:
void displayDerived1() {
};
public:
void displayDerived2() {
};
public:
void displayFinal() {
cout << "This is the Final class." << endl;
};
int main() {
Final obj;
// Accessing members
obj.displayDerived1();
obj.displayDerived2();
obj.displayFinal();
return 0;
Explanation:
1. Virtual Inheritance:
o The Derived1 and Derived2 classes inherit from the Base class virtually using the
virtual keyword.
o This ensures that Final class inherits only one copy of the Base class, avoiding
ambiguity.
2. Diamond Problem:
o Without virtual inheritance, the Base class would be inherited twice into Final (once
via Derived1 and once via Derived2), causing ambiguity when accessing members of
Base.
3. No Ambiguity:
o Virtual inheritance eliminates multiple copies of the Base class, ensuring only one
shared copy is inherited.
Output:
Key Points:
• Use virtual inheritance when a class appears multiple times in an inheritance hierarchy to
prevent multiple copies of the base class.
class Shape {
public:
// Destructor
virtual ~Shape() {
};
// Derived class 1
class Circle : public Shape {
public:
};
// Derived class 2
public:
};
int main() {
shape1->draw();
shape1->displayInfo();
shape2->draw();
shape2->displayInfo();
// Clean up
delete shape1;
delete shape2;
return 0;
Explanation:
o The Shape class has a pure virtual function draw() declared using virtual void draw()
= 0;.
o A class containing at least one pure virtual function is considered an abstract class.
3. Dynamic Polymorphism:
o A base class pointer (Shape*) is used to call the draw() function on derived class
objects.
4. Destructor:
o A virtual destructor ensures proper cleanup when deleting objects via a base class
pointer.
Output:
Drawing a Circle.
This is a shape.
Drawing a Rectangle.
This is a shape.
Key Points:
• Abstract classes provide a blueprint for derived classes.
• Derived classes must override pure virtual functions, or they themselves become abstract.
#include <iostream>
// Base class
class Base {
public:
Base() {
};
// Derived class
public:
Derived() {
};
int main() {
return 0;
Output:
#include <iostream>
class Outer {
public:
class Inner {
public:
void display() {
};
void show() {
};
int main() {
Outer::Inner objInner;
objInner.display();
Outer objOuter;
objOuter.show();
return 0;
Output:
Pointer to Objects
#include <iostream>
class MyClass {
public:
void display() {
};
int main() {
MyClass obj;
ptr->display();
return 0;
Output:
this Pointer
#include <iostream>
class MyClass {
public:
void display() {
cout << "The address of this object is: " << this << endl;
}
};
int main() {
MyClass obj;
obj.display();
return 0;
Output:
#include <iostream>
class Base {
public:
};
public:
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;
return 0;
Output:
Virtual Functions
#include <iostream>
class Base {
public:
};
public:
};
int main() {
Base* ptr;
Derived obj;
ptr = &obj;
return 0;
Output:
#include <iostream>
class AbstractBase {
public:
};
public:
cout << "Derived class implementing pure virtual function." << endl;
};
int main() {
Derived obj;
obj.display();
return 0;
}
Output:
Polymorphism
#include <iostream>
class Shape {
public:
};
public:
};
public:
};
int main() {
Shape* shape;
Circle circleObj;
Rectangle rectangleObj;
shape = &circleObj;
shape = &rectangleObj;
return 0;
Output:
Drawing a Circle.
Drawing a Rectangle.