Some advice on my code,please

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
using namespace std;

class Rectangle {
public:
Rectangle (double,double);
Rectangle (double,double,double,double);
bool check_square(double, double);
void show_check_sqaure_result(bool);
private:
double x,y;           
double length,width;
};

class Circle {
public:
Circle (Rectangle);
};

Rectangle::Rectangle(double l,double w)
{
length = l;
width = w;
cout<<"The area is "<<length*width<<" unit sqaure."<<endl;
check_square(l,w);
}

Rectangle::Rectangle(double x0,double y0,double x1, double y1)
{
double a = (x1>x0) ? (x1-x0):(x0-x1);
double b = (y1>y0) ? (y1-y0):(y0-y1);
cout<<"The area is "<<a*b<<" unit sqaure."<<endl;
check_square(a,b);
}

bool Rectangle::check_square(double a, double b)
{
double c;
c = a-b;
show_check_sqaure_result(c);
}

void Rectangle::show_check_sqaure_result(bool k)
{
if (k==0)
cout<<"It is a square.";
else
cout<<"It is NOT a square.";
cout<<endl;
}

Circle::Circle(Rectangle)
{
cout<<"The area of respective circle is"<<endl;
}

int main()
{
Rectangle A(4.6,10, 3, 9);
Circle B(A);
Rectangle C(1,1);
Circle D(C);
system("pause");
}


My concern is that in Line 52,the constructor of class Circle. Here I set the parameters as an Rectangle object. How can I design this constructor so that it can only (1) accept and (2) distinguish Rectangle objects with 2 and 4 parameters for separate treatment?

Thanks.
You can't, and you shouldn't.

You can force the issue with class constructor functions, but again, you really shouldn't be trying to do that. A rectangle is a rectangle is a rectangle. If there is a difference between two and four parameters then they should be distinct objects (not both rectangles).

Hope this helps.
Last edited on
Thank you for your advice. I finally got what I wanted.
Topic archived. No new replies allowed.