Sei sulla pagina 1di 31

PROGRAM 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 the result 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 gets values from the user to test this function.

Source code:-

#include<iostream.h>
double power(double,int p);
int main()
{
double pow,n;
char c,ch;
do
{
int p=2;
cout<<"\n enter the number";
cin>>n;
cout<<"\n want to enter an exponent???(y/n)";
cin>>c;
if(c=='y')
{
cout<<"\n enter the exponent";
cin>>p;
pow=power(n,p);
}
else
pow=power(n,p);
cout<<"\n the result is:"<<pow<<endl;
cout<<"\n want to continue???(y/n)";
cin>>ch;
}while(c=='y');
return 0;
}
double power(double n,int p)
{
int i;
double res=1;
if(p==0)
res=1;
for(i=1;i<=p;i++)
res=res*n;
return res;
}

OUTPUT:-





























PROGRAM No. 2

Aim:- A point on the two dimensional plane can be represented by two
numbers: an X coordinate and a Y coordinate. For example, (4,5) represents a
point 4 units to the right of the origin along the X axis and 5 units up the Y axis.
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 coordinate is the sum of
their 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

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
struct dimention
{
int x,y;
};
struct dimention a,b,c;
int main()
{
cout<<"Enter the dimention of a";
cin>>a.x;
cin>>a.y;
cout<<"Enter the dimention of b";
cin>>b.x;
cin>>b.y;
cout<<"Addition of coordinates is:";
c.x=a.x+b.x;
c.y=a.y+b.y;
cout<<c.x<<endl<<c.y;
getch();
return 0;
}

OUTPUT:-






























PROGRAM 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 a 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 first number, operator, second number: 10/ 3
Answer = 3.333333
Do another (Y/ N)? Y
Enter first number, operator, second number 12 + 100
Answer = 112
Do another (Y/ N) ? N

Source Code:-

#include<iostream.h>
#include<conio.h>
int main()
{
char c,ch;
float res;
int a,b;
clrscr();
do
{
cout<<"Enter the first operand";
cin>>a;
cout<<"Enter the operator (+,-,*,/)";
cin>>c;
cout<<"Enter second operand";
cin>>b;
switch(c)
{
case '+':
res=a+b;
cout<<"\n"<<a<<" "<<c<<" "<<b<<"="<<res<<endl;
break;
case '-':
res=a-b;
cout<<"\n"<<a<<" "<<c<<" "<<b<<"="<<res<<endl;
break;
case '*':
res=a*b;
cout<<"\n"<<a<<" "<<c<<" "<<b<<"="<<res<<endl;
break;
case '/':
if(b==0)
cout<<"\n Division by 0 is not allow \n";
else
{
res=a/b;
cout<<"\n"<<a<<" "<<c<<" "<<b<<"="<<res<<endl;
}
break;
default :
cout<<"Enter valid choice.";
break;
}
cout<<"Do u want to continue (y/n)??";
cin>>ch;
}
while(ch=='y');
return 0;
}
















OUTPUT:-




























PROGRAM 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. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
struct ph
{
int area_code,ex_code,ph_code;
}p,pl;
int main()
{
clrscr();
cout<<"Enter the area code";
cin>>p.area_code;
cout<<"Enter the exchange code";
cin>>p.ex_code;
cout<<"Enter the phone no.";
cin>>p.ph_code;
pl.area_code=100;
pl.ex_code=200;
pl.ph_code=3000;
cout<<"Your no. is="<<"("<<p.area_code<<")"<<"-"<<p.ex_code<<"-"<<p.ph_code;
cout<<"\n"<<" My no.is="<<"("<<pl.area_code<<")"<<"-"<<pl.ex_code<<"-
"<<pl.ph_code;
getch();
return 0;
}




OUTPUT:-




























PROGRAM No. 5

Aim:- Create two classes DM and DB which store the value of distances. DM
stores distances in metres and centimetres and DB in feet and inches. Write a
program that can read values for the class objects and add one object of DM
with another object of DB. Use a friend function to carry out the addition
operation. The object that stores the results maybe a DM object or DB object,
depending on the units in which the results are required. The display should be
in the format of feet and inches or metres and centimetres depending on the
object on display.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class DB;
class DM
{
private:
float m;
float cm;
public:
void get();
void display();
friend DM sum1(DM& , DB&);
friend DB sum2(DM& , DB&);
};
void DM::get()
{
cout<<"\nMeter";
cin>>m;
cout<<"centimeter";
cin>>cm;
}
void DM::display()
{
cout<<"\nMeter"<<m;
cout<<"\ncentimeter"<<cm;
}
class DB
{
private:
float feet;
float inch;
public:
void get();
void display();
friend DM sum1(DM& , DB&);
friend DB sum2(DM& , DB&);
};
void DB::get()
{
cout<<"\nFeet";
cin>>feet;
cout<<"\nInch";
cin>>inch;
}
void DB::display()
{
cout<<"\nFeet"<<feet;
cout<<"\nInch"<<inch;
}
DM sum1(DM& dm, DB& db)
{
DM s1;
db.feet=db.feet*0.3;
db.inch=db.inch*2.54;
s1.m=dm.m+db.feet;
s1.cm=dm.cm+db.inch;
return(s1);
}
DB sum2(DM& dm, DB& db)
{
DB s2;
dm.m=dm.m/0.3;
dm.cm=dm.cm/2.54;
s2.feet=db.feet+dm.m;
s2.inch=db.inch+dm.cm;
return(s2);
}
int main()
{
clrscr();
DM dm;
DB db;
DM s1;
DB s2;
int c;
cout<<"\nEnter values";
dm.get();
cout<<endl;
db.get();
dm.display();
cout<<"\n\nEnter\n'1' to print result in meter and centimeter";
cout<<"\n'2' to print result in feet and inches";
cin>>c;
switch(c)
{
case 1:
s1=sum1(dm,db);
s1.display();
break;
case 2:
s2=sum2(dm,db);
s2.display();
break;
default:
cout<<"Wrong value is taken";
return 0;
}
getch();
return 0;
}













OUTPUT:-
















PROGRAM No. 6

Aim:- Create a class rational which represents a numerical value by two
double values- NUMERATOR & DENOMINATOR. Include the following public
member Functions:
constructor with no arguments (default).
constructor with two arguments.
void reduce( ) that reduces the 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.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class rational
{
private:
double num;
double denom;
double hcf;
public:
rational()
{
num=0;
denom=0;
}
rational(double n, double d)
{
num=n;
denom=d;
}
void get();
void display();
void reduce();
};
void rational::get()
{
cout<<"\nEnter the numerator:";
cin>>num;
cout<<"\nEnter the denominator:";
cin>>denom;
}
void rational::display()
{
cout<<"\nThe rational number is:"<<num<<"/"<<denom;
}
void rational::reduce()
{
double n,d;
n=num;
d=denom;
while(n!=d)
{
if(n>d)
n=n-d;
else
d=d-n;
}
hcf=n;
num=num/hcf;
denom=denom/hcf;
}

int main()
{clrscr();
rational r;
cout<<"\nAfter calling default constructor with no argument:";
r.display();
rational s(20,9);
cout<<"\nAfter calling default constructors with two arguments:";
s.display();
s.get();
s.display();
cout<<"\nAfter reducing:";
s.reduce();
s.display();
getch();
return 0;
}
OUTPUT:-



























PROGRAM No. 7

Aim:- A program for operator overloading using friend function for
extraction and insertion.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class integer
{
private:
int x;
public:
friend istream&operator>>(istream &is, integer &in)
{
is>>in.x;
return is;
}
friend ostream&operator<<(ostream &os, integer &in)
{
os<<in.x;
return os;
}
integer operator+(integer &m)
{
integer t;
t.x=x+m.x;
return t;
}
};
int main()
{
clrscr();
integer a,b,c;
cout<<"Enter value of a";
cin>>a;
cout<<"Enter value of b";
cin>>b;
c=a+b;
cout<<"\nSum=";
cout<<c;
getch();
return 0;
}
OUTPUT:-






























PROGRAM No. 8

Aim:- Make a class Employee with a name and salary. Make a class Manager
inherit from Employee. Add an instance variable, named department, of type
string. Supply a method to toString that prints the managers name,
department and salary. Make a class Executive inherit from Manager. Supply a
method to String that prints the string Executive followed by the
information stored in the Manager superclass object. Supply a test program
that tests these classes and methods.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class Employee
{
protected:
char name[20];
long int salary;
public:
void get()
{
cout<<"\nEnter Employee Name:";
cin>>name;
cout<<"\nEnter salary:";
cin>>salary;
}
void display()
{
cout<<"\nName:"<<name;
cout<<"\nSalary:"<<salary;
}
};
class Manager:public Employee
{
protected:
char dept[20];
public:
void get()
{
Employee::get();
cout<<"\nEnter Department:";
cin>>dept;
}
void twostring()
{
Employee::display();
cout<<"\nDepartment:"<<dept;
}
};
class Executive:public Manager
{
public:
void get()
{
Manager::get();
}
void twostring()
{
cout<<"\nExecutive:";
Manager::twostring();
}
};
int main()
{ clrscr();
Executive ex;
ex.get();
ex.twostring();
getch();
return 0;
}














OUTPUT:-




















PROGRAM No. 9

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.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class father
{
protected:
int age;
public:
father(int x=0)
{
age=x;
}
virtual void iam()
{
cout<<"\nI AM THE FATHER, my age is :"<<age;
}
};
class son: public father
{
public:
son(int y=0)
{
age=y;
}
virtual void iam()
{
cout<<"\nI AM THE SON, my age is:"<<age;
}
};
class daughter: public father
{
public:
daughter(int z=0)
{
age=z;
}
virtual void iam()
{
cout<<"\nI AM THE DAUGHTER, my age is:"<<age;
}
};
int main()
{
clrscr();
son s(18);
daughter d(15);
father f(50);
f.iam();
s.iam();
d.iam();
cout<<"\n\nCalling by Pointers..."<<endl;
father *p;
p=&f;
p->iam();
p=&s;
p->iam();
p=&d;
p->iam();
getch();
return 0;
}



OUTPUT:-























PROGRAM No. 11

Aim:- Imagine a tollbooth with a class called toll Booth. The two data items
are a type unsigned int to hold the total number of cars, and a type double to
hold the total amount of money collected. A constructor initializes both these
to 0. A member function called payingCar ( ) increments the car total and adds
0.50 to the cash total. Another function, called nopayCar ( ), increments the car
total but adds nothing to the cash total. Finally, a member function called
displays the two totals. Include a program to test this class. This program
should allow the user to push one key to count a paying car, and another to
count a nonpaying car. Pushing the ESC kay should cause the program to print
out the total cars and total cash and then exit.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class tollBooth
{
private:
unsigned int tot_cars;
double tot_amt;
public:
tollBooth()
{
tot_cars=0;
tot_amt=0;
}
void payingCar()
{
tot_cars++;
tot_amt+=0.5;
}
void nopayCar()
{
tot_cars++;
}
void display()
{
cout<<"\nTotal numbers of cars:"<<tot_cars;
cout<<"\nTotal amount:"<<tot_amt;
}
};
int main()
{ clrscr();
tollBooth tb;
char ch;
do
{
cout<<"\nPress 'p' for paying cars,'n' for non paying cars and 'e' to display:";
cin>>ch;
switch(ch)
{
case 'p':
tb.payingCar();
break;
case 'n':
tb.nopayCar();
break;
case 'e':
tb.display();
break;
} }
while(ch!='e');
getch();
return 0;
}


















OUTPUT:-
























PROGRAM No. 12

Aim:- Create a base class called shape. Use this class to store two double type
values that could be used to compute the area of figures. Derive two specific
classes called triangle and rectangle from the base shape. Add to the base
class, a member function get_data ( ) to initialize baseclass data members and
another member function display_area ( ) to compute and display the area of
figures. Make display_area ( ) as a virtual function and redefine this function in
the derived classes to suit their requirements. Using these three classes, design
a program that will accept dimensions of a triangle or a rectangle interactively
and display the area.

SOURCE CODE:-

#include<iostream.h>
#include<conio.h>
class shape
{
protected :
double x;
double y;
public:
shape()
{
x=0;
y=0;
}
void get();
virtual void display()=0;
};
void shape::get()
{
cout<<"\n Enter the dimension...\n";
cout<<"x:";
cin>>x;
cout<<"y";
cin>>y;
}class triangle:public shape
{
private:
double area;
public:
void display();
};
void triangle::display()
{
area=(x*y)/2;
cout<<"Area of the triangle:"<<area;
}
class rectangle:public shape
{
private:
double area;
public:
void display();
};
void rectangle::display()
{
area=x*y;
cout<<"Area of the rectangle:"<<area;
}
int main()
{
rectangle r;
triangle t;
char ch;
int flag=0;
do
{
cout<<"\n\nChoose the shape whose area has to be calculated or press 'E' to exit...";
cout<<"\n1. Triangle.";
cout<<"\n2. Rectangle.\n";
cin>>ch;
switch(ch)
{
case'1':t.get();
t.display();
break;
case'2':r.get();
r.display();
break;
case'e':cout<"\nAorting!!!";
flag=1;
break;
}
if(flag)
{
break;
}
}
while((ch!='E')||(ch!='e'));
return 0;
}



































OUTPUT:-

Potrebbero piacerti anche