Oops Lab
Oops Lab
REPORT
Lab 2 7 - 13
Lab 3 14 - 21
Lab 4 22 - 24
Lab 5 25 – 27
Lab 6 28 – 36
Lab 7 37 – 45
Lab 8 46 - 54
Lab 9 55 - 63
Lab 10 64 - 66
Lab 11
2|Page
LAB-1
1. Write a C++ program to add two numbers.
#include <iostream> using
namespace std; int main() {
cout << num1 << " + " << num2 << " = " << sum; return 0;
}
OUTPUT
3|Page
Write A C++ program to multiply two numbers.
#include <iostream>
using namespace std;
int main() {
cout << num1 << " * " << num2 << " = " << mult; return 0;
OUTPUT
4|Page
Write A C++ program to print number entered by a user.
#include <iostream>
OUTPUT
5|Page
int main()
{
int a, b,quotient,remainder;
OUTPUT
6|Page
LAB-2
Write a C++ program to find the largest of two nos
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Enter two numbers to be compared" << endl;
cin >> a >> b;
if (a>b)
{
cout << "Largest number is: " << a << endl;
}
else if(b>a)
{
cout << "Largest number is: " << b << endl;
}
else
{
cout << "Both no.s are equal" << endl;
}
return 0;
}
7|Page
Write a C++ program to find the largest of three nos.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
cout << "Enter three numbers to be compared" << endl;
cin >> a >> b >>c;
if (a >= b && a >= c)
{
cout << "The largest number is: " << a << endl;
}
else if (b >= a && b >= c)
{
cout << "The largest number is: " << b << endl;
}
else
{
cout << "The largest number is: " << c << endl;
}
return 0;
}
8|Page
1. Write a C++ program to calculate the roots of a quadratic equation.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
double d, r1, r2;
cout << "The quadratic equation is ax^2+bx+c, where x is root
and a,b,c are coefficients\n";
cout << "\n";
cout << "Enter coefficient a: "<< endl; cin
>> a;
cout << "Enter coefficient b: "<< endl; cin
>> b;
cout << "Enter coefficient c: "<< endl; cin
>> c;
d= (b * b) - (4 * a * c);
if(d>0)
{
r1 = ((-b) + sqrt(d)) / (2 * a);
r2 = ((-b) - sqrt(d)) / (2 * a);
cout << "1st Root: " << r1 << endl;
cout << "2nd Root: " << r2 << endl;
}
else if (d == 0)
{
r1 = -b / (2 * a);
9|Page
cout << "Both roots are equal and their value is: " << r1 <<
endl;
}
else
{
cout << "Roots are complex " << endl;
}
return 0;
}
10 | P a g
e
Write a C++ program to determine whether an entered character is a
vowel or not.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
if(ch=='A' || ch=='a'|| ch=='E' || ch=='e'|| ch=='I'|| ch=='i'|| ch=='O'|| ch=='o'|| ch=='U'||
ch=='u')
{
cout << ch << " is a vowel." << endl;
}
else
{
cout << ch << " is not a vowel." << endl;
}
return 0;
}
11 | P a g
e
Write a C++ program to enter a number from 1-7 and display the corresponding day
of the week using switch case statement.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number from 1 to 7: ";
cin >> n;
switch (n)
{
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input. Please enter a number from 1 to 7." << endl;
}
return 0;
}
12 | P a g
e
13 | P a g
e
LAB-3
Write a C++ program to print the pattern of
1. Right Triangle
CODE:
return 0;
}
OUTPUT
14 | P a g
e
2. Inverted Right Triangle.
CODE:
main() {
int n;
std::cout << "Enter the number of rows: "; std::cin >>
n;
15 | P a g
e
return 0;
}
OUTPUT
3. Equilateral Triangle
CODE:
17 | P a g
e
4. Inverted Equilateral Triangle.
CODE:
OUTPUT
18 | P a g
e
19 | P a g
e
5. Inverted Mirrored Right Triangle.
CODE:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter no. of rows: ";
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i-1;j++)
{
cout<<" ";
}
for(int k=1;k<=n-i+1;k++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
20 | P a g
e
OUTPUT
21 | P a g
e
LAB-4
Write a C++ program to enter a decimal number. Calculate and display the binary
equivalent of this number.
CODE:
#include <iostream>
using namespace std;
int main()
{
int dec, bin = 0, r, p = 1;
cout << "Enter a number to be converted: ";
cin >> dec;
while (dec != 0)
{
r = dec % 2;
bin = bin + (r * p);
dec = dec / 2;
p *= 10;
}
cout << "The number in the binary form is: " << bin ;
return 0;
}
OUTPUT
22 | P a g
e
1. Write a C++ program to print the reverse of a number.
CODE:
#include <iostream>
using namespace std;
int main()
{
int num, rev = 0, d;
cout << "Enter a number to be reversed: ";
cin >> num;
while (num != 0)
{
d = num % 10;
rev = rev * 10 + d;
num /= 10;
}
cout << "Reverse of the number: " << rev << endl;
return 0;
}
OUTPUT:
23 | P a g
e
2. Write a C++ program to enter a number and then calculate the sum of its digit.
CODE:
#include <iostream>
using namespace std;
int main()
{
int n, temp, sum = 0, d;
cout << "Enter an integer: ";
cin >> n;
temp = n;
while (n != 0) {
d = n % 10;
sum += d;
n /= 10;
}
cout << "The sum of the digits of " << temp << " is: " << sum << endl;
return 0;
}
OUTPUT:
24 | P a g
e
LAB-5
Write a simple C++ program with a Class having multiple variables and functions to
demonstrate:
#include<bits/stdc++.h>
using namespace std;
class animal
{
private:
int weight;
public:
// state or properties
int age;
string type;
// parameterised constructor
// class functions
void sleep(){
cout<<"sleeping"<<endl;
}
void eat(){
cout<<"Eating"<<endl;
}
25 | P a g
e int getweight(){
return weight ;
}
this->weight = weight;
}
inline void print()
{
cout<<age<<" "<<weight<<" "<<" "<<type<<endl;
}
// destructor
~animal(){
cout<<"i am inside destructor i.e object destroyed"<<endl ;
}
};
int main()
{
// destructor
26 | P a g
e
27 | P a g
e
LAB-6
Add two arrays using friend function and operator
overloading.
CODE:
#include <iostream> using
namespace std; const int
MAX_SIZE = 100;
class Array {
private:
int size;
int elements[MAX_SIZE];
public:
Array(int s) : size(s) {
if (size > MAX_SIZE) {
cout << "Array size exceeds the maximum size." << endl; exit(1);
}
cout << "Enter elements for the array:" << endl; for
(int i = 0; i < size; ++i)
{
cin >> elements[i];
28 | P a g
e
}
}
29 | P a g
e
}
return result;
}
int main()
{
int size;
Array array1(size);
Array array2(size);
return 0;
}
OUTPUT
30 | P a g
e
Add, subtract, multiply, and divide two complex numbers using
operator overloading (with Constructor).
CODE:
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
int main() {
Complex num1(4.0, 3.0);
Complex num2(5.0, 2.0);
32 | P a g
e
Complex sum = num1 + num2;
Complex diff = num1 - num2;
Complex product = num1 * num2;
Complex quotient = num1 / num2;
return 0;
}
OUTPUT
33 | P a g
e
Convert a square into a rectangle class (type conversion).
CODE
#include <iostream>
using namespace std;
class Square
{
protected:
double side;
public:
Square(double s) : side(s) {}
void setSide(double s) {
side = s;
}
34 | P a g
e
class Rectangle : public Square
{
private:
double width;
public:
Rectangle(double l, double w) : Square(l), width(w) {}
void setWidth(double w) {
width = w;
}
int main()
{
Square square(5.0);
Rectangle rectangle(6.0, 4.0);
35 | P a g
e
cout << "Square Area: " << square.area() << " square units" <<
endl;
cout << "Rectangle Area: " << rectangle.area() << " square
units" << endl;
return 0;
}
OUTPUT
36 | P a g
e
LAB-7
Question:
Problem definition: A Bank maintains two kinds of account for
customers, one called a savings account and the other as current
account.
CODE:
#include <iostream>
#include <string>
using namespace std;
class Account {
protected:
37 string
|Pag
e
customerName; int
accountNumber;
double balance;
public:
void input() {
cout << "Enter Customer Name: ";
getline(cin, customerName);
cout << "Enter Account
Number: "; cin >>
accountNumber;
cout << "Enter Initial
Balance: "; cin >> balance;
cin.ignore(); // Clear the newline character from the buffer
}
void displayBalance() {
cout << "Account Number: " << accountNumber
<< endl; cout << "Customer Name: " <<
customerName << endl; cout << "Balance: " <<
balance << endl;
}
38 | P}aelse
g {
e
cout << "Invalid deposit amount. Please enter a positive amount."
<<
endl;
}
}
};
int main() {
Sav_Acc savingsAccount;
Cur_Acc currentAccount;
int
choice;
do {
40 | P a g
e
cout << "\nChoose an
option:\n"; cout << "1.
Display Balance\n";
cout << "2. Deposit to Savings
Account\n"; cout << "3. Deposit to
Current Account\n";
cout << "4. Compute and Deposit
Interest\n"; cout << "5. Withdraw from
Savings Account\n";
cout << "6. Withdraw with Cheque from Current
Account\n"; cout << "7. Exit\n";
cin >> choice;
switch
(choice) {
case 1:
savingsAccount.displayBalanc
e();
currentAccount.displayBalanc
e(); break;
case 2:
double depositAmountSav;
cout << "Enter the amount to deposit in Savings
Account: "; cin >> depositAmountSav;
savingsAccount.deposit(depositAmount
41 | P a g Sav); break;
e
case 3:
double depositAmountCur;
cout << "Enter the amount to deposit in Current
Account: "; cin >> depositAmountCur;
currentAccount.deposit(depositAmountCur);
break;
case 4:
savingsAccount.computeInterest();
break;
case 5:
double withdrawalAmountSav;
cout << "Enter the amount to withdraw from Savings
Account: "; cin >> withdrawalAmountSav;
savingsAccount.withdrawal(withdrawalAmount
Sav); break;
case 6:
double withdrawalAmountCur;
cout << "Enter the amount to withdraw with cheque from
Current Account: ";
cin >> withdrawalAmountCur;
currentAccount.withdrawalWithCheque(withdrawalAmoun
tCur); break;
case 7:
cout << "Exiting program. Thank you!" << endl;
break;
default:
cout << "Invalid choice. Please select a valid option." << endl;
}
42 | P a g
e } while (choice != 7);
return 0;
}
OUTPUT:
43 | P a g
e
44 | P a g
e
45 | P a g
e
LAB-8
Program 1: Modify the program assignment of Lab 7 by using
constructors to initialize the class members in all 3 classes.
Code:
#include <iostream>
#include <string>
using namespace std;
class account
{
protected:
string customer_name;
int account_number;
float balance;
public:
account(const string &name, int acc_number) : customer_name(name),
account_number(acc_number), balance(0.0)
{
}
void acceptDeposit()
{
float amount;
cout << "Enter the amount to deposit: ";
cin >> amount;
balance += amount;
}
void displayBalance()
{
46 | P a g
e cout << "Account Number: " << account_number << endl;
cout << "Customer Name: " << customer_name << endl;
cout << "Balance: " << balance << endl;
}
};
class sav_acc : public account
{
public:
sav_acc(const string &name, int acc_number) : account(name, acc_number)
{
}
void computeInterest()
{
{
47 | P a g
e balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}
else
{
cout << "Insufficient balance for withdrawal." << endl;
}
}
};
class cur_acc : public account
{
public:
cur_acc(const string &name, int acc_number) : account(name, acc_number) {}
void withdraw()
{
float amount;
cout << "Enter the amount to withdraw: ";
cin >> amount;
if (amount <= balance)
{
balance -= amount;
cout << "Withdrawal of " << amount << " successful." << endl;
}
else
{
cout << "Insufficient balance for withdrawal." << endl;
}
}
};
48 | P a g
int
e main()
{
int choice;
cout << "1. Savings Account\n2. Current Account\nEnter your choice: ";
cin >> choice;
string customer_name;
int account_number;
if (choice == 1)
{
sav_acc sa(customer_name, account_number);
while (true)
{
cout << "1. Deposit\n2. Display Balance\n3. Compute Interest\n4. Withdraw\n5.
Exit\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
sa.acceptDeposit();
break;
case 2:
sa.displayBalance();
49 | P a gbreak;
e
case 3:
sa.computeInterest();
break;
case 4:
sa.withdraw();
break;
case 5:
return 0;
default:
cout << "Invalid choice. Try again." << endl;
}
}
}
else if (choice == 2)
{
cout << "1. Deposit\n2. Display Balance\n3. Withdraw\n4. Exit\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
ca.acceptDeposit();
break;
case 2:
ca.displayBalance();
break;
case 3:
50 | P a g
e ca.withdraw();
break;
case 4:
return 0;
default:
cout << "Invalid choice. Try again." << endl;
}
}
}
else
{
cout << "Invalid account type. Please choose 1 for Savings Account or 2 for Current
Account." << endl;
}
return 0;
}
OUTPUT:
51 | P a g
e
Program 2:
Code:
#include <iostream>
#include <string>
class Person
{ protected:
std::string name;
int code;
public:
Person(std::string _name, int _code) : name(_name), code(_code) {}
void displayPersonInfo()
{
std::cout << "Person Name: " << name << ", Code: " << code << std::endl;
}
}
;
public:
Account(std::string _name, int _code, double _accountPay) : Person(_name, _code),
accountPay(_accountPay) {}
void displayAccountInfo()
{
52 | P a g
displayPersonInfo();
e
std::cout << "Account Pay: $" << accountPay << std::endl;
}
};
class Admin : public Person
{
protected:
int adminExperience;
public:
Admin(std::string _name, int _code, int _adminExperience) : Person(_name, _code),
adminExperience(_adminExperience) {}
void displayAdminInfo()
{
displayPersonInfo();
std::cout << "Admin Experience: " << adminExperience << " years" << std::endl;
}
};
class Master : public Account, public Admin
{
public:
Master(std::string _name, int _code, double _accountPay, int _adminExperience)
Output:
54 | P a g
e
LAB-9
Program 1: Write a C++ program to copy a given string into a
new string. Memory for the new string must be allocated
dynamically.
Code:
#include <iostream>
#include <cstring>
using namespace std; int
main() {
char inputString[100];
return 0;
}
OUTPUT :
55 | P a g
e
Program 2 : Write a C++program to sort an array in ascending order.
Code :
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
bubbleSort(arr, n);
return 0;
}
OUTPUT :
56 | P a g
e
Lab-10
PROGRAM-1:
CODE
#include <iostream> #include
<fstream> using namespace
std;
int main()
{
char arr[100];
cout <<"Enter the name and age\n"; cin.getline (arr, 100);
ofstream myfile ("shailendra.txt"); myfile<<arr;
myfile.close();
cout << "File is created Successfully" <<endl <<endl; return 0;
}
OUTPUT
57 | P a g
e
PROGRAM-2:
CODE
#include<iostream>
#include<fstream>
int main()
char arr[100];
cin.getline (arr,100);
ofstream myfile("shukla.txt");
myfile<<arr;
myfile.close();
char arr1[100];
obj.getline (arr1,100);
cout<<arr1<<endl;
obj.close();
return 0;
} OUTPUT
58 | P a g
e
PROGRAM-3:
CODE
#include<iostream>
#include<fstream>
int main()
char arr[100];
cin.getline (arr,100);
ofstream myfile("shukla.text",ios::app);
myfile<<arr;
myfile.close();
char arr1[100];
obj.getline (arr1,100);
cout<<arr1<<endl;
obj.close(); return 0; }
OUTPUT
59 | P a g
e
PROGRAM-4:
#include<iostream>
#include <fstream>
int main()
ofstream fout;
fout.open("country");
fout.close();
fout.open("capital");
fout<<"washington \n";
fout<<"london \n";
fout<<"seoul";
fout.close();
char line[80];
ifstream fin;
fin.open("country");
60 | P a g
e
cout<<"contents of country file \n";
while(fin){
fin.getline(line,N);
cout <<line<<endl;
fin.close();
return 0;
OUTPUT
PROGRAM-5:
CODE
#include <fstream>
int main()
ofstream fout;
fout.open("country");
fout.close();
61 | P a g
e fout.open("capital");
fout<<"washington \n";
fout<<"london \n";
fout<<"seoul";
fout.close();
char line[200];
ifstream fin;
fin.open("country");
while(!fin.eof()){
fin.getline(line,N);
cout <<line<<endl;
fin.close();
return 0;
OUTPUT
62 | P a g
e
PROGRAM-6:
#include<iostream>
#include<fstream>
int main(){
ifstream f1,f2;
f1.open("country");
f2.open("capital");
char arr[100];
while(!f1.eof()||!f2.eof()){
f1.getline(arr,100);
cout<<"capital of"<<arr<<endl;
f2.getline(arr,100);
cout<<arr<<endl;
f1.close();
f2.close();
63 | P a g
e
Lab-11
PROGRAM-1:
Write a program with the following:
1. A function to read two "complex" double type numbers from keyboard.
2. A function to calculate division of these two complex numbers.
3. A try block to throw an exception when a wrong type of data is keyed in.
4. A try block to detect and throw an exception if the condition “divide by zero ” occurs.
5. Appropriate catch block to handle the exception thrown
CODE
#include <iostream>
#include <stdexcept>
using namespace std;
class Complex
{
public:
double real;
double imag;
};
// Function to read complex numbers from the keyboard Complex readComplexNumber()
{
Complex num;
cout << "Enter real part: ";
cin >> num.real;
if (cin.fail())
{
throw runtime_error("Invalid input. Real part must be a number.");
}
64cout
| P a<<
g "Enter imaginary part: ";
e
cin >> num.imag;
if (cin.fail())
{
throw runtime_error("Invalid input. Imaginary part must be a number.");
}
return num;
}
if (denominator == 0)
{
throw runtime_error("Division by zero is not allowed.");
}
return result;
}
int main()
{
try
{
65 | P a g
e
// Read two complex numbers
Complex num1 = readComplexNumber();
Complex num2 = readComplexNumber();
return 0;
}
OUTPUT
66 | P a g
e