Unit - 1: 1) What Is Object Oriented Programming? What Are The Benefits and Applications of Oop?
Unit - 1: 1) What Is Object Oriented Programming? What Are The Benefits and Applications of Oop?
Unit – 1 :
1) What is Object Oriented Programming? What are the benefits and applications of
OOP?
=>
1. Class:
- A class is a blueprint or template for creating objects. It defines the attributes (data)
and methods (functions) that objects of the class will have.
- Think of a class as a blueprint for a house. It specifies what the house will look like,
how many rooms it has, what materials it's made of, etc., but it's not the actual house
itself.
2. Object:
- An object is an instance of a class. It is a concrete realization of the blueprint defined
by the class.
- Going back to the analogy, if a class is the blueprint for a house, then an object is an
actual, physical house built based on that blueprint. Each house (object) constructed
from the same blueprint (class) may have different attributes (such as color, size,
location) but will share the same fundamental structure defined by the blueprint.
Relationship:
- Classes and objects have a hierarchical relationship where a class serves as a
template for creating objects, and objects are instances of classes.
- Multiple objects can be created from a single class, each with its own unique set of
attributes and behaviors, but all sharing the same structure and functionality defined by
the class.
- Changes made to a class can affect all objects created from it, but changes made to
individual objects do not affect other objects or the class itself.
/* In short>>>
In object-oriented programming:
In essence, classes define the structure, while objects represent the actual instances
with specific characteristics and functionalities.
Abstraction:
Abstraction simplifies complex systems by focusing on essential properties and
behaviors , ignoring unnecessary details.
Ex..,
Encapsulation:
5) Explain in brief about reusability in OOP with suitable example.
=>
Ex..,
#include <iostream>
// Inheritance Example
class Animal {
public:
};
public:
};
// Composition Example
class Engine {
public:
void start() {
};
class Car {
private:
Engine engine;
public:
void startEngine() {
engine.start();
};
int main() {
// Inheritance example
// Composition example
Car car;
car.startEngine();
return 0;
Output:
Woof!
Engine started
#include <iostream>
class Shape {
public:
};
public:
};
int main() {
shapePtr->draw();
return 0;
Output:
Drawing a circle
7) Explain the Arithmetic and Bitwise operators C++ with suitable example
=>
Ex..,
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
return 0;
}
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Bitwise Operators: Bitwise operators perform operations at the bit level. They are
used to manipulate individual bits of integer operands.
Bitwise Operators: Bitwise operators perform operations at the bit level. They are
used to manipulate individual bits of integer operands.
Ex..,
#include <iostream>
int main() {
int a = 5;
int b = 3;
return 0;
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left Shift: 10
Right Shift: 2
8) Explain the Relational and Logical Operators C++ with suitable example
=>
Relational operators: Relational operators are used to establish relationships
between operands and determine the equality, inequality, or ordering of values.
Ex..,
#include <iostream>
int main() {
int a = 5, b = 10;
return 0;
Output:
0
1
0
Ex..,
#include <iostream>
int main() {
return 0;
Output:
0
9) Explain the Increment/Decrement and Assignment Operator and Operators C++ with
suitable example
=>
1. Increment/Decrement Operators:
Increment (`++`) and decrement (`--`) operators are used to increase or decrease the
value of a variable by one, respectively. They can be used in prefix form (`++i`, `--i`) or
postfix form (`i++`, `i--`).
Example:
#include <iostream>
int main() {
int i = 5;
// Increment operator
cout << "After increment: " << ++i << endl; // Output: 6
// Decrement operator
cout << "After decrement: " << --i << endl; // Output: 5
return 0;
Output:
Before increment: 5
After increment: 6
Before decrement: 6
After decrement: 5
2. Assignment Operator:
Assignment operator (`=`) is used to assign a value to a variable. It assigns the value on
the right-hand side of the operator to the variable on the left-hand side.
Example:
#include <iostream>
int main() {
int a = 5;
int b;
// Assignment operator
b = a;
return 0;
Output:
Value of b: 5
#include <iostream>
using namespace std;
int main() {
double length, width;
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
double area = length * width;
cout << "Area of the rectangle: " << area << endl;
return 0;
}
b) Write a C++ program to find the factorial of a number using recursive function.
=>
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
}
}
int main() {
int num;
if (num < 0) {
} else {
cout << "Factorial of " << num << " = " << result;
}
return 0;
#include <iostream>
int main() {
cout << "Volume of the cube: " << volume << endl;
return 0;
13)
a) Write a C++ program to find sum of first n number of even and sum of first n
number of odd numbers
=>
#include <iostream>
int main() {
int n;
if (i % 2 == 0) {
sumEven += i;
} else {
sumOdd += i;
cout << "Sum of first " << n << " even numbers: " << sumEven << endl;
cout << "Sum of first " << n << " odd numbers: " << sumOdd << endl;
return 0;
14)
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
b) Write a C++ to generate all the prime numbers between 1 and n, where the value of
n to be taken from the user
=>
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Prime numbers between 1 and " << n << " are: ";
return 0;
}
15)
a) Write a C++ program to find the greatest of three numbers
=>
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
cout << "The greatest number is: " << greatest << endl;
return 0;
}
bool isPrime(int n) {
if (n <= 1) {
return false;
if (n % i == 0) {
return false;
return true;
int main() {
int num;
if (isPrime(num)) {
} else {
cout << num << " is not a prime number." << endl;
return 0;
16)
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
switch (op) {
case '+':
cout << "Result: " << num1 + num2;
break;
case '-':
cout << "Result: " << num1 - num2;
break;
case '*':
cout << "Result: " << num1 * num2;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2;
} else {
cout << "Error! Division by zero.";
}
break;
default:
cout << "Error! Invalid operator.";
break;
}
return 0;
}
b) Write a C++ program to find the reverse of a number. Hence find it is palindrome
or not?
=>
#include <iostream>
int reversedNum = 0;
num /= 10;
return reversedNum;
int main() {
int num;
cout << "Reverse of " << num << " is: " << reversedNum << endl;
if (isPalindrome(num)) {
} else {
return 0;
17)
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double radius, height;
cout << "Enter radius of the cone: ";
cin >> radius;
cout << "Enter height of the cone: ";
cin >> height;
cout << "Area of the cone: " << area << endl;
cout << "Volume of the cone: " << volume << endl;
return 0;
}
b) Write a C++ program to add two numbers using a method that takes two numbers
as parameters and returns the result.
=>
#include <iostream>
using namespace std;
int main() {
double num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
return 0;
}
18)
a) Write a C++ program to convert seconds into hours, minutes and seconds.
=>
#include <iostream>
using namespace std;
int main() {
int seconds, hours, minutes, remainingSeconds;
cout << "Enter the number of seconds: ";
cin >> seconds;
cout << "Hours: " << hours << ", Minutes: " << minutes << ", Seconds: " <<
remainingSeconds << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
swap(num1, num2);
cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
}
19) Write a C++ program to create one dimensional array. Get the array size from the
user. Initialize the array with dynamic elements. Find the maximum and minimum of
those array elements by using a method.
=>
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
int maxElement = findMax(arr, size);
int minElement = findMin(arr, size);
delete[] arr;
return 0;
}
Unit – 2 :
class Rectangle {
// Data members
int length;
int width;
public:
// Member functions
void setLength(int l) {
length = l;
}
void setWidth(int w) {
width = w;
}
int calculateArea() {
return length * width;
}
};
1. Inside the class definition: Member functions are defined directly within the class
declaration. This approach is suitable for small, simple functions.
class MyClass {
public:
void memberFunction() {
// Function definition
}
};
2.Outside the class definition using the scope resolution operator (::): Member
functions are declared inside the class but defined outside using the scope resolution
operator. This approach is suitable for larger functions or when separating interface
from implementation.
Eg..,
class MyClass {
public:
void memberFunction(); // Declaration
};
void MyClass::memberFunction() {
// Function definition
}
3. Inline member functions: Member functions are defined inline within the class
declaration itself. This approach is suitable for small, frequently used functions.
Eg..,
class MyClass {
public:
void memberFunction() {
// Function definition
}
};
Eg..,
class MyClass {
private:
int data;
public:
friend void friendFunction(MyClass obj);
};
3) Define object. How they can be declared? Also explain memory allocation of objects
in C++ Programming
=>
2. Declare an object: Once the class is defined, you can declare objects of that class
by specifying the class name followed by the object name.
Eg..,
class MyClass {
// Class definition
};
int main() {
// Declare an object of MyClass
MyClass obj1;
// Declare another object of MyClass
MyClass obj2;
// ...
return 0;
}
Eg..,
class MyClass {
// Class definition
};
int main() {
// Static variable (static memory allocation)
static MyClass staticObj;
// ...
return 0;
}
Eg..,
class MyClass {
// Class definition
};
int main() {
// Dynamic memory allocation
MyClass* dynamicObj = new MyClass();
int main() {
MyClass obj(5, 10); // Creating object with parameterized constructor
obj.display(); // Output: x = 5, y = 10
return 0;
}
int main() {
MyClass obj; // Creating object
// Object goes out of scope, destructor is called automatically
return 0;
}
int main() {
MyClass obj1, obj2, obj3;
MyClass::displayCount(); // Output: Total objects created: 3
return 0;
}
int main() {
Rectangle obj1(5, 10);
Rectangle obj2(5, 10);
obj1.compare(obj2); // Passing object as parameter
return 0;
}
9 ) What is a friend function? How it can be declared? What are its characteristics?
=>
10 ) Explain the concept of method overloading with suitable example.
=>
Method overloading in C++ allows multiple functions with the same name but
different parameter lists to be defined within the same scope. The compiler selects
the appropriate function to call based on the number and types of arguments passed
to it.
Example:
#include <iostream>
class Math {
public:
return a + b;
return a + b + c;
return a + b;
};
int main() {
Math math;
cout << "Sum of 5 and 10: " << math.add(5, 10) << endl; // Output: 15
cout << "Sum of 5, 10, and 15: " << math.add(5, 10, 15) << endl; // Output: 30
cout << "Sum of 2.5 and 3.5: " << math.add(2.5, 3.5) << endl; // Output: 6.0
return 0;
}
• . (dot)
• .* (member pointer)
• :: (scope resolution)
• ?: (ternary conditional)
• sizeof (size of)
• typeid (type information)
Eg..,
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
int employeeID;
string employeeName;
void getInfo() {
cout << "Enter Employee ID: ";
cin >> employeeID;
cout << "Enter Employee Name: ";
cin.ignore();
getline(cin, employeeName);
}
public:
void displayInfo() {
cout << "Employee ID: " << employeeID << endl;
cout << "Employee Name: " << employeeName << endl;
}
void setData() {
getInfo();
}
};
int main() {
Employee emp;
emp.setData();
cout << "\nEmployee Information:" << endl;
emp.displayInfo();
return 0;
}
Output:
Enter Employee ID: 101
Enter Employee Name: John Doe
Employee Information:
Employee ID: 101
Employee Name: John Doe
15 ) Write a C++ program to design the class Student containing data members
Rollno, name and age. Include a method named display() for display the student
information. Include default constructor and parameterized constructor with
default argument to initialize the data members.
Display three students’ details, using array of objects
=>
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string name;
int age;
public:
// Default constructor
Student() : rollNo(0), name(""), age(0) {}
int main() {
Student students[3] = {
Student(101, "Alice", 20), // Parameterized constructor with explicit values
Student(102, "Bob"), // Parameterized constructor with default age
Student() // Default constructor
};
cout << "Student Details:" << endl;
for (int i = 0; i < 3; ++i) {
cout << "\nStudent " << (i + 1) << ":\n";
students[i].display();
}
return 0;
}
Output:
Student Details:
Student 1:
Roll No: 101
Name: Alice
Age: 20
Student 2:
Roll No: 102
Name: Bob
Age: 0
Student 3:
Roll No: 0
Name:
Age: 0
#include <iostream>
using namespace std;
class Rectangle {
private:
int length;
int breadth;
public:
// Constructor definition outside the class
Rectangle(int l, int b);
// Constructor definition
Rectangle::Rectangle(int l, int b) {
length = l;
breadth = b;
}
int main() {
Rectangle rect(5, 4); // Creating object with constructor
cout << "Area of Rectangle: " << rect.area() << endl; // Accessing member function
return 0;
}
Output:
Area of Rectangle: 20
17)
a)Write a C++ program to design a class Complex for adding two complex numbers;
also show the use of constructor.
=>
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(float r = 0.0, float i = 0.0) : real(r), imag(i) {}
Complex add(const Complex& c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
void display() {
cout << "Complex Number: " << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex num1(2.5, 3.5);
Complex num2(1.5, 2.5);
return 0;
}
Output:
First Complex Number: 2.5 + 3.5i
Second Complex Number: 1.5 + 2.5i
Sum of complex numbers: Complex Number: 4 + 6i
class Student {
private:
int rollNo;
string name;
int age;
public:
Student(int r, string n, int a) : rollNo(r), name(n), age(a) {}
void display() {
cout << "Roll No: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student *ptr = new Student(101, "Alice", 20);
delete ptr;
return 0;
}
Output:
Student Details:
Roll No: 101
Name: Alice
Age: 20
18)
a)Write a C++ program to design a class Geometry containing the methods:
▪area() for finding the area of a square
▪volume() for finding the volume of a cuboid
Also overload the area() function for finding the area of a rectangle
=>
#include <iostream>
using namespace std;
class Geometry {
public:
float area(float side) {
return side * side;
}
int main() {
Geometry geo;
cout << "Area of square with side " << side << ": " << geo.area(side) << endl;
cout << "Area of rectangle with length " << length << " and breadth " << breadth << ": "
<< geo.area(length, breadth) << endl;
cout << "Volume of cuboid with length " << length << ", breadth " << breadth << " and
height " << height << ": " << geo.volume(length, breadth, height) << endl;
return 0;
}
Output:
Area of square with side 5: 25
Area of rectangle with length 6 and breadth 4: 24
Volume of cuboid with length 6, breadth 4 and height 3: 72
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double breadth;
static int count; // Static variable to count the number of Rectangle objects
public:
Rectangle(double l, double b) : length(l), breadth(b) {
count++; // Increment count when an object is created
}
int main() {
// Creating Rectangle objects
Rectangle rect1(3, 4);
Rectangle rect2(5, 6);
return 0;
}
Output:
Number of Rectangle objects created: 2
19)
a) Write a Program to find Maximum out of Two Numbers using friend function.
Note: Here one number is a member of one class and the other number is member
of another class.
=>
#include <iostream>
using namespace std;
class Number1 {
private:
int num;
public:
Number1(int n) : num(n) {}
class Number2 {
private:
int num;
public:
Number2(int n) : num(n) {}
int main() {
Number1 num1(5);
Number2 num2(10);
cout << "Maximum of " << num1.num << " and " << num2.num << " is: " << max(num1,
num2) << endl;
return 0;
}
Output:
Maximum of 5 and 10 is: 10
20) Write a C++ program to overload unary minus (-) operator to negate a point in
three-dimensional space using a member function.
=>
#include <iostream>
using namespace std;
class Point3D {
private:
double x, y, z;
public:
Point3D(double x_val = 0.0, double y_val = 0.0, double z_val = 0.0)
: x(x_val), y(y_val), z(z_val) {}
int main() {
Point3D point(2.5, -3.7, 4.2);
return 0;
}
Output:
Original Point: (2.5, -3.7, 4.2)
Negated Point: (-2.5, 3.7, -4.2)
21) Write a C++ program to overload the + operator for concatenating two strings
using member function. For e.g “I Love ” + “India” = I Love India.
=>
#include <iostream>
#include <string>
using namespace std;
class Concatenator {
private:
string str;
public:
Concatenator(string s) : str(s) {}
int main() {
Concatenator str1("I Love ");
Concatenator str2("India");
return 0;
}
Output:
Concatenated String: I Love India.