what's the problem?
Feb 9, 2011 at 3:33am 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
#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.
Feb 9, 2011 at 3:37am UTC
1 2
cout << mysquare [i].getarea() <<"t" ;
cout << mysquare[i].getPerimeter() <<endl; // getarea <-> getarea(), getPerimeter <-> getPerimeter()
Feb 9, 2011 at 3:54am UTC
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();
}
Feb 9, 2011 at 9:00am UTC
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]
Feb 9, 2011 at 2:28pm UTC
so how to fix it?
Feb 9, 2011 at 4:21pm UTC
You should set it to some standard value, like 0.
Topic archived. No new replies allowed.