OOPs Practical Manual
OOPs Practical Manual
LAB MANUAL
List of Experiments
Output :
Input: Enter a number: 17
Output: 17 is a prime number.
2. Write a Program to read number and to display the largest value between: (a) Two
number, (b)Three Numbers, (c) Four numbers by using switch-case statements.
#include <iostream>
using namespace std;
void largestTwo(int a, int b) {
cout << "Largest between two numbers: " << max(a, b) << endl;
}
void largestThree(int a, int b, int c) {
cout << "Largest between three numbers: " << max({a, b, c}) << endl;
}
void largestFour(int a, int b, int c, int d) {
cout << "Largest between four numbers: " << max({a, b, c, d}) << endl;
}
int main() {
int choice;
cout << "Enter choice: 1 for two numbers, 2 for three numbers, 3 for four numbers: ";
cin >> choice;
switch (choice) {
case 1: {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
largestTwo(a, b);
break;
}
case 2: {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
largestThree(a, b, c);
break;
}
case 3: {
int a, b, c, d;
cout << "Enter four numbers: ";
cin >> a >> b >> c >> d;
largestFour(a, b, c, d);
break;
}
default:
cout << "Invalid choice" << endl;
}
return 0;
}
Explanation
1. The program prompts the user to choose one of three options:
o Option 1: Find the largest between two numbers.
o Option 2: Find the largest between three numbers.
o Option 3: Find the largest between four numbers.
2. Using a switch-case, it reads the appropriate number of inputs for the selected option
and calculates the largest using conditional expressions.
3. The result is printed for each case, and an error message is displayed if the user enters
an invalid choice.
Output :
(a) Two Numbers
Input:
Enter choice: 1
Enter two numbers: 5 8
Output: Largest between two numbers: 8
3. Write a Program to find sum of first natural numbers: sum= 1+2+3+4+……. 100 by
using(a) for loop, (b)while loop, (c) dowhile loop
#include <iostream>
using namespace std;
int main() {
int sum = 0;
return 0;
}
Explanation
1. For loop: Initializes i to 1 and iterates up to 100, adding i to sum in each iteration.
2. While loop: Uses a while loop to accumulate sum while i is incremented from 1 to
100.
3. Do-while loop: Similar to while, but ensures the loop executes at least once.
Each method will yield the sum of the first 100 natural numbers, which is 5050.
Output :
(a) Using for Loop
Output: Sum of first 100 natural numbers (for loop): 5050
(b) Using while Loop
Output: Sum of first 100 natural numbers (while loop): 5050
(c) Using do-while Loop
Output: Sum of first 100 natural numbers (do-while loop): 5050
4. Write a Program to find sum of the following series using function declaration: Sum=
x- (x)3/3!+(x)5/5! ............... (x)n/n!
#include <iostream>
#include <cmath>
using namespace std;
double factorial(int n) {
double fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
double seriesSum(double x, int n) {
double sum = 0;
for (int i = 1; i <= n; i += 2) {
double term = pow(x, i) / factorial(i);
sum += (i % 4 == 1) ? term : -term;
}
return sum;
}
int main() {
double x;
int n;
cout << "Enter value of x and n: ";
cin >> x >> n;
cout << "Sum of the series: " << seriesSum(x, n) << endl;
return 0;
}
Explanation
1. factorial: A function to calculate the factorial of a number. This is used for the
denominator of each term.
2. calculateSeriesSum: A function that calculates the series sum up to the specified
highest odd power n.
o It iterates through odd powers only (1, 3, 5, ...).
o Each term alternates in sign, which is handled by the variable sign.
3. main: Reads x and n from the user, then calls calculateSeriesSum to display the result.
Output :
Input:
Enter value of x and n: 2 5
Output: Sum of the series: 1.933333
5. Write a Program to read the element of the given two matrixes & to perform the
matrix Multiplication.
#include <iostream>
using namespace std;
int main() {
int r1, c1, r2, c2;
// Declare matrices
int mat1[r1][c1], mat2[r2][c2], result[r1][c2] = {0};
}
}
Explanation
1. Input Validation: The program first checks if matrix multiplication is possible by
verifying that the number of columns in the first matrix (c1) matches the number of
rows in the second matrix (r2).
2. Matrix Initialization: Reads elements for both matrices, mat1 and mat2.
3. Multiplication Logic: Uses three nested loops:
o The outer two loops iterate over rows of mat1 and columns of mat2.
o The innermost loop iterates over columns of mat1 (or rows of mat2),
calculating the product and summing it for each element of the resulting
matrix.
4. Display Result: Prints the resulting matrix product.
Output :
1.
Input:
Enter rows and columns of the matrices: 2 2
Enter elements of first matrix:
12
34
Enter elements of second matrix:
20
12
Output:
Product of matrices:
44
10 8
2.
Enter rows and columns of the first matrix: 2 3
Enter rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
123
456
Enter elements of the second matrix:
78
9 10
11 12
Product of the matrices:
58 64
139 154
6. Write a Program to exchange the contents of two variables by using (a) Call by value,
(b) Call by reference.
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swapByReference (Call by Reference): x = " << x << ", y = " << y << endl;
return 0;
}
Explanation and Output
1. swapByValue: This function swaps the values of a and b locally within the function.
Since it's call by value, the original x and y in main remain unchanged.
2. swapByReference: This function swaps the values of a and b by reference. Changes
made to a and b here will directly reflect in x and y in main.
Output :
Before swapping: x = 10, y = 20
Inside swapByValue (Call by Value): a = 20, b = 10
After swapByValue (Call by Value): x = 10, y = 20
After swapByReference (Call by Reference): x = 20, y = 10
if (denominator == 0) {
cout << "Error: Division by zero." << endl;
result.real = result.imag = 0;
return result;
}
int main() {
// Addition
result = add(c1, c2);
cout << "Addition: ";
display(result);
// Subtraction
result = subtract(c1, c2);
cout << "Subtraction: ";
display(result);
// Multiplication
result = multiply(c1, c2);
cout << "Multiplication: ";
display(result);
// Division
result = divide(c1, c2);
cout << "Division: ";
display(result);
return 0;
}
Explanation:
1. Structure Definition:
o The Complex structure represents a complex number with two fields: real and
imag (for real and imaginary parts).
2. Arithmetic Functions:
o Addition: Adds the real and imaginary parts of two complex numbers
separately.
o Subtraction: Subtracts the real and imaginary parts of two complex numbers
separately.
o Multiplication: Uses the formula for multiplying complex numbers:
(a+bi)×(c+di)=(ac−bd)+(ad+bc)i(a + bi) \times (c + di) = (ac - bd) + (ad +
bc)i(a+bi)×(c+di)=(ac−bd)+(ad+bc)i
o Division: Uses the formula for dividing complex numbers:
(a+bi)(c+di)=(ac+bd)(c2+d2)+(bc−ad)(c2+d2)i\frac{(a + bi)}{(c + di)} =
\frac{(ac + bd)}{(c^2 + d^2)} + \frac{(bc - ad)}{(c^2 + d^2)}i(c+di)(a+bi)
=(c2+d2)(ac+bd)+(c2+d2)(bc−ad)i It also checks for division by zero (i.e.,
when the denominator is zero).
3. Display Function:
o The display function prints the complex number in the format a + bi or a - bi
based on the sign of the imaginary part.
4. Main Function:
o The program reads two complex numbers from the user, performs addition,
subtraction, multiplication, and division, and displays the results.
Output :
Enter the real and imaginary parts of the first complex number (c1): 3 2
Enter the real and imaginary parts of the second complex number (c2): 1 4
Addition: 4 + 6i
Subtraction: 2 - 2i
Multiplication: -5 + 14i
Division: 0.7 - 0.4i
8. Write an object oriented program (OOP) using C++ to exchange the private data
members of two different functions using friend Functions.
#include <iostream>
using namespace std;
// Class definition
class Box {
private:
int length;
int width;
public:
// Constructor to initialize the values
Box(int l, int w) : length(l), width(w) {}
b1.length = b2.length;
b1.width = b2.width;
b2.length = tempLength;
b2.width = tempWidth;
}
int main() {
// Creating two objects of class Box
Box box1(10, 5);
Box box2(20, 15);
return 0;
Explanation:
1. Box Class:
o It has two private data members: length and width, which represent the
dimensions of the box.
o A constructor initializes these values.
o A display function is provided to show the values of length and width.
2. Friend Function:
o exchange is a friend function of the Box class. This means exchange can
access and modify the private members (length and width) of the Box class
directly.
o The function takes two Box objects as arguments and exchanges their length
and width values.
3. Main Function:
o Two Box objects, box1 and box2, are created and initialized with different
values.
o Before calling the friend function exchange, the values of the boxes are
displayed.
o After calling exchange, the data members of the two boxes are swapped, and
the new values are displayed.
Output :
Before exchange:
Box 1: Length: 10, Width: 5
Box 2: Length: 20, Width: 15
After exchange:
Box 1: Length: 20, Width: 15
Box 2: Length: 10, Width: 5
9. Write an OOP using C++ to count how many times a particular member function of a
class is called by: (a) A particular object, (b) Any objects
#include <iostream>
using namespace std;
class MyClass {
private:
int individualCount; // Count calls for a particular object
static int globalCount; // Static count, shared by all objects
public:
// Constructor to initialize the individual count
MyClass() {
individualCount = 0;
}
int main() {
// Create two objects of MyClass
MyClass obj1, obj2;
// Display global count (how many times the function was called across all objects)
MyClass::displayGlobalCount();
return 0;
}
Explanation:
1. Class Definition:
Output :
Function called 2 times by this object.
Function called 1 times by this object.
Function called 3 times in total by all objects.
10. Write an OOP using C++ to define a constructor for a “Date” class that initializes
the Date objects with initial values. In case initial values are not provided, it should
initialize the objects with default values.
#include <iostream>
using namespace std;
class Date {
private:
int day;
int month;
int year;
public:
// Constructor with default values
Date(int d = 1, int m = 1, int y = 2000) : day(d), month(m), year(y) {}
void displayDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
};
int main() {
Date date1(15, 8, 2023); // Initializing with custom values
Date date2; // Initializing with default values
Explanation:
1. Class Definition:
o The Date class has three private data members: day, month, and year.
2. Constructor:
o The constructor is designed to take three parameters: d (day), m (month), and
y (year).
o If no arguments are provided when creating an object, default values are
assigned: day = 1, month = 1, and year = 2000.
o If some arguments are provided, the constructor initializes the data members
with those values.
3. Display Function:
o The display function prints the date in the format day/month/year.
4. Main Function:
o The program creates three Date objects:
▪ date1 is created using the default constructor, which initializes the date
as 1/1/2000.
▪ date2 is created with custom values 15/8/2023.
▪ date3 is created with only the day and month specified (10/11), with
the year defaulting to 2000.
Output :
Custom initialized date: Date: 15/8/2023
Default initialized date: Date: 1/1/2000