You are on page 1of 19

Object Oriented Programming & Methodology

CS-305

PRACTICAL FILE

SUBMITTED TO:- SUBMITTED BY:-


Mrs.Reena Chauhan Aman Jha
Asst.Professor 0905CS181019
Deptt. Of CS/IT CS-'A' 3rd Sem

1
INDEX
S.No. Program Page No.
1 Program to display the
following output using 3-4
single cout statement

2 Program that can read


values for the class 5-8
objects and add one
object DM with another
object of DB. Use a Friend
Function to carry out the
addition operation

3 Program in C++ to create


a class FLOAT that 9-11
contains one float data
member

4 Program in c++ power()


to raise a number m to a 12-13
power n.

5 Program in c++ for a class


to represent a bank 14-17
account

6 Program in c++ to a
creates a vector of user 18-19
given size M using new
operator.

2
Q1.Program to display the following output using single
cout statement Maths =90
Physics=77
Chemistry=69

#include<conio.h>
#include<iostream.h> void
main()
{ clrscr();
cout<<"OUTPUT PROGRAM\n\n\n";
cout<<"\tMaths = 90\n"<<"\tPhysics
= 77\n"<<"\tChemistry = 69\n"; getch();
}

3
4
Q2. Program that can read values for the class objects and add
one object DM with another object of DB. Use a Friend Function
to carry out the addition operation. The object that stores the
results may be a DM object or DB object, depending on the units
in which the
#include<iostream.h>
#include<conio.h>
class DB; class DM {
float meter,centi;
public:
void getdata()
{
cout<<"\nEnter the distance in(meter-centimeter):";
cin>>meter>>centi;
}
void display()
{
cout<<"\nThe distance is:";
cout<<meter<<" meters and "<<centi<<" centimeter";
}
friend void add(DM &,DB &);
}; class DB {
float inch,feet;
public:
void getdata()
{
cout<<"\nEnter the distance in(feet-inch):";
cin>>feet>>inch;
}
void display()
{
cout<<"\nThe distance is:";
cout<<feet<<" feet and "<<inch<<" inch"; }
friend void add(DM &,DB &);
};

5
void add(DM &a,DB &b)
{ int
ch;
cout<<"\nPress 1 for meter-centi:";
cout<<"\nPress 2 for feet-inch:";
cout<<"\nEnter your choice:";
cin>>ch; if(ch==1) { DM d;
int c=(a.meter*100+a.centi+b.feet*30.48+b.inch*2.54);
if(c>=100)
{
d.meter=c/100;
d.centi=c%100;
} else
{
d.meter=0;
d.centi=c;
}
d.display();
} else {
DB d; int
i=(a.meter*39.37+a.centi*.3937008+b.feet*12+b.inch);
if(i>=12)
{
d.feet=i/12;
d.inch=i%12;
} else
{
d.feet=0;
d.inch=i;
}
d.display();
} } void
main() {
clrscr();
DM a; DB
b;
a.getdata();

6
b.getdata();
add(a,b);
getch();
}

7
8
Q3. Program in C++ to create a class FLOAT that contains one float data
member. Overload all the four arithmetic operators so that they
operate on the objects of FLOAT.
#include<iostream.h>
#include<conio.h>

class FLOAT
{ float no;
public:
FLOAT(){} void
getdata()
{
cout<<"\n ENTER AN FLOATING NUMBER :";
cin>>no; } void putdata() {
cout<<"\n\nANSWER IS :"<<no;
}
FLOAT operator+(FLOAT);
FLOAT operator*(FLOAT);
FLOAT operator-(FLOAT);
FLOAT operator/(FLOAT);
};
FLOAT FLOAT::operator+(FLOAT a)
{
FLOAT temp;
temp.no=no+a.no; return
temp;
}
FLOAT FLOAT::operator*(FLOAT b)
{
FLOAT temp;
temp.no=no*b.no; return
temp;
}
FLOAT FLOAT::operator-(FLOAT b)
{
FLOAT temp;
temp.no=no-b.no; return
temp;
}
FLOAT FLOAT::operator/(FLOAT b)
{

9
FLOAT temp;
temp.no=no/b.no; return
temp;
}
main() {
clrscr();
FLOAT a,b,c;
a.getdata();
b.getdata();
c=a+b;
cout<<"\n\nAFTER ADDITION OF TWO OBJECTS";
c.putdata();
cout<<"\n\nAFTER MULTIPLICATION OF TWO OBJECTS";
c=a*b;
c.putdata();
cout<<"\n\nAFTER SUBSTRACTION OF TWO OBJECTS";
c=a-b;
c.putdata();
cout<<"\n\nAFTER DIVISION OF TWO OBJECTS";
c=a/b;
c.putdata(); getch();
}

10
11
Q4. Program in c++ power() to raise a number m to a power n.
The function takes a double value for m and int value for n and returns
the result correctly. Use a default value of 2 for n to make the function
to calculate squares when this argument is omitted. Write a main that
gets the values of m and n from the user to test the function.

#include<iostream.h>
#include<conio.h> #include<math.h>
double power(double m,int n=2)
{ double t;
t=pow(m,n);
return t; }
void main() {
clrscr();
double num,ans;
int p;
cout<<"Enter the number: \n"; cin>>num;
cout<<"\n enter the power:";
cin>>p; if(p==0) {
ans=power(num);
cout<<"\nPower of "<<num<<" is "<<ans<<endl;
} else
{
ans=power(num,p);
cout<<"\nPower of"<<num<<" is"<<ans<<endl;
} getch();
}

12
13
Q5. Program in c++ for a class to represent a bank account.
#include<iostream.h>
#include<conio.h> class
Bank
{private: int
acno; char
name[30]; long
balance;
public:
void OpenAccount()
{cout<<"Enter Account Number:";
cin>>acno; cout<<"Enter Name:";
cin>>name; cout<<"Enter
Balance:"; cin>>balance;}
void ShowAccount()
{ cout<<"Account Number:"<<acno<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Balance:"<<balance<<endl;
}
void Deposit()
{ long amt;
cout<<"Enter Amount U want to deposit?";
cin>>amt; balance=balance+amt;
}
void Withdrawal()
{ long amt;
cout<<"Enter Amount U want to withdraw?";
cin>>amt; if(amt<=balance)
balance=balance-amt; else
cout<<"Less Balance..."<<endl;
}
int Search(int);
};
int Bank::Search(int a)
{if(acno==a)
{ShowAccount();
return(1);} return(0);
}
void main() {Bank
C[3]; clrscr();
int found=0,a,ch,i;
for(i=0;i<=2;i++)
{C[i].OpenAccount();

14
}
do{
clrscr();
cout<<"1:Display All\n2:By Account
No\n3:Deposit\n4:Withdraw\n5:Exit"<<endl;
cout<<"Ur Choice?";
cin>>ch;
switch(ch) {case 1:
for(i=0;i<=2;i++)
{C[i].ShowAccount();}
break; case 2:
cout<<"Account Number?";
cin>>a;
for(i=0;i<=2;i++)
{found=C[i].Search(a);
if(found) break;
}
if(!found)
cout<<"Record Not Found"<<endl;
break;
case 3:
cout<<"Account Number To Deposit
Amount?"; cin>>a;
for(i=0;i<=2;i++) {found=C[i].Search(a);
if(found){
C[i].Deposit();
break;
}
}
if(!found) cout<<"Record
Not Found"<<endl; break; case
4:
cout<<"Account Number To Withdraw Amount?";
cin>>a; for(i=0;i<=2;i++)
{found=C[i].Search(a);
if(found){
C[i].Withdrawal();
break; }
}
if(!found)
cout<<"Record Not Found"<<endl;
break;
case 5:
cout<<"Have a nice day"<<endl;

15
break;
default:
cout<<"Wrong Option"<<endl;
}
getch();
}while(ch!=5);

16
17
Q6.Program in c++ to write a function that creates a vector
of user given size M using new operator.
#define null 0
#include <iostream.h>
#include <conio.h>

void main() {
void memory(int); clrscr();
cout<<"Enter Memory M you Want to create:-";
int size; cin>>size; memory(size); getch();
}
void memory(int s)
{
int *m = new int[s];
if(m!=null) {
cout<<"\nWe are Successfull";
cout<<"\n\n\n\n\tNow You Want to Delete This Created Memory";
cout<<"\n\n\tEnter Y or y for Deleting else anything:-"; char
ch; cin>>ch;
if(ch=='y' || ch=='Y')
{
delete[]m;
cout<<"\n\n\n\tCreated Memory is Delete";
}
else cout<<"\n\n\tOK, your Memory
is Safe";
} else
cout<<"\nWe are UN-Successfull";
}

18
19

You might also like