#Include #Include: Class Private Public
#Include #Include: Class Private Public
members length and width. The class has the following member functions:
void setlength(float) to set the length data member
void setwidth(float) to set the width data member
float perimeter() to calculate and return the perimeter of the rectangle
float area() to calculate and return the area of the rectangle
void show() to display the length and width of the rectangle
int sameArea(Rectangle) that has one parameter of type Rectangle.
sameArea returns 1 if the two Rectangles have the same area, and returns 0
if they don't.
#include<iostream.h>
#include<conio.h>
class Rectangle
{
private:
float length;
float width;
public:
void setlength(float);
void setwidth(float);
float perimeter();
float area();
void show();
int sameArea(Rectangle);
};
float Rectangle::perimeter()
{
return (2 * length + 2 * width);
}
float Rectangle::area()
{
return length * width;
}
void Rectangle::show()
{
cout << "Length: " << length << " Width: " << width;
}
int main()
{
Rectangle first;
Rectangle second;
first.setlength(5);
first.setwidth(2.5);
second.setlength(5);
second.setwidth(18.9);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << "Perimeter: " <<
first.perimeter() << endl << endl;
cout << "Second rectangle: ";
second.show();
cout << endl << "Area: " << second.area() << "Perimeter: " <<
second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
first.setlength(15);
first.setwidth(6.3);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << "Perimeter: "<<
first.perimeter() << endl << endl;
cout << "Second rectangle: ";
second.show();
cout << endl << "Area: " << second.area() << "Perimeter: "<<
second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
getch();
return 0;
}
Write the definition for a class called complex that has floating point data
members for storing real and imaginary parts. The class has the following
member functions:
void set(float, float) to set the specified value in object
void disp() to display complex number object
complex sum(complex) to sum two complex numbers & return complex
number
1. Write the definitions for each of the above member functions.
2. Write main function to create three complex number objects. Set the value
in two objects and call sum() to calculate sum and assign it in third object.
Display all complex numbers.
#include<iostream.h>
#include<conio.h>
class complex
{
private:
float x;
float y;
public:
void set(float real, float img)
{
x=real; y=img;
}
complex sum(complex);
void disp();
};
complex complex::sum(complex C)
{
complex t;
t.x = x + C.x;
t.y = y + C.y;
return t;
}
void complex::disp()
{
cout<<x<<" + j"<<y<<endl;
}
int main()
{
complex C1,C2,C3;
C1.set(2.5,7.1);
C2.set(4.2,5.5);
C3=C1.sum(C2);