0% found this document useful (0 votes)
88 views5 pages

#Include #Include: Class Private Public

The document defines a Rectangle class with length and width data members and methods to set dimensions, calculate perimeter and area, display rectangle details, and check if two rectangles have the same area. It also includes a main() method to test the class.

Uploaded by

Ronit Deb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views5 pages

#Include #Include: Class Private Public

The document defines a Rectangle class with length and width data members and methods to set dimensions, calculate perimeter and area, display rectangle details, and check if two rectangles have the same area. It also includes a main() method to test the class.

Uploaded by

Ronit Deb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write the definition for a class called Rectangle that has floating point data

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);
};

void Rectangle::setlength(float len)


{
length = len;
}

void Rectangle::setwidth(float wid)


{
width = wid;
}

float Rectangle::perimeter()
{
return (2 * length + 2 * width);
}
float Rectangle::area()
{
return length * width;
}

void Rectangle::show()
{
cout << "Length: " << length << " Width: " << width;
}

int Rectangle::sameArea(Rectangle other)


{
float areaf = length * width;
float areas = other.length * other.width;
if (areaf == areas)
return 1;
return 0;
}

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);

cout<<"\n complex Number 1 = ";C1.disp();


cout<<"\n complex Number 2 = ";C2.disp();
cout<<"\n complex Number 3 = ";C3.disp();
getch();
return 0;
}

You might also like