what's the problem?

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
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<cmath>

using namespace std;

class square{
private: 
double side;
double area;
double perimeter;
void calcArea(void) {area = side * side;}
void calperimeter(void) {perimeter = side *4;}
public:
void setData (double G);
double getside(void) {return side;}
double getarea(void) {return area;}
double getPerimeter(void) {return perimeter;}
square(void) {cout<<"we should not be here in inclement weather!"<<endl;}
square(double G) {setData (G);}
~square(void) {cout<<"it is ludicrous when we have class in inclement weather."<<endl;}
};
void square::setData (double G  = 4)
{
side = ((G >= 0) ? G: abs(G));
calcArea();
calperimeter();
}
int main (void) {
const short unsigned int SIZE = 4;
square mysquare[SIZE] = {square (2), square (5)};
for ( int i = 0; 0 < SIZE; i++) {
	cout << mysquare [i].getarea <<"t";
	cout << mysquare[i].getPerimeter<<endl;
}
cout<< "that’s all!"<<endl;
}


the last cout's statement have some error.
1
2
cout << mysquare [i].getarea <<"t";
	cout << mysquare[i].getPerimeter<<endl;


it said square.getarea; function call missing arugment list use&square to creat a point to member.
same for sqaure.getperimter.
closed account (3hM2Nwbp)
1
2
        cout << mysquare [i].getarea() <<"t";
	cout << mysquare[i].getPerimeter() <<endl; // getarea <-> getarea(), getPerimeter <-> getPerimeter() 
i changed that, and it seems my trap for making negative input to positive input have something wrong.
void square::setData (double G = 4)
{
side = ((G >= 0) ? G: abs(G));
calcArea();
calperimeter();
}
jimmy5023 wrote:
making negative input to positive input have something wrong.
Nope. Even thought side = ((G >= 0) ? G: abs(G)); could be side = abs(G); or side = ((G >= 0) ? G: (-G));

Your problem is the constructor on line 20 doesn't initialize it's member variables. So you get random numbers for mysquare[2] and mysquare[3]
so how to fix it?
You should set it to some standard value, like 0.
Topic archived. No new replies allowed.