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

C++ Constructors, Inheritance & Memory Management

The document outlines various C++ programming concepts including constructors, destructors, member functions, dynamic memory allocation, and different types of inheritance (single, multiple, multilevel, hierarchical, and hybrid). Each section provides an aim, algorithm, program code, output, and results demonstrating the successful implementation of these concepts. Overall, it serves as a comprehensive guide for understanding and applying fundamental C++ programming principles.

Uploaded by

shanmathi.ece05
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 views16 pages

C++ Constructors, Inheritance & Memory Management

The document outlines various C++ programming concepts including constructors, destructors, member functions, dynamic memory allocation, and different types of inheritance (single, multiple, multilevel, hierarchical, and hybrid). Each section provides an aim, algorithm, program code, output, and results demonstrating the successful implementation of these concepts. Overall, it serves as a comprehensive guide for understanding and applying fundamental C++ programming principles.

Uploaded by

shanmathi.ece05
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

1.

Constructors and Destructors

🔹 AIM: To write a simple program to implement constructors and destructors in C++.

🔹 ALGORITHM:

1. Start the program


2. Define a class MyClass
3. Create a constructor that prints a message
4. Create a destructor that prints a message
5. In main(), create an object of MyClass
6. Print a message inside main()
7. End the program

FLOWCHART:
PROGRAM:

#include <iostream>
using namespace std;

class MyClass {
public:
MyClass() {
cout << "Constructor called" << endl;
}
~MyClass() {
cout << "Destructor called" << endl;
}
};

int main() {
MyClass obj;
cout << "Inside main function" << endl;
return 0;
}

OUTPUT:

Code
Constructor called
Inside main function
Destructor called

🔹 RESULT: The program successfully demonstrates the use of constructors and destructors.

2. Member Functions, Classes, and Friend Functions

🔹 AIM: To implement member functions, classes, and friend functions in C++.

🔹 ALGORITHM:

1. Start the program


2. Define a class with private data
3. Define member functions to manipulate data
4. Declare a friend function to access private data
5. In main(), create an object and call both member and friend functions
6. End the program

🔹 FLOWCHART:
🔹 PROGRAM:

cpp
#include <iostream>
using namespace std;

class MyClass {
private:
int data;
public:
MyClass(int val) : data(val) {}
void display() {
cout << "Data: " << data << endl;
}
friend void modify(MyClass&);
};

void modify(MyClass& obj) {


[Link] += 10;
cout << "Data modified by friend function: " << [Link] << endl;
}

int main() {
MyClass obj(5);
[Link]();
modify(obj);
return 0;
}

🔹 OUTPUT:

Code
Data: 5
Data modified by friend function: 15

🔹 RESULT: The program successfully demonstrates member functions and friend functions.

3. Dynamic Memory Allocation and Overloading


🔹 AIM: To implement dynamic memory allocation and function overloading in C++.

🔹 ALGORITHM:

1. Start the program


2. Define a class with overloaded functions
3. Use new to allocate memory dynamically
4. Call overloaded functions
5. Use delete to free memory
6. End the program

🔹 FLOWCHART:

🔹 PROGRAM:

cpp
#include <iostream>
using namespace std;

class Calculator {
public:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
};

int main() {
Calculator* calc = new Calculator;
cout << "Int addition: " << calc->add(3, 4) << endl;
cout << "Float addition: " << calc->add(3.5f, 2.5f) << endl;
delete calc;
return 0;
}

🔹 OUTPUT:

Code
Int addition: 7
Float addition: 6

🔹 RESULT: The program successfully demonstrates dynamic memory allocation and function
overloading.

Program to Implement various inheritances

Aim

To implement a C++ program demonstrating single inheritance, where a derived class inherits
properties and behaviors from a single base class.

Algorithm

1. Start the program.


2. Define a base class with one or more member functions.
3. Define a derived class that inherits from the base class using the public access specifier.
4. Add additional member functions in the derived class.
5. In the main() function:
o Create an object of the derived class.
o Call both base class and derived class functions using the derived object.
6. Display the output.
7. End the program.
📊 Graphical Flowchart
┌───────────────────┐
│ Base Class │
│ (Animal) │
│ eat() │
└─────────┬─────────┘
│ Inherits

┌───────────────────┐
│ Derived Class │
│ (Dog) │
│ bark() │
└─────────┬─────────┘


┌───────────────────┐
│ Main Function │
│ Create Dog object │
│ Call eat() & bark()│
└───────────────────┘

Program
cpp

#include <iostream>
using namespace std;

// Base Class
class Animal {
public:
void eat() {
cout << "Animal eats food." << endl;
}
};

// Derived Class (Single Inheritance)


class Dog : public Animal {
public:
void bark() {
cout << "Dog barks loudly." << endl;
}
};

int main() {
Dog d; // Object of derived class
[Link](); // Base class function
[Link](); // Derived class function

return 0;
}
✅ Output
Code

Animal eats food.


Dog barks loudly.

📌 Result

The program successfully demonstrates single inheritance in C++.

 The derived class Dog inherits the eat() function from the base class Animal.
 The derived class also defines its own function bark().
 When executed, the program shows that the derived class can access both base and
derived class functions.

Aim

To write a C++ program that demonstrates multiple inheritance, where a derived class inherits
properties and behaviors from more than one base class.

⚙️Algorithm

1. Start the program.


2. Define two or more base classes, each with member functions.
3. Define a derived class that inherits from multiple base classes using the public access
specifier.
4. Add additional member functions in the derived class.
5. In the main() function:
o Create an object of the derived class.
o Call functions from all base classes and the derived class using the derived object.
6. Display the output.
7. End the program.

📊 Graphical Flowchart
Code

┌───────────────┐ ┌───────────────┐
│ Base Class 1 │ │ Base Class 2 │
│ (Engine) │ │ (Wheels) │
│ start() │ │ rotate() │
└───────┬────────┘ └───────┬────────┘
│ │
└──────────┬──────────────┘

┌───────────────────┐
│ Derived Class │
│ (Car) │
│ drive() │
└─────────┬─────────┘


┌───────────────────┐
│ Main Function │
│ Create Car object │
│ Call start(), │
│ rotate(), drive() │
└───────────────────┘

C++ Program (Multiple Inheritance)


cpp

#include <iostream>
using namespace std;

// Base Class 1
class Engine {
public:
void start() {
cout << "Engine starts." << endl;
}
};

// Base Class 2
class Wheels {
public:
void rotate() {
cout << "Wheels rotate." << endl;
}
};

// Derived Class (Multiple Inheritance)


class Car : public Engine, public Wheels {
public:
void drive() {
cout << "Car drives smoothly." << endl;
}
};

int main() {
Car c; // Object of derived class
[Link](); // Base class 1 function
[Link](); // Base class 2 function
[Link](); // Derived class function

return 0;
}
✅ Output
Code

Engine starts.
Wheels rotate.
Car drives smoothly.

📌 Result

The program successfully demonstrates multiple inheritance in C++.

 The derived class Car inherits the start() function from the base class Engine and the
rotate() function from the base class Wheels.
 The derived class also defines its own function drive().
 When executed, the program shows that the derived class can access functions from both
base classes as well as its own.

🎯 Aim

To write a C++ program that demonstrates multilevel inheritance, where a derived class inherits
from another derived class, forming a chain of inheritance.

⚙️Algorithm

1. Start the program.


2. Define a base class with member functions.
3. Define a derived class that inherits from the base class.
4. Define another derived class that inherits from the first derived class (multilevel chain).
5. In the main() function:
o Create an object of the last derived class.
o Call functions from all classes using this object.
6. Display the output.
7. End the program.

📊 Graphical Flowchart
Code

┌───────────────────┐
│ Base Class │
│ (Vehicle) │
│ fuel() │
└─────────┬─────────┘
│ Inherits

┌───────────────────┐
│ Derived Class 1 │
│ (Bike) │
│ balance() │
└─────────┬─────────┘
│ Inherits

┌───────────────────┐
│ Derived Class 2 │
│ (SportsBike) │
│ speed() │
└─────────┬─────────┘


┌───────────────────┐
│ Main Function │
│ Create SportsBike │
│ Call fuel(), │
│ balance(), speed() │
└───────────────────┘

C++ Program (Multilevel Inheritance)


cpp

#include <iostream>
using namespace std;

// Base Class
class Vehicle {
public:
void fuel() {
cout << "Vehicle needs fuel." << endl;
}
};

// Derived Class 1
class Bike : public Vehicle {
public:
void balance() {
cout << "Bike balances on two wheels." << endl;
}
};

// Derived Class 2 (Multilevel Inheritance)


class SportsBike : public Bike {
public:
void speed() {
cout << "SportsBike runs at high speed." << endl;
}
};

int main() {
SportsBike sb; // Object of last derived class
[Link](); // Base class function
[Link](); // Derived class 1 function
[Link](); // Derived class 2 function

return 0;
}

✅ Output
Code

Vehicle needs fuel.


Bike balances on two wheels.
SportsBike runs at high speed.

📌 Result

The program successfully demonstrates multilevel inheritance in C++.

 The base class Vehicle provides the fuel() function.


 The derived class Bike inherits from Vehicle and adds the balance() function.
 The derived class SportsBike inherits from Bike and adds the speed() function.
 When executed, the program shows that the final derived class can access functions from
all levels of the inheritance chain.

🎯 Aim

To write a C++ program that demonstrates hierarchical inheritance, where multiple derived
classes inherit from a single base class.

⚙️Algorithm

1. Start the program.


2. Define a base class with member functions.
3. Define two or more derived classes that inherit from the base class using the public
access specifier.
4. Add additional member functions in each derived class.
5. In the main() function:
o Create objects of each derived class.
o Call base class functions and derived class functions using these objects.
6. Display the output.
7. End the program.

📊 Graphical Flowchart
Code
┌───────────────────┐
│ Base Class │
│ (Shape) │
│ draw() │
└─────────┬─────────┘

┌──────────────┴──────────────┐
▼ ▼
┌───────────────────┐ ┌───────────────────┐
│ Derived Class 1 │ │ Derived Class 2 │
│ (Circle) │ │ (Rectangle) │
│ area() │ │ area() │
└─────────┬─────────┘ └─────────┬─────────┘
│ │
▼ ▼
┌───────────────────┐ ┌───────────────────┐
│ Main Function │ │ Main Function │
│ Create Circle obj │ │ Create Rectangle obj│
│ Call draw(), area()│ │ Call draw(), area() │
└───────────────────┘ └───────────────────┘

C++ Program (Hierarchical Inheritance)


cpp

#include <iostream>
using namespace std;

// Base Class
class Shape {
public:
void draw() {
cout << "Drawing a shape." << endl;
}
};

// Derived Class 1
class Circle : public Shape {
public:
void area() {
cout << "Area of Circle = πr^2" << endl;
}
};

// Derived Class 2
class Rectangle : public Shape {
public:
void area() {
cout << "Area of Rectangle = l*b" << endl;
}
};

int main() {
Circle c; // Object of derived class Circle
[Link](); // Base class function
[Link](); // Derived class function
Rectangle r; // Object of derived class Rectangle
[Link](); // Base class function
[Link](); // Derived class function

return 0;
}

✅ Output
Code

Drawing a shape.
Area of Circle = πr^2
Drawing a shape.
Area of Rectangle = l*b

📌 Result

The program successfully demonstrates hierarchical inheritance in C++.

 The base class Shape provides the draw() function.


 The derived class Circle inherits from Shape and adds the area() function for circles.
 The derived class Rectangle inherits from Shape and adds the area() function for
rectangles.
 When executed, the program shows that each derived class can access the base class
function as well as its own specific function.

🎯 Aim

To write a C++ program that demonstrates hybrid inheritance, which is a combination of two
or more types of inheritance (e.g., multiple + hierarchical or multiple + multilevel).

⚙️Algorithm

1. Start the program.


2. Define a base class with member functions.
3. Define two derived classes that inherit from the base class.
4. Define another class that inherits from both derived classes (combining multiple and
hierarchical inheritance).
5. In the main() function:
o Create an object of the final derived class.
o Call functions from the base class, intermediate derived classes, and the final
derived class.
6. Display the output.
7. End the program.

📊 Graphical Flowchart
Code

┌───────────────────┐
│ Base Class │
│ (Base) │
│ show() │
└─────────┬─────────┘

┌──────────────┴──────────────┐
▼ ▼
┌───────────────────┐ ┌───────────────────┐
│ Derived Class 1 │ │ Derived Class 2 │
│ (Derived1) │ │ (Derived2) │
│ display1() │ │ display2() │
└─────────┬─────────┘ └─────────┬─────────┘
│ │
└──────────────┬──────────────┘

┌───────────────────┐
│ Final Derived │
│ (Hybrid) │
│ hybridFunc() │
└─────────┬─────────┘


┌───────────────────┐
│ Main Function │
│ Create Hybrid obj │
│ Call show(), │
│ display1(), │
│ display2(), │
│ hybridFunc() │
└───────────────────┘

C++ Program (Hybrid Inheritance)


cpp

#include <iostream>
using namespace std;

// Base Class
class Base {
public:
void show() {
cout << "Base class function." << endl;
}
};

// Derived Class 1
class Derived1 : public Base {
public:
void display1() {
cout << "Derived1 class function." << endl;
}
};

// Derived Class 2
class Derived2 : public Base {
public:
void display2() {
cout << "Derived2 class function." << endl;
}
};

// Hybrid Derived Class (Multiple + Hierarchical)


class Hybrid : public Derived1, public Derived2 {
public:
void hybridFunc() {
cout << "Hybrid class function." << endl;
}
};

int main() {
Hybrid h; // Object of final derived class
[Link](); // Hybrid class function

// Note: Ambiguity occurs if we directly call show()


// because both Derived1 and Derived2 inherit Base.
// We must specify the path explicitly:
h.Derived1::show();
h.display1();
h.Derived2::show();
h.display2();

return 0;
}

✅ Output
Code

Hybrid class function.


Base class function.
Derived1 class function.
Base class function.
Derived2 class function.

📌 Result

The program successfully demonstrates hybrid inheritance in C++.

 The base class Base provides the show() function.


 Two derived classes Derived1 and Derived2 inherit from Base.
 The final class Hybrid inherits from both Derived1 and Derived2, combining multiple
and hierarchical inheritance.
 When executed, the program shows that the final derived class can access functions from
all classes, though ambiguity must be resolved using scope resolution (Derived1::show,
Derived2::show).

You might also like