In-class programming Practice (4.Feb) Requirement: create one project for each p
ID: 3549849 • Letter: I
Question
In-class programming Practice (4.Feb)
Requirement: create one project for each problem; add comments to your code.
Submission: after finishing the all the problems, paste the code and snapshot the output in one file, submit it to the dropbox.
===================================================
Problem 3
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Rectangle
{
double width, height;
public:
Rectangle ();
Rectangle (double, double);
double sum_w_h();
static int count;
int area (void){return (width*height);}
};
int Rectangle::count = 0;
Rectangle:: Rectangle()
{
count = count + 1 ;
width = 5;
height = 5;
}
Rectangle :: Rectangle (double a, double b)
{
width = a;
height = b;
}
double Rectangle :: sum_w_h()
{
double z = width + height;
return z;
}
int main ()
{
int size =10;
Rectangle rect[10];
double sum =0;
for (int i =0; i<10; i++)
{
sum= sum + rect[i].area();
}
cout << sum << endl;
cout << endl;
cout << Rectangle :: count << endl;
cin.ignore(2);
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Rectangle
{
double width, height;
int centerX,centerY;//Added Attributes
public:
Rectangle ();
Rectangle (double, double);
double sum_w_h();
static int count;
double area (void){return (width*height);}
void move(int x,int y)//Added Function
{
centerX=x;
centerY=y;
}
};
int Rectangle::count = 0;
Rectangle:: Rectangle()
{
count = count + 1 ;
width = 5;
height = 5;
}
Rectangle :: Rectangle (double a, double b)
{
width = a;
height = b;
}
double Rectangle :: sum_w_h()
{
double z = width + height;
return z;
}
int main ()
{
int size =10;
Rectangle rect[10];
double sum =0;
for (int i =0; i<10; i++)
{
sum= sum + rect[i].area();
}
cout << sum << endl;
cout << endl;
cout << Rectangle :: count << endl;
cin.ignore(2);
return 0;
}
Test cases:
1) give width-0 length-0
2)give one of them as negative
3)both negative