You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To write a C++ program for hierarchical inheritance.
Algorithm :
Start the program and include necessary headers (iostream for input/output)
Define a base class (e.g., Animal) with common data members and member functions that all
derived classes will share.
Define multiple derived classes (e.g., Dog, Cat), each inheriting from the single base class
using the colon (:) syntax and a visibility mode (typically public).
Add unique data members and member functions specific to each derived class.
Create objects of the derived classes in the main function.
Call both the inherited (base class) and unique (derived class) member functions using the
derived class objects.
Print the results to demonstrate access to all members.
End the program
Program
#include<iostream>usingnamespacestd;// Base classclassAnimal {
public:voideat() { cout << "This animal eats food." << endl; }
};
// Derived class 1classDog : publicAnimal {
public:voidbark() { cout << "The dog barks." << endl; }
};
// Derived class 2classCat : publicAnimal {
public:voidmeow() { cout << "The cat meows." << endl; }
};
intmain() {
// Create an object of the Dog class
Dog d;
cout << "Dog Class:" << endl;
d.eat(); // Accessing base class function
d.bark(); // Accessing derived class function
cout << endl; // Add a newline for readability// Create an object of the Cat class
Cat c;
cout << "Cat Class:" << endl;
c.eat(); // Accessing base class function
c.meow(); // Accessing derived class functionreturn0;
}
Output :
Dog Class:
This animal eats food.
The dog barks.
Cat Class:
This animal eats food.
The cat meows.
Ex.No:7(a) Operator Overloading
Aim:
To write a C++ program for Operator Overloading.
Algorithm
Define a class with the necessary data members (usually private) and member functions (public).
Declare a special operator function within the class (as a member function) or outside the class (as a global function, often a friend function if private member access is needed).
The function should use the syntax returnType operator symbol(arguments) (e.g., Complex operator+(const Complex& other)).
Implement the logic within the function to perform the desired operation on the user-defined types.
This typically involves creating a temporary object, performing the calculation on its members, and returning it.
In the main function, create objects of the class and use the overloaded operator in a natural syntax
(e.g., C3 = C1 + C2;). The compiler will internally translate this into a call to the operator function (e.g., C3 = C1.operator+(C2); or a global function call).
Program
#include<iostream>usingnamespacestd;classComplex {
private:int real, imag;
public:// ConstructorComplex(int r = 0, int i = 0) : real(r), imag(i) {}
// Overload the + operator
Complex operator+(const Complex &other) {
Complex temp; // Create a temporary object to store the result
temp.real = real + other.real; // Add the real parts
temp.imag = imag + other.imag; // Add the imaginary partsreturn temp; // Return the result object
}
// Function to display the complex numbervoiddisplay() { cout << real << " + i" << imag << endl; }
};
intmain() {
Complex c1(10, 5);
Complex c2(2, 3);
Complex c3;
// Use the overloaded + operator
c3 = c1 + c2;
cout << "First complex number: ";
c1.display();
cout << "Second complex number: ";
c2.display();
cout << "Sum of complex numbers: ";
c3.display();
return0;
}
Output
First complex number: 10 + i5
Second complex number: 2 + i3
Sum of complex numbers: 12 + i8
Ex.No:7(b) Operator overloading with Friend Functions
Aim
To write a C++ program overloads the binary + operator for a Complex number class using a
function.
Algorithm
#include<iostream>usingnamespacestd;classComplex {
private:float real, imag;
public:// ConstructorsComplex() : real(0), imag(0) {}
Complex(float r, float i) : real(r), imag(i) {}
// Declare the addition operator as a friend functionfriend Complex operator+(Complex a, Complex b);
// Display functionvoiddisplay() { cout << real << " + i" << imag << endl; }
};
// Define the friend function outside the class
Complex operator+(Complex a, Complex b) {
Complex temp;
temp.real = a.real + b.real; // Direct access to private members
temp.imag = a.imag + b.imag; // Direct access to private membersreturn temp;
}
intmain() {
Complex c1(2.5, 3.5);
Complex c2(1.6, 2.7);
Complex c3;
// Use the overloaded + operator
c3 = c1 + c2; // Equivalent to c3 = operator+(c1, c2);
cout << "Complex Number 1: ";
c1.display();
cout << "Complex Number 2: ";
c2.display();
cout << "Sum (CN1 + CN2): ";
c3.display();
return0;
}
Output:
Complex Number 1: 2.5 + i3.5
Complex Number 2: 1.6 + i2.7
Sum (CN1 + CN2): 4.1 + i6.2
Ex.No:7(c) Overloading Unary Operators
Aim
Algorithm
Start the program.
Define a class (e.g., Number) with private data members.
Declare the overloaded operator function as a public member function within the class. This function
should use the operator keyword followed by the operator symbol (e.g., operator-()) and take no
arguments for member functions.
Implement the operator function to perform the desired operation on the object's data members (e.g.,
negate a value, increment a counter).
Create an object of the class in the main function.
Call the overloaded operator function using the operator symbol with the object (e.g., -n1).
Display the results to verify the output.
Stop the program.
Program
#include<iostream>usingnamespacestd;classNumber {
private:int x;
public:// Constructor to initialize the valueNumber(int p) {
x = p;
}
// Overload the unary - operatorvoidoperator -() {
x = -x; // Negates the value of the data member
}
// Function to display the valuevoiddisplay() {
cout << "x = " << x << endl;
}
};
intmain() {
Number n(10); // Create an object 'n' with initial value 10
cout << "Initial value: ";
n.display();
-n; // Calls the overloaded operator-() function
cout << "Value after overloading unary - operator: ";
n.display();
return0;
}
Output
Initial value: x = 10
Value after overloading unary - operator: x = -10
Ex.No:8(a) Pointers with arithmetic operations
Aim
To write a C++ program for arithmetic operations using pointers.
Algorithm
Increment (++, +=): Move the pointer to the next element of the same data type.
Decrement (--, -=): Move the pointer to the previous element.
Addition (+): Calculate the address of an element a certain number of positions forward.
Subtraction (-): Calculate the address of an element a certain number of positions backward, or the number of elements between two pointers.
De-reference the pointer: Use the * operator to access the value at the memory address the
pointer currently holds.
Print results: Display the addresses and values to observe the effects of pointer arithmetic.
Program
#include<iostream>usingnamespacestd;intmain() {
// 1. Declare and initialize an arrayint arr[] = {10, 20, 30, 40, 50};
// Initialize a pointer to the first elementint* ptr = arr;
cout << "Original address (ptr): " << ptr << ", Value: " << *ptr << endl;
// Increment: move to the next element (increments by sizeof(int), usually 4 bytes)
ptr++;
cout << "After Increment (ptr++): " << ptr << ", Value: " << *ptr << endl;
// Addition: move 2 elements forward from the start of the arrayint* new_ptr_add = arr + 2;
cout << "After Addition (arr + 2): " << new_ptr_add << ", Value: " << *new_ptr_add << endl;
// Decrement: move back one element
ptr--;
cout << "After Decrement (ptr--): " << ptr << ", Value: " << *ptr << endl;
// Subtraction: find the number of elements between two pointersint* ptr2 = &arr[4]; // Point to the last elementptrdiff_t diff = ptr2 - arr; // Calculate difference
cout << "Pointer Subtraction (ptr2 - arr): " << diff << " elements" << endl;
return0;
}
Output
Original address (ptr): 0x7ffee4b0a980, Value: 10
After Increment (ptr++): 0x7ffee4b0a984, Value: 20
After Addition (arr + 2): 0x7ffee4b0a988, Value: 30
After Decrement (ptr--): 0x7ffee4b0a980, Value: 10
Pointer Subtraction (ptr2 - arr): 4 elements
Ex.No:8(b) This Pointer
Aim
To write a C++ program using this pointer.
Algorithm
Define a class (e.g., Example) with a private integer data member (data).
Define a public member function (e.g., setValue) that takes an integer parameter with the same
name (data).
Inside the function, use this->data to refer to the class's data member and the plain data to refer
to the function parameter.
Define another member function (e.g., displayValue) to print the value of the data member.
In the main function, create an object of the class.
Call the setValue function to assign a value.
Call the displayValue function to show the result..
Program
#include<iostream>usingnamespacestd;classExample {
private:int data; // Class data memberpublic:// Member function with a parameter named 'data'voidsetValue(int data) {
// 'this->data' refers to the class member// 'data' (without this->) refers to the function parameterthis->data = data;
}
voiddisplayValue() {
cout << "The value of the data member is: " << this->data << endl;
}
};
intmain() {
Example obj;
// Create an object 'obj'
obj.setValue(42);
// Call setValue with the value 42
obj.displayValue(); // Display the stored valuereturn0;
}
Output
The value of the data member is: 42
Ex.No:8(c) Pointers to derived classes and base classes
Aim
To write a C++ program for accessing data from base classes using pointers.
Algorithm
Define a Base Class: Create a class (e.g., Animal) with a virtual function (e.g., speak()). The virtual keyword enables dynamic polymorphism.
Define a Derived Class: Create another class (e.g., Dog) that inherits from the base class and overrides the virtual function.
Create Objects: Instantiate objects of both the base and derived classes.
Declare a Pointer: Declare a pointer of the base class type.
Assign Pointers: Point the base class pointer to the address of the base class object, call the function, and then point the same pointer to the address of the derived class object and call the function again.
Observe Output: Notice how the appropriate function (base or derived) is called at runtime,
Program
#include<iostream>// Base ClassclassAnimal {
public:// Virtual function enables runtime polymorphismvirtualvoidspeak() {
std::cout << "Animal speaks a generic sound." << std::endl;
}
};
// Derived ClassclassDog : publicAnimal {
public:// Overriding the base class functionvoidspeak() {
std::cout << "Dog barks: Woof! Woof!" << std::endl;
}
};
intmain() {
Animal genericAnimal;
Dog specificDog;
// Declare a pointer of the base class type
Animal* animalPtr;
// --- Point to Base Class Object --animalPtr = &genericAnimal;
std::cout << "Pointer points to Base Class object:" << std::endl;
animalPtr->speak(); // Calls Base Class speak()
std::cout << "\n----------------------------------\n" << std::endl;
// --- Point to Derived Class Object --// A pointer to a base class can point to a derived class object (upcasting)
animalPtr = &specificDog;
std::cout << "Pointer points to Derived Class object:" << std::endl;
animalPtr->speak(); // Calls Derived Class speak() because it's a virtual functionreturn0;
}
Output
Pointer points to Base Class object:
Animal speaks a generic sound.
---------------------------------Pointer points to Derived Class object:
Dog barks: Woof! Woof!
Ex.No:9(a) Virtual base class
Aim
To write a C++ program for virtual base class.
Algorithm
Define the initial base class (e.g., A).
Define two intermediate derived classes (e.g., B and C), both of which inherit from the initial
base class virtually using the virtual keyword.
Define the final derived class (e.g., D) which inherits from both intermediate classes (B and C).
Instantiate an object of the final derived class (D). Due to virtual inheritance, only one shared
instance of the initial base class (A) is created.
Access members of the initial base class through the final derived class object without
ambiguity.
Program
#include<iostream>usingnamespacestd;// Step 1: Define the initial base class (A)classA {
public:int value;
A() {
value = 0;
}
};
// Step 2: Define intermediate class B, virtually inheriting from AclassB : virtual public A {
public:B() {
// Constructor for B might set value, but the most derived class controls construction
}
};
// Step 2: Define intermediate class C, virtually inheriting from AclassC : virtual public A {
public:C() {
// Constructor for C might set value, but the most derived class controls construction
}
};
// Step 3: Define final class D, inheriting from B and CclassD : publicB, publicC {
public:// The most derived class (D) is responsible for constructing the virtual base class (A)D() {
value = 42; // Accessing 'value' directly is unambiguous
}
};
intmain() {
// Step 4: Create an object of the most derived class (D)
D obj;
// Step 5: Access the shared member 'value' from the single instance of A
cout << "Value stored in the shared base A instance: " << obj.value << endl;
return0;
}
Output
Value stored in the shared base A instance: 42
Ex.No:9(b) Virtual Destructors
Aim
To write a C++ program for virtual function using destructors.
Algorithm
Define the Base Class: Create a base class with a constructor, a member for dynamically
allocated memory (if needed to demonstrate the leak), and a virtual destructor.
Define the Derived Class: Create a derived class that inherits from the base class, has its own
dynamically allocated memory, constructor, and destructor. The derived destructor
automatically overrides the base's virtual destructor.
Use Polymorphism: In the main function, dynamically allocate an object of the derived class,
but store its address in a pointer to the base class.
Delete through Base Pointer: Use the delete operator on the base class pointer.
Verify Deletion: The virtual destructor mechanism ensures the derived class destructor is called
first, followed by the base class destructor, leading to proper cleanup.
Creating Derived object via Base pointer:
Base constructor called
Derived constructor called, allocated data
Deleting object via Base pointer:
Derived destructor called, freed data
Base destructor called
Ex.No:9(c) Pure Virtual Functions
Aim
To write a C++ program for virtual functions.
Algorithm
Define an abstract base class Shape with a public pure virtual function draw() using the
syntax virtual void draw() = 0;. This makes Shape an abstract class, preventing direct
instantiation.
Define concrete derived classes Circle and Rectangle that inherit from Shape and provide their
specific implementations for the draw() function, using the override keyword for clarity.
In the main function, create instances of the derived classes (circleObj, rectObj).
Declare a pointer of the base class type (Shape* shape1). Abstract classes can have pointers and
references.
Assign the address of the Circle object to the base class pointer.
Call the draw() function through the base class pointer, which uses runtime polymorphism to
execute the derived class's version of the function.
Repeat for the Rectangle object, demonstrating dynamic binding.
Program
#include<iostream>usingnamespacestd;// Abstract base classclassShape {
public:// Pure virtual function declarationvirtualvoiddraw() = 0;
};
// Derived class CircleclassCircle : publicShape {
public:// Implementation of the pure virtual functionvoiddraw() override {
cout << "Drawing a Circle" << endl;
}
};
// Derived class RectangleclassRectangle : publicShape {
public:// Implementation of the pure virtual functionvoiddraw() override {
cout << "Drawing a Rectangle" << endl;
}
};
intmain() {
// Pointers of the abstract base class type are used for polymorphism
Shape* shape1;
Circle circleObj;
Rectangle rectObj;
// Point to a Circle object and call the draw function
shape1 = &circleObj;
shape1->draw(); // Calls Circle's draw()// Point to a Rectangle object and call the draw function
shape1 = &rectObj;
shape1->draw(); // Calls Rectangle's draw()return0;
}