[go: up one dir, main page]

0% found this document useful (0 votes)
959 views24 pages

Opps Practical

The document provides details of several projects completed as part of an Object Oriented Programming lab subject. It includes the aim, source code, procedure and output for each project. The projects involve writing functions to calculate the power of a number, add coordinates, create a basic calculator, store phone numbers in a structure, add distances in different units using classes, and more. The document is submitted as the practical file for a Bachelor of Technology degree in computer science and engineering.

Uploaded by

Aditya sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
959 views24 pages

Opps Practical

The document provides details of several projects completed as part of an Object Oriented Programming lab subject. It includes the aim, source code, procedure and output for each project. The projects involve writing functions to calculate the power of a number, add coordinates, create a basic calculator, store phone numbers in a structure, add distances in different units using classes, and more. The document is submitted as the practical file for a Bachelor of Technology degree in computer science and engineering.

Uploaded by

Aditya sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

2321008

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

Department of Computer Science and Engineering


Ambala College of Engineering and Applied Research, Devsthali, Ambala
(Haryana)
Affiliated to Kurukshetra University, Kurukshetra
2321008

INDEX
S.NO PRACTICAL AIM DATE SIGNATURE REMARKS

To find the power of a


1.
number.

2. To add two Co-ordinates.

To make a calculator to
perform functions like
3.
addition, subtraction,
multiplication and division.

To store the three parts of a


4.
phone number separately.

To add the values in the


5. format of feet and inches or
meters and centimeters.

To add two Rational


6.
Numbers.

To find age of daughter and


7. son based on the age of
father.

To create a binary file by


8.
reading the data.

To create a database which


9. can store the information of
a patient.

10. To test the function.


2321008

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.

Step3: Make power()function to calculate the power of the number.


Step4: Calculate the power using for loop.
Step5: Call the power()function in main()function.
Step6: Print the result.
2321008

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

case ‘+’: cout<<num1+num2<<" "<<endl;


break;
case ‘-’:cout<<num1-num2<<" "<<endl;
break;
case ‘*’:cout<<num1*num2<<" "<<endl;
break;
case'/’:cout<<num1/num2<<" "<<endl;
break;
default: cout<<"You Entered an invalid operation ";
}
cout<<"Do Another (Y/N) ? ";
cin>>y0rN;
while(yOrN ==’ Y ‘||y0rN ==’ y’); I
return 0;
}
Output:
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.

Software Used: Code Blocks.


Procedure:
Step 1: Make a structure.
Step 2: Make required variables of the structure.
Step 3: Start main function.
Step 4: Create two objects of structure.
Step 5: Display my number using object1 (p1).
Step 6: Display your number using object2 (p2).

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.

Software Used: Code Blocks.


Procedure:
Step 1: Create two classes (DB and DM).
Step 2: Create Variables of class DB (feet and inches).
Step 3: Create Variables of class DM (meter and centimeter).
Step 4: Take values of these variables from user.
Step 5: Create friend function (sum ()) to sum the all values.
Step 6: Create the objects of both classes (DB and DM).
Step 7: Using Objects call the functions of the classes.
Step 8: Call function (sum ()) to display the sum of all values.

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);
};

float sum (DM a, DB b)


{
float x, y, z;
x=(a.m+(a.cm/100));
y=(b.ft+(b.in/12));
z=(x+(y*0.304)); //Since 1 ft = 0.304 m
return z;
}
int main()
{
DM a;
DB b;

cout<<"Enter the value in meter and centimeter: ";


a.get_data();
2321008

cout<<"\nEnter the value in feet and inches: ";


b.get_data();
sum(a, b);
cout<<"\nThe summed value in meter is: "<<sum (a, b) <<endl;
return 0;
}

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:

 Constructor with no arguments(default).


 Constructor with two arguments.
 Void reduce() that rational number by eliminating the highest common factor between
the numerator and denominator.
 Overload + operator to add two rational number.
 Overload >> operator to enable input through cin.
 Overload << operator to enable output through cout.
 Write a main() to test all the functions in the class.

Software Used: Code Blocks.


Procedure:
Step 1: Make a function god() to calculate HCF and GCD of numerator or
denominator.
Step 2: Make a class rational().
Step 3: Default constructor of class provide default values.
Step 4: make double argument constructure of rational class.
Step 5: make reduce() function to reduce the value of numerator and denominator.
Step 6: create two function for operator overloading (ostream and istream).
Step 7: Create main() function.
Step 8: Create objects of rational() class and call the values of numerator and
denominator .

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

class father { protected : int age; public;


father (int x) {age =
x;} virtual void iam
()
{ cout < < I AM THE FATHER, my age is : << age<< end1:}
};
Derive the two classes son and daughter from the above class and for each, define iam ( ) to
write our similar but appropriate messages. You should also define suitable constructors for
these classes. Now, write a main ( ) that creates objects of the three classes and then calls iam ( )
for them. Declare pointer to father. Successively, assign addresses of objects of the two derived
classes to this pointer and in each case, call iam ( ) through the pointer to demonstrate
polymorphism in action.

Software Used: Code Blocks.


Procedure:

Step 1: Create a base class as named father.


Step 2: Create two derived classes (son and daughter).
Step 3: In each class make function iam() to display a message.
Step 4: Create the main() function.
Step 5: Make objects of each class. Step 6: Make pointer of base class.
Step 7: Call the functions of derived class using base class pointer.

Source
Code:
#include<iostream>
using namespace std;
class father

{
protected
2321008

int age;
public:
father(){}
father(int x)

age = x;

virtual void iam(){

cout<<"I AM THE FATHER, my age is : "<<age;}

};

class son:public father

public:

son(int x)
{ age=x;

}
void iam(){

cout<<"\nI AM THE SON, my age is :"<<age<<endl;

};

class doughter:public father{


public:

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.

Software Used: Code Blocks.


Procedure:
Step 1: Create a class named student.
Step 2: Make a function getdata() to get all information of student.
Step 3: Create main function and make the object of student class.
Step 4: Make an object fio of fstream .
Step 5: Using fio open text file and convert it to binary
file. Step 6: Display output in text format.
Step 7: Using object of student class take inputs from user.

Source Code:
#include<iostream>
#include<fstream>
using namespace std;
class student { int
roll_no, marks;
char name [30];
public:

void get data() { cout<<"\n Enter


roll no.: "; cin>>roll_no; cout<<"\
n Enter name of student: "; cin
>>name; cout<<"\n Enter marks: ";
cin>>mark
2321008

}};

int main() { student stu; fstream fio;


fio.open("file.txt",ios::binary|ios::out);
stu.getdata();
}

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).

Software Used: Code Blocks.


Procedure:
Step 1: Create the struct date to take the dates.
Step 2: Create class hospital to take information of patient.
Step 3: Make display function to display the information of the patient.
Step 4: Make a base class named age to perform conditions.
Step 5: Start the main function.
Step 6: Make objects of each classes and call the function using these objects.

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

cout<<"Enter name of the patient: ";


cin>>name;
cout<<"Enter date of admission: ";
cin>>date_adm.day>>date_adm.month>>date_adm.year;
cout<<"Enter date of discharge: ";
cin>>date_dis.day>>date_dis.month>>date_dis.year;
}
void display()
{
cout<<"Patient name: "<<name;
cout<<"Date of admission: "<<date_adm.day<<date_adm.month<<date_adm.year;
cout<<"Date of discharge: "<<date_dis.day<<date_dis.month<<date_dis.year;
}};

class age: public hospital


{ int a;
public:
void get()
{ cout<<"Enter age:
"; cin>>a;
}
void put()
{ if(a<12)
{ display();
cout<<"age:
"<<a;
}
else
2321008

cout<<"age greater than 12";


}};

int main() { age a1;


a1.getdata();
a1.get();
a1.put(); return 0;
}

Output:

You might also like