Class Array sample

This simple code gives you the area of ten rectangles from the users entered info. Happy Coding!




#include <iostream>

using namespace std;

class Rectangle
{
private:
double width;
double length;

public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLenth() const;
double getArea() const;
};

void Rectangle::setWidth(double w)
{
width=w;
}

void Rectangle::setLength(double len)
{
length=len;
}

double Rectangle::getWidth() const
{
return width;
}

double Rectangle::getLenth() const
{
return length;
}

double Rectangle::getArea() const
{
return width*length;
}

int main()
{
Rectangle box[10];
double rectWidth;
double rectLength;

for(int i=0;i<10;i++)
{
cout<<"Enter Rectangle "<<i+1<<" Width ";
cin>>rectWidth;
cout<<"Enter Rectangle "<<i+1<<" Length ";
cin>>rectLength;
cout<<endl;

box[i].setWidth(rectWidth);
box[i].setLength(rectLength);
}

for(int i=0;i<10;i++)
{
cout<<endl<<endl;
cout<<"Rectangle "<<i+1<<" width = "<<box[i].getWidth()<<endl;
cout<<"Rectangle "<<i+1<<" length = "<<box[i].getLenth()<<endl;
cout<<"Rectangle "<<i+1<<" area = "<<box[i].getArea()<<endl;
}
return 0;
}

This is not the cleanest code, but you can use it as a template for you own needs.
Last edited on
Topic archived. No new replies allowed.