Classes

Hello Everyone!!

I am working on a general Class for a problem of mine and I ran into a problem. In my setData function, I am supposed to set it up so that it takes a default value of 4.0 in its parameter list, and if a negative value is passed to the function, the side of the square is set to the absolute value of this negative value. If someone could possibly help me that would it be great thanks!! Here is what I have so far:

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>

using namespace std;

class Square {
private:
	double side;
	double area;
	double perimeter;
	void calcArea() {area=side*side;}
	void calcPerim() {perimeter=4*side;}
public:
	void setData(double ss = 4.0);
	double getSide() const {return side;}
	double getArea() const {return area;}
	double getPerimeter() const {return perimeter;}
	Square() {cout << "\nWe should not be here in this inclement weather!\n";  setData();}
	Square(double param) {setData(param);}
	~Square() {cout << "\nIt is ludicrous when we have class in inclement weather.\n";}
};

void Square::setData(double ss = 4.0)
{
	side = ss;
	calcArea();
	calcPerim();
}

int main()
{
	const short unsigned int SIZE = 4;
	Square mySquare[SIZE] = {Square(2), Square(5)};
	for (int i=0; i<SIZE; i++) {
		cout << mySquare[i].getArea() << "t";
		cout << mySquare[i].getPerimeter() << endl;
	}
	cout << "That's all\n";
}

closed account (1yR4jE8b)
Are you getting any compiler errors? If yes, what are they?

Just from what you've said, you properly implement default arguements in your setData() functions, although I have no idea why you would want a default argument for a mutator method.

If you want the absolute value, just check to see if the number is negative, and if it is just multiply it by -1.
1
2
if(side < 0)
    side *= -1;
Last edited on
The compiler error at this point is ""error C2572: 'Square::setData' : redefinition of default parameter : parameter 1"". I have no idea why I am getting this error though.
You can only put the default parameter either the definition or the declaration, not both.
@firedraco

Thanks for the help!
Topic archived. No new replies allowed.