Adding side length to task for square
Oct 21, 2013 at 1:59pm UTC
I am suppose to:
Create an instance of Square, called s1,with a side length of 5.
Print the side length of s1.
Print the area of s1.
Print the perimeter of s1.
Got it printing but I can't figure how how to set side length to 5.
1 2 3 4 5 6
Square* s1 = new Square();
cout << "Details of s1:" << endl;
cout << "side length: " << s1->getSideLength() <<endl;
cout << "area: " << s1->getArea() << endl;
cout << "perimeter:" << s1->getPerimeter() << endl;
cout << " " << endl;
Oct 21, 2013 at 2:10pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# include <iostream>
using namespace std;
# define pi 3.14
class square
{
int side;
public :
square(void ){side = 0;};
square(int s){side = s;}
int getSideLength(){return side;}
float getArea(){return side*side*pi;}
float getPerimeter(){return 2*side*pi;}
};
int main()
{
square* s1 = new square();
cout << "Details of s1:" << endl << endl;
cout << "side length: " << s1->getSideLength() <<endl;
cout << "area: " << s1->getArea() << endl;
cout << "perimeter:" << s1->getPerimeter() << endl;
cout << " " << endl;
delete s1;// Dont forget to delete
square* s2 = new square(5);
cout << "Details of s2:" << endl << endl;
cout << "side length: " << s2->getSideLength() <<endl;
cout << "area: " << s2->getArea() << endl;
cout << "perimeter:" << s2->getPerimeter() << endl;
cout << " " << endl;
delete s2;
return 0;
}
Hope that helps
Topic archived. No new replies allowed.