Opps Practical
Opps Practical
PRACTICAL FILE
of
“OBJECT ORIENTED PROGRAMMING LAB”
SUBJECT CODE : PC-CS 207AL
SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR
THE AWARD OF
BACHELORS OF TECHNOLOGY (B.TECH)
IN
COMPUTER SCIENCE AND ENGINEERING
(SESSION: 2022-2023)
SUBMITTED BY:
Aditya Sharma
2321008
BRANCH : Computer Science and Engineering
UNDER THE SUPERVISION OF:
Er. Seema Rani
Assistant Professor
CSE Department, ACE
INDEX
S.NO PRACTICAL AIM DATE SIGNATURE REMARKS
To make a calculator to
perform functions like
3.
addition, subtraction,
multiplication and division.
Project No. 1
Aim: Raising a number n to a power p is the same as multiplying n by itself p
times. Write a function called power() that takes a double value for n and an int
value for p, and returns result the as double value. Use a default argument of 2
for p, so that if this argument is omitted, the number will be squared. Write a
main() function that gives value from the
user to test this function.
Source Code:
#include<iostream>
using namespace std;
double power(double num ,int pow=2){
double powerIs=1;
double temp=num;
int i=1;
while(i<=pow){
powerIs=temp*powerIs;
i++;
}
return powerIs;
};
}
Software Used: VS Code
Procedure:
Step1: Declare int and double variables.
Step2: Enter the number and exponent through console.
Source Code :
int main(){
double num;
int pow;
double result;
cout<<"Enter the Number : "<<endl;
cin>>num;
cout<<"Enter power you want : "<<endl;
cin>>pow;
result=power(num,pow);
cout<<result;
return 0;
Output:
2321008
Project No. 2
Aim:
A point on the two-dimensional plane can be represented by two numbers: an X
coordinate and a Y coordinates. The sum of two points can be defined as a new
point whose X coordinate is the sum of the X coordinates of the points and whose
Y coordinates is the sum of Y coordinates. Write a program that uses a structure
called point to model a point. Define three points and have the user input values to
two of them. Then set the third point equal to the sum of the other two, and display
the value of the new point. Interaction with the program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+p2 are: 8,11
Software Used: VS Code
Procedure:
Step 1: Make structure point to take values.
Step 2: Declare objects of this structure.
Step 3: call point structure in main().
Step 4: Enter the Coordinates of point 1 and 2.
Step 5: calculate sum.
Step 6: Print sum.
Source Code:
#include<iostream>
using namespace std;
struct point
{ int x,y; };
int main(){
struct point p1,p2,p3;
cout<<"Enter the Coordinates for p1: ";
2321008
cin>>p1.x>>p1.y;
cout<<"Enter the Coordinates for p2: ";
cin>>p2.x>>p2.y;
p3.x=p1.x + p2.x;
p3.y=p1.y + p2.y;
cout<<"Coordinates of p1 + p2 are : "<<p3.x <<","<<p3.y;
return 0;
}
Output:
2321008
Project No. 3
Aim: Create the equivalent of a four-function calculator. The program should
request the user to enter a number, an operator and another number. It should then
carry out the specified arithmetical operation: adding, subtracting, multiplying, or
dividing the two numbers. ( it should use switch statement to select the operation).
Finally it should display the result. When it finishes the calculation, the program
should ask if the user wants to do another calculation. The response can be ‘Y’ or
‘N’. Some sample interaction with the program might look like this.
Enter the first number, operator, second number : 10/3
Answer = 3.33333
Do another(Y/N)? N
Procedure:
Step 1: initialize and declare variables.
Step 2: use do while loop . because it runs first time without checking the condition
Step 3: enter the first number, operator and second number to calculate.
Step 4: use switch case to check the operation
Step 5: show result
Step 6: if user wants another operation run the code again
Source code:
#include<iostream>
using namespace std;
int main(){
char yorN;
char operetor;
double num1, num2;
do{
cout<<"Enter the first number, operator, second number :";
cin>>num1>>operetor>>num2;
cout<<"Answer =";
switch(operetor) {
2321008
Project No. 4
Aim: A phone number, such as (212) 767-8900, can be thought of as having three parts: the
area code (212), the exchange (767) and the number (8900). Write a program that uses a
structure to store these three parts of a phone number separately. Call the structure phone. Create
two structure variables of type phone. Initialize one, and have the user input a number for the
other one. Then display both numbers.
Source Code:
#include<bits/stdc++.h>
using namespace std;
struct phone {
int area_code, exchange, number;
};
int main ()
{
phone p1, p2;
p2 = {212,767,8900};
cout<<"\Enter area code, exchange, and number: ";
cin>>p1.area_code>>p1.exchange>>p1.number;
cout<<endl;
cout<<"My number is "<<"("<<p2.area_code<<") "<<p2.exchange<<"-"<<p2.number;
cout<<endl;
cout<<"Your number is "<<"("<<p1.area_code<<") "<<p1.exchange<<"-"<<p1.number;
return 0;
}
Output:
2321008
2321008
Project No. 5
Aim: Create two classes DM and DB which store the value of distances. DM stores distances in
meters and centimeters and DB in feet and inches. Write a program that can read values for the
class objects and one object of DM with another object of DB. Use the friend function to carry
out the addition operation. The object that stores the results may be a DM object and DB objects,
depending on the units in which the results are required. The display should be in the format of
feet and inches or meters and centimeters depending on the object on display.
Source Code:
#include<iostream>
using namespace std;
class DB;
class DM
{
int m, cm;
public:
void get_data()
{
cout<<"\nEnter meter value: ";
cin>>m;
cout<<"Enter centimeter value: ";
cin>>cm;
}
friend float sum (DM a, DB b);
2321008
};
class DB
{
int ft, in;
public:
void get_data()
{
cout<<"\nEnter feet value: ";
cin>>ft;
cout<<"Enter inches value: ";
cin>>in;
}
friend float sum (DM a, DB b);
};
Output:
2321008
Practical No. 6
Aim: Create a class rational which represents a numerical value by two double values- NUMERATOR
and DENOMINATOR. Include the following public member Functions:
Source Code:
#include<iostream>
using namespace std;
double gcd(int a, int b)
{
if (b ==
0) return
a;
return gcd(b, a % b);
}
class rational
{
public:
2321008
double num,denom;
rational()
{
num=1;
denom=1;
}
rational(double d1,double d2)
{
num=d1;
denom=d2;
}
void reduce()
{
double g;
g=gcd(num,denom);
num/=g; denom/=g;
cout<<"\nNumerator = "<<num<<"\nDenominator = "<<denom;
}
friend ostream &operator<<( ostream &output, const rational &r)
{
output << "Numerator = " <<r.num<< "\nDenominator = " <<r.denom;
return output;
2321008
}
friend istream &operator>>( istream &input, rational &r )
{ input >>r.num>>r.denom;
return input;
}
};
int main()
{
rational r,r1(2,4),r3;
r1.reduce();
cout<<"\nEnter the value of object:";
cin>>r3;
cout<<"\nEnter object values are :"<<r3;
return 0;
}
Output:
2321008
Project No. 7
Aim: Consider the following class definition
Source
Code:
#include<iostream>
using namespace std;
class father
{
protected
2321008
int age;
public:
father(){}
father(int x)
age = x;
};
public:
son(int x)
{ age=x;
}
void iam(){
};
doughter(int x)
age=x;
void iam(){
2321008
cout<<"\nI AM THE DOUGHTER, my age is :"<<age<<endl;
};
int main()
father f(45);
father *p;
f.iam();
son s(19);
p=&s;
p->iam();
doughter d(22);
p=&d;
p->iam();
return 0;
Output:
2321008
Practical No. 8
Aim: Write a program that creates a binary file by reading the data for the students from the
terminal. The data of each student consist of roll.no, name (a string of 30 or lesser no. of
characters) and marks.
Source Code:
#include<iostream>
#include<fstream>
using namespace std;
class student { int
roll_no, marks;
char name [30];
public:
}};
Output:
2321008
Practical No. 9
Aim: A hospital wants to create a database regarding its indoor patients. The Information to
store includes a) Name of the patients b) Date of admission c) Disease d) Date of discharge.
Create a structure to store the data (year, month and date as its members). Create a base class to
store the above information the member function should include function to enter information
and display a list of all the patients in the database. Create a derived class to store the age of the
patients. List the information about all the to store the age of the patients. List the information
about all the pediatric patients (less than 12 years in age).
Source Code:
#include<iostream>
using namespace std;
struct date {
int day;
int month;
int year;
};
class hospital
{ char
name[100];
struct date date_adm;
struct date date_dis;
public:
void getdata() {
2321008
Output: