Oops File
Oops File
Oops File
Program 1
Write a program to create a class for complex no write function to get values int add two complex no.
Theory:-
• We define a class named Complex with private member variables real and imag to store the real and
imaginary parts of the complex number.
• We provide a constructor to initialize these member variables.
• We define a member function add() which takes another Complex object as an argument and returns a
new Complex object representing the sum of the two complex numbers.
• We define a member function display() to display the complex number in the format a + bi.
• Finally, in the main() function, we create two Complex objects, display them, add them using the add()
function, and display the result.
Code:-
#include <iostream>
class Complex {
private:
double real; // real part
double imag; // imaginary part
public:
// Constructor to initialize real and imaginary parts
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
return imag;
}
};
int main() {
// Creating objects of Complex class
Complex c1(3.0, 2.0); // 3 + 2i
Complex c2(1.5, -1.0); // 1.5 - 1i
return 0;
}
Output:-
Program 2
Write a program to find factorial of a number using recursion.
Theory:
Factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to
n. For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
0! = 1 (by definition)
To calculate the factorial of a number using recursion, we can define a function factorial() that calls itself with a
smaller argument until it reaches the base case (which is when the argument is 0 or 1). The base case will
return 1 since 0! and 1! both equal 1.
If n is 0 or 1, return 1.
Otherwise, return n multiplied by the factorial of (n - 1).
• The factorial() function takes an integer n as input and returns the factorial of n.
• In the main() function, the user is prompted to enter a non-negative integer.
• If the entered number is negative, a message is displayed indicating that factorial is not defined for
negative numbers.
• Otherwise, the factorial() function is called with the entered number, and the result is printed.
Code:-
#include <iostream>
int main() {
int number;
std::cout << "Enter a non-negative integer: ";
std::cin >> number;
if (number < 0) {
std::cout << "Factorial is not defined for negative numbers." << std::endl;
} else {
unsigned long long result = factorial(number);
std::cout << "Factorial of " << number << " is: " << result << std::endl;
}
return 0;
}
Output:-
Program 3
Write a program to implement constructor and destructor.
Theory:-
Constructor:- A constructor is a special member function that is automatically called when an instance
(object) of a class is created. It is primarily used to initialize the object's data members or to perform any
necessary setup tasks.
Destructor:- A destructor is a special member function that is automatically called when an object goes out of
scope or is explicitly deleted using the delete keyword. Its main purpose is to release resources (such as
memory) acquired by the object during its lifetime.
• When the main() function starts, an object of the MyClass is created, so the constructor is called.
• When the program execution ends or the object goes out of scope, the destructor is automatically
called.
Code:-
#include <iostream>
class MyClass {
public:
// Constructor
MyClass() {
std::cout << "Constructor called" << std::endl;
}
// Destructor
~MyClass() {
std::cout << "Destructor called" << std::endl;
}
};
int main() {
// Object creation
MyClass obj;
Theory:-
Switch-case statement:- The switch-case statement is a control statement that allows you to execute
different blocks of code based on the value of a variable or an expression. It provides an efficient way to select
among multiple alternatives.
• The program prompts the user to enter an arithmetic operator (+, -, *, /) and two numbers.
• It then uses a switch-case statement to perform the corresponding arithmetic operation based on the
operator entered by the user.
• The result is displayed to the user.
• If the user enters an invalid operator or attempts to divide by zero, appropriate error messages are
displayed.
Code:-
#include <iostream>
int main() {
char op;
double num1, num2;
return 0;
}
Output:-
Program 5
Write a program to implement virtual function and function overriding using inheritance.
Theory:-
Virtual function:- A virtual function is a member function declared within a base class that can be overridden
by a function with the same signature in a derived class. When a virtual function is called using a pointer or
reference to a base class object, the appropriate derived class function is executed based on the actual type of
the object.
Function overriding:- Function overriding occurs when a derived class provides a specific implementation for
a function that is already defined in the base class. The signature (name and parameter list) of the function in
the derived class must match that of the virtual function in the base class.
Code:-
#include <iostream>
// Base class
class Shape {
public:
// Virtual function
virtual void draw() {
std::cout << "Drawing a shape" << std::endl;
}
};
// Derived class
class Circle : public Shape {
public:
// Override the draw function
void draw() override {
std::cout << "Drawing a circle" << std::endl;
}
};
// Derived class
class Rectangle : public Shape {
public:
// Override the draw function
void draw() override {
std::cout << "Drawing a rectangle" << std::endl;
}
};
int main() {
Shape* shape1 = new Circle(); // Base class pointer pointing to a Circle object
Shape* shape2 = new Rectangle(); // Base class pointer pointing to a Rectangle object
delete shape1;
delete shape2;
return 0;
}
Output:-
Program 6
Write a program to swapping of two numbers using pointer.
Theory:-
Pointer:- Pointers are variables that store memory addresses as their values.
They are used to indirectly access the data stored in memory locations.
In C++, the address-of operator & is used to get the memory address of a variable, and the dereference
operator * is used to access the value stored at a particular memory address.
Swapping using Pointers:- To swap two numbers using pointers, you need to create pointers to the variables
holding those numbers.
Then, you exchange the values stored at the memory addresses pointed to by these pointers.
• We define a function swap() that takes two pointers as arguments and swaps the values pointed to by
these pointers.
• Inside main(), we declare two variables num1 and num2 and initialize them with values.
• We create pointers ptr1 and ptr2 pointing to num1 and num2, respectively.
• We call the swap() function with ptr1 and ptr2 as arguments.
• After swapping, the values of num1 and num2 are printed to verify the swap.
Code:-
#include <iostream>
int temp = *ptr1; // Store the value pointed to by ptr1 in a temporary variable
*ptr1 = *ptr2; // Assign the value pointed to by ptr2 to the memory location pointed to by ptr1
*ptr2 = temp; // Assign the value stored in the temporary variable to the memory location pointed to by
ptr2
int main() {
std::cout << "num1 = " << num1 << ", num2 = " << num2 << std::endl;
swap(ptr1, ptr2);
std::cout << "num1 = " << num1 << ", num2 = " << num2 << std::endl;
return 0;
Output:-