#include<iostream.h>
#include<conio.h>
/*….Defining class…*/
class distance
{
int feet ,inches;
public:
void getdata(int f,int i)
{
feet=f;
inches=i;
}
void printit(void)
{
cout<<feet<<”feet”<<inches<<”inches”<<”\n”;
}
distance sum(distance d2); //add distance d2 to current distance object
}; /*…Class End’s…*/
distance distance::sum(distance d2)
{
distance d3; //create object d3 of type distance
//(the function adds passed distance type object to current object)
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches +d2.inches)%12;
return(d3);
}
class distance
{
int feet ,inches;
public:
void getdata(int f,int i)
{
feet=f;
inches=i;
}
void printit(void)
{
cout<<feet<<”feet”<<inches<<”inches”<<”\n”;
}
distance sum(distance d2); //add distance d2 to current distance object
}; /*…Class End’s…*/
distance distance::sum(distance d2)
{
distance d3; //create object d3 of type distance
//(the function adds passed distance type object to current object)
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches +d2.inches)%12;
return(d3);
}
/*…Main Function Begin’s…*/
void main()
{
distance length1,length2,total;
length1.getdata(17,6);
length2.getdata(13,8);
total=length1.sum(length2); //i.e.,total=length1+length2
cout<<”length1:”;
length1.printit();
cout<<”length2:”;
length2.printit();
cout<<”total length:”;
total.printit();
getch();
}
void main()
{
distance length1,length2,total;
length1.getdata(17,6);
length2.getdata(13,8);
total=length1.sum(length2); //i.e.,total=length1+length2
cout<<”length1:”;
length1.printit();
cout<<”length2:”;
length2.printit();
cout<<”total length:”;
total.printit();
getch();
}
/*…Main Function End’s…*/
//the above program adds 2 distance (objects) length1 and length2 to produce
//third distance(object)total.
//the function sum() takes an object of distance type as argument and returns
//an object of distance type containing sum of the passed object and the
//current object through which sum() function is invoked.
//the above program adds 2 distance (objects) length1 and length2 to produce
//third distance(object)total.
//the function sum() takes an object of distance type as argument and returns
//an object of distance type containing sum of the passed object and the
//current object through which sum() function is invoked.
/*…The output is something like this…*/::::::::
No comments:
Post a Comment