[go: up one dir, main page]

0% found this document useful (0 votes)
17 views3 pages

Classes

The document contains C++ code defining two classes, Circle and Rectangle, each with methods to set dimensions, calculate area, and perimeter. The main function demonstrates the usage of these classes by creating instances, setting their dimensions, and outputting their properties. Error handling is included to ensure only positive values are accepted for dimensions.

Uploaded by

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

Classes

The document contains C++ code defining two classes, Circle and Rectangle, each with methods to set dimensions, calculate area, and perimeter. The main function demonstrates the usage of these classes by creating instances, setting their dimensions, and outputting their properties. Error handling is included to ensure only positive values are accepted for dimensions.

Uploaded by

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

Creating Class Circle

class circle
{
private:
float radius;
public:
void set_radius (float r)
{
if (r>=0)
radius = r;
else cout<<"Error, please enter positive value";
}

float get_area()
{
return (3.14*radius*radius);
}
float get_radius()
{
return radius;
}
float get_prem()
{
return (2*3.14*radius);
}
};
int main()
{
circle c1;
circle c2;
c1.set_radius (5);
c2.set_radius (10);
cout<<"the first circle radius = "<<c1.get_radius()<<"\n";
cout<<"the second circle radius = "<<c2.get_radius()<<"\n";
cout<<"the first circle area = "<<c1.get_area()<<"\n";
cout<<"the second circle area = "<<c2.get_area()<<"\n";
cout<<"the first circle prem. = "<<c1.get_prem()<<"\n";
cout<<"the second circle prem. = "<<c2.get_prem()<<"\n";
}
Creating Class Rectangle

class rectangle
{
private:
float width,length;
public:
void set_length (float l)
{
if (l>=0)
length = l;
else cout<<"Error, please enter positive value";
}
void set_width (float w)
{
if (w>=0)
width = w;
else cout<<"Error, please enter positive value";
}
float get_area()
{
return width*length;
}
float get_length()
{
return length;
}
float get_width()
{
return width;
}
float get_prem()
{
return (2*(width+length));
}
};
int main()
{
rectangle r1;
rectangle r2;
rectangle r3;
float x,y;
cout<<"enter r3 length\n";
cin>>x;
cout<<"\n enter r3 width\n";
cin>>y;
r1.set_length (5);
r1.set_width (3);
r2.set_length (8);
r2.set_width (6);
r3.set_length (x);
r3.set_width (y);
cout<<"the first rectangle length = "<<r1.get_length()<<"\n";
cout<<"the second rectangle length = "<<r2.get_length()<<"\n";
cout<<"the first rectangle width = "<<r1.get_width()<<"\n";
cout<<"the second rectangle width = "<<r2.get_width()<<"\n";
cout<<"the first rectangle area = "<<r1.get_area()<<"\n";
cout<<"the second rectangle area = "<<r2.get_area()<<"\n";
cout<<"the first rectangle prem = "<<r1.get_prem()<<"\n";
cout<<"the second rectangle prem = "<<r2.get_prem()<<"\n";
cout<<"the third rectangle length = "<<r3.get_length()<<"\n";
cout<<"the third rectangle width = "<<r3.get_width()<<"\n";
cout<<"the third rectangle area = "<<r3.get_area()<<"\n";
cout<<"the third rectangle prem = "<<r3.get_prem()<<"\n";
}

You might also like