Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Question#1:

Define a class for rectangle objects defined by two points, the top-left and
bottom-right corners of the rectangle. Include a constructor to copy a rectangle, a
method to return a rectangle object that encloses the current object and the rectangle
passed as an argument, and a method to display the defining points of a rectangle. Test
the class by creating four rectangles and combining these cumulatively to end up with a
rectangle enclosing them all. Output the defining points of all the rectangles you create.

Code:

#include<iostream>
using namespace std;
class rectangle
{
int top_left ;
int bottom_right ;
public:
rectangle () : top_left(0) , bottom_right( 0)
{
}
rectangle( int tl, int br) : top_left( tl) , bottom_right( br)
{
}
void show()
{
cout<<"\n";
cout<<top_left<<" and "<<bottom_right<<" are top_left and bottom right " <<endl;
}
void add_rectangle ( rectangle r1 , rectangle r2 , rectangle r3 , rectangle r4)
{
top_left = r1.top_left + r2.top_left + r3.top_left + r4.top_left;
bottom_right = r1.bottom_right + r2.bottom_right + r3.bottom_right + r4.bottom_right;
}
};

int main()
{
rectangle r5;
rectangle r1(4,8), r2(9,10) ,r3(1,5), r4(9,6);
cout<<"\n Displaying points of 4 rectangles "<<endl;
r1.show();
r2.show();
r3.show();
r4.show();
cout<<"\n Rectangle Eclosing all points "<<endl;
r5.add_rectangle(r1,r2,r3,r4);
r5.show();
rectangle r6( r5);
cout<<"\n After copying Data "<<endl;
r6.show();
return 0;
}

Output:
Question#2:
Define a class, mcmLength, to represent a length measured in meters,
centimeters, and millimeters, each stored as integers. Include methods to add and
subtract objects, to multiply and divide an object by an integer value, to calculate an area
resulting from the product of two objects, and to compare objects. Include constructors
that accept three arguments—meters, centimeters, and millimeters; one integer
argument in millimeters; one double argument in centimeters; and no arguments, which
create an object with the length set to zero. Check the class by creating some objects and
testing the class operations.
Code:

#include<iostream>
using namespace std;
class mcmLength
{
private:
int m;
int cm;
int mm;
public:
mcmLength(int met, int centi, int mili)
{
m=met;
cm=centi;
mm=mili;
}
mcmLength(int mili)
{
m=mili/1000;
cm= (mili-m*1000)/10;
mm= mili-m*1000-cm*10;
}
mcmLength(double centi)
{
m=centi/1000;
cm= (centi-m*1000);
mm=0;
}
mcmLength()
{
m=cm=mm=0;
}
void showdata(int x,int y,int z)
{
m=x;
cm=y;
mm=z;
cout<<"\nLength is "<<m<<"m "<<cm<<"cm "<<mm<<"mm "<<endl;
}
void add_data(mcmLength L1,mcmLength L2)
{

m=L1.m+L2.m ;
cm=L1.cm+L2.cm;
mm=L1.mm+L2.mm;
cout<<"\nAdding Two objects : "<<m<<"m "<<cm<<"cm"<<mm<<"mm"<<endl;
}
void sub_data(mcmLength L1,mcmLength L2)
{

m=L1.m-L2.m ;
cm=L1.cm-L2.cm;
mm=L1.mm-L2.mm;
cout<<"\nSubtracting Two objects : "<<m<<"m "<<cm<<"cm"<<mm<<"mm"<<endl;
}
void mul_data(mcmLength L1,mcmLength L2)
{

m=L1.m*L2.m ;
cm=L1.cm*L2.cm;
mm=L1.mm*L2.mm;
cout<<"\nMultiplying Two objects : "<<m<<"m "<<cm<<"cm"<<mm<<"mm"<<endl;
}
void div_data(mcmLength L1,mcmLength L2)
{

m=L1.m/L2.m ;
cm=L1.cm/L2.cm;
mm=L1.mm/L2.mm;
cout<<"\nDividing Two objects : "<<m<<"m "<<cm<<"cm"<<mm<<"mm"<<endl;
}
mcmLength area(mcmLength L, mcmLength L1)
{
mcmLength Area;
int a,b,c;
a=L.m*1000+L.cm*10+L.mm;
b=L1.m*1000+L1.cm*10+L1.mm;
c=a*b;
Area.m=c/1000;
Area.cm=(c-Area.m*1000)/10;
Area.mm=c-Area.mm*1000-Area.cm*10;
//return Area,c;
cout<<"Area of both lengths in mcm format : ";
Area.disp(Area);

}
void disp(mcmLength f3)
{
cout<<f3.m<<'\"'<<f3.cm<<'\'';
cout<<f3.mm;
}
mcmLength compare(mcmLength L, mcmLength L1)
{
if(L.m>L1.m)
{
cout<<"\nThe bigger length is ";
L.disp(L);
}
else if (L.m==L1.m)
{
if(L.cm>L1.cm)
{
cout<<"\nThe bigger length is ";
L.disp(L);
}
else if(L.cm==L1.cm)
{
if(L.mm>L1.mm)
{
cout<<"\nThe bigger length is ";
L.disp(L);
}
else
if(L.mm==L1.mm)
cout<<"\nBoth lengths are equal";
}
}
else{
cout<<"\nThe bigger length is ";
L1.disp(L1);}
}
};
int main()
{
mcmLength L;
mcmLength L1;
mcmLength L2;
L.showdata(7,50,60);
L1.showdata(5,20,10);
L2.add_data(L,L1);
L2.sub_data(L,L1);
L2.mul_data(L,L1);
L2.div_data(L,L1);
L2.area(L,L1);
L2.compare(L,L1);
}

Output:
Question#3:
Define a class, tkgWeight, to represent a weight in tons, kilograms, and
grams, and include a similar range of methods and constructors as the previous example.
Demonstrate this class by creating and combining some class objects.

Code:

#include<iostream>
using namespace std;
class tkgweight
{
private:
float tons;
float kilograms;
float grams;
public:
tkgweight():tons(0),kilograms(0),grams(0)
{

}
void showdata(int x,int y,int z)
{
tons=x;
kilograms=y;
grams=z;
cout<<"\nWeight is "<<tons<<"tons "<<kilograms<<"Kg "<<grams<<"gm"<<endl;
}
void add_data(tkgweight W,tkgweight W1)
{

tons=W.tons+W1.tons ;
kilograms=W.kilograms+W1.kilograms;
grams=W.grams+W1.grams;
cout<<"\nAfter adding two weights the weight is : "<<tons<<"tons
"<<kilograms<<"Kg "<<grams<<"gm"<<endl;
}
void sub_data(tkgweight W,tkgweight W1)
{

tons=W.tons-W1.tons ;
kilograms=W.kilograms-W1.kilograms;
grams=W.grams-W1.grams;
cout<<"\nAfter subtracting two weights the weight is : "<<tons<<"tons
"<<kilograms<<"Kg "<<grams<<"gm"<<endl;
}
void mul_data(tkgweight W,tkgweight W1)
{

tons=W.tons*W1.tons ;
kilograms=W.kilograms*W1.kilograms;
grams=W.grams*W1.grams;
cout<<"\nAfter multiplying two weights the weight is : "<<tons<<"tons
"<<kilograms<<"Kg "<<grams<<"gm"<<endl;
}
void div_data(tkgweight W,tkgweight W1)
{
tons=W.tons/W1.tons ;
kilograms=W.kilograms/W1.kilograms;
grams=W.grams/W1.grams;
cout<<"\nAfter dividing two weights the weight is : "<<tons<<"tons
"<<kilograms<<"Kg "<<grams<<"gm"<<endl;
}
tkgweight area(tkgweight W, tkgweight W1)
{
tkgweight Area;
int a,b,c;
a=W.tons*1000+W.kilograms*10+W.grams;
b=W1.tons*1000+W1.kilograms*10+W1.grams;
c=a*b;
Area.tons=c/1000;
Area.kilograms=(c-Area.tons*1000)/10;
Area.grams=c-Area.tons*1000-Area.kilograms*10;
cout<<"\nArea of both Weight in ton/kg/gm format : ";
Area.disp(Area);
}
void disp(tkgweight f3)
{
cout<<f3.tons<<"tons "<<f3.kilograms<<"kg ";
cout<<f3.grams<<"gm ";
}
tkgweight compare_data(tkgweight W, tkgweight W1)
{
if(W.tons>W1.tons)
{
cout<<"\nThe bigger length is ";
W.disp(W);
}
else if (W.kilograms==W1.kilograms)
{
if(W.kilograms>W1.kilograms)
{
cout<<"\nThe bigger length is ";
W.disp(W);
}
else if(W.kilograms==W1.kilograms)
{
if(W.grams>W1.grams)
{
cout<<"\nThe bigger length is ";
W.disp(W);
}
else
if(W.grams==W1.grams)
cout<<"\nBoth lengths are equal";
}
}
else
{
cout<<"\nThe bigger length is ";
W1.disp(W1);
}
}
};
int main()
{
tkgweight W;
tkgweight W1;
tkgweight W2;
W.showdata(7,50,60);
W1.showdata(5,20,10);
W2.add_data(W,W1);
W2.sub_data(W,W1);
W2.mul_data(W,W1);
W2.div_data(W,W1);
W2.area(W,W1);
W2.compare_data(W,W1);
}

Output:

You might also like