Issue with scale function in class square

I've been trying to implement the void scale function in class square but still did not managed to get it. The scale function and radius function is required to get the output. Can someone show me the way? Note: main() cannot be modified in any way. Thanks!

Output:
- Square::scale test failed
12.0205 89.5415
0.990527 0.458577

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class IScalable
{
	public:
	virtual void scale(float factor)=0;
};

class Object
{
public:

private:
	float d;

public:
	Object(float n) : d(n){}
struct PointType
{
	float x2;
	float y2;
	PointType( float x1,  float y1) :x2(x1),y2(y1){}
	PointType(){}
	float x()
	{
		return x2;
	}
	float y()
	{
		return y2;
	}
	PointType center()
	{
	return *this;
	}
};
class Point :public Object 
{
 
private:
    PointType mpoint;
	
public:

    Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
};
 class Circle : public Point, public IScalable
{ 
	private:
	Object::PointType m_pt;
 	float r;
 	public:  
	
	Circle(const PointType pts, float rad, float dep) 
	: m_pt(pts),r(rad),Point(m_pt,dep) {}
	
	float radius()
	{
		return r;
	}
	
	Circle center()
	{
		return *this;	
		
	};
	 float x()
	{
		return m_pt.x2;

	}
	  float y()
	{
		return m_pt.y2;
	}
        void scale(float scale) override
	{
		 r*=scale;		
	}
};
class Square: public Circle 

	private:
	Object::PointType s_pt;
	Object::VectorType v_pt;
	float a;
	
	public:
	Square(const PointType spts,const VectorType vpts,float depth) :
	s_pt(spts),v_pt(vpts),Circle(spts,a,depth){}

	float radius()
	{
		float rad= sqrt(v_pt.tx * v_pt.tx + v_pt.ty * v_pt.ty); 
		a=rad;
		return a;
	}
	float x()
	{
		return v_pt.tx/radius();
	}
	float y()

	{
		return v_pt.ty/radius();
	}
	void scale(float factor) override
	{
		a*=factor;
	}
};
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

const float EPSILON = 1e-5f;

bool is_near(float x, float y)
{
	return std::abs(x - y) < EPSILON;
}

float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}
int main()
{
	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	float x = frand();
	float y = frand();
	float sx = frand();
	float sy = frand();
	float depth = frand();
	Square square(Square::PointType(x, y), Square::VectorType(sx, sy), 
        depth);
        float scale = frand();
	float radius = std::sqrt(sx * sx + sy * sy);
	Square square3(square);
	square3.scale(scale);
	
	if (is_near(square3.radius(), scale * radius))
	{
		++count;
	}
	else
	{
		std::cout << "  - Square::scale test failed" << std::endl;
	}
	std::cout << square3.radius()<< " " << scale* radius<<std::endl;
	++max_count;
}
 	
Last edited on
Maybe it's because I haven't had breakfast, but I can't understand your code:
- What does Object::d? represent?
- Object has nested type PointType but doesn't use it. So why is it in Object?
- PointType::center() only serves to obfuscate. (Almost) anywhere you'd write (expr).center(), you could just write (expr) instead.
Point::mpoint is private, so it's hidden from the derived classes and it isn't used by any of the non-existent members of Point. So it serves no purpose. So what's the purpose of class Point?
- If a circle is a point, then why does it also contain another PointType? If Circle::r is the radius then what is the center? Circle.m_pt or Circle::Point.m_point?
- Circle::center() should return a point, not a Circle.
- Why do you derive Square from Circle? A square isn't a circle. One way to represent a square is by diagonally opposite vertexes. You might try doing it this way.
- What does it mean to scale a square? Should it grow in one direction? Or should the center remain the same? It looks like you want the center to remain the same, so add that as a comment.

For the third time (see http://www.cplusplus.com/forum/beginner/254132/) What are you trying to do?
Hi Dhayden, I know this is weird. But it is what my school wants. They want us to use square to inherit from a circle. Yes, does not make sense. But really I've gotten used to my school's style. I am just implementing the functions based on the main() they provide. Most of what you have commented is how I implement my functions based on my main(). So, really I do not know why. It is just how it is.
@playpro10,

I'm coming to this late on the 26th, so I'm checking to see if you finished, have more code (or different code) to post, or if you're still working on it.

I have to say I'm often puzzled to the point of despair when I see a class assignment like this. Where, as you've given, main was provided by the teacher and can't be changed, ++max_count at the end of main makes it clear there's no focus or attention being paid, because that increment has no purpose (and isn't used). For that matter, neither is count. Perhaps the teacher has plans to move the content of main elsewhere that would make some sense, but then why leave such a mystery to confuse?

Do I understand that all you are given is main? Are there no guidance or requirements given related to IScalable, Object, Point, etc.?



Playpro10, can you post the text of the assignment. It will help greatly. If you aren't comfortable posting the text, send it to me as a private message please.
Topic archived. No new replies allowed.