Several simple questions about an interesting test

The problem comes the first (it's a bit of lengthy but definitely easy):


This is a test of your C++ design and programming skills.

1) Make an inheritance hierarchy with at least two classes which can represent the coordinate points given below. Each class should provide a method (or methods) which will return the name and the coordinates of the point.
Include any other methods you find appropriate or necessary to solve the rest of the test or to make the classes useful in practice. Two coordinate systems which will need to be handled are given below. The theta angle
for polar coordinates is measured from the positive x-axis in the counter-clockwise direction. You may add any other classes or methods you think will make it easier for other people to use or extend your inheritance hierarchy.

PointName CoordinateSystem Coordinates
A Polar r = 8, theta = pi/6 radians
B Polar r = 10, theta = pi/3 radians
C Cartesian x = 2, y = 8
D Cartesian x = 4, y = 10


2) Now, make a new class called PointDistances that holds a collection of pointers to objects from the CoordinatePoint classes you wrote for problem 1).
The pointers stored in the PointDistances class will include both pointers to your classes created in problem 1), and pointers to new derived classes added to your hierarchy by other programmers.
Give it the following methods:
2a) a method that adds a copy of a CoordinatePoint object to the collection.
2b) a method (or methods) which will return copies of the two points which are closest together and the distance between these 2 points.

3) Write a main() function using the classes you wrote for 1) and 2) which will print out the PointName and the Coordinates of the 2 closest points given in 1) along with the distance between these 2 points.



Here's my code:
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 using namespace std;
 
 enum CS{
	 Polar = 0,
	 Cartesian = 1	 
	 };
	 
 class point {
	 public:
     char _PointName;
     int _System;
     
     public:
     point(char PointName, int System):_PointName(PointName), _System(System){;}
     point(const point& p): _PointName(p._PointName), _System(p._System){;}
     //point& operator=(const point& p){
		 //_PointName = p._PointName;
		 //_System = p._System;
		  //}
     char& name() {return _PointName;}
     int& system() {return _System;}
     ~point(){;}
 };

class Ppoint: public point{
	public:
	double _r;
	double _theta;
	
	public:	
    Ppoint(char PointName, int System, double r, double theta):point(PointName, System), _r(r), _theta(theta){;}
    Ppoint(const Ppoint& p): point(p), _r(p._r), _theta(p._theta){;}

    //Ppoint& operator=(const Ppoint& p){
		 //_PointName = p._PointName;
		 //_System = p._System;
		 //_r = p._r;
		 //_theta = p._theta;
		  //}
    ~Ppoint(){;}
		
    double r(){return _r;}
    double theta() {return _theta;}
	
		
		};
	
class Cpoint: public point{
	public:
	double _x;
	double _y;
	
	public:
	Cpoint(char PointName, int System, double x, double y): point(PointName, System), _x(x), _y(y){;}
    Cpoint(const Cpoint& p): point(p), _x(p._x), _y(p._y){;}
    //Cpoint& operator=(const Cpoint& p){
		 //_PointName = p._PointName;
		 //_System = p._System;
		 //_x = p._x;
		 //_y = p._y;
		  //}
    ~Cpoint(){;}
	
	double x(){return _x;}
    double y() {return _y;}	
	
	};
	
	
class PointDistances{
	public:
	vector<point*> ptvec;
	double shortest;
	point* close1, close2;
	
	PointDistances(){
		shortest = 0;
		}
	~PointDistances() {;}
	
	protected:
	double dist(point* a, point* b){
		double x1, x2, y1, y2;
		if (a->system() == Polar){
			x1 = a->r()*cos(a->theta());
			y1 = a->r()*sin(a->theta());
		}
		else
		{
			x1 = a->x();
			y1 = a->y();			
		}
		
		if (b->system() == Polar){
			x2 = b->r()*cos(b->theta());
			y2 = b->r()*sin(b->theta());
		}
		else
		{
			x2 = b->x();
			y2 = b->y();			
		}
		
        return sqrt((pow((x1-x2),2) + pow((y1-y2),2)));
	}
	
	public:
    void add(point* newpt) {ptvec.push_back(newpt);}
		
	double closest(){
		double distance = 0;
		for (int i=0; i < ptvec.size(); i++){
			for (int j=i+1; j <= ptvec.size(); j++){
				distance = dist(ptvec[i],ptvec[j]);
				if (shortest > distance ||close1 == NULL||close2 == NULL)
				   {close1 = ptvec[i];
				    close2 = ptvec[j];
   				    shortest = distance;
				   }
				}
			}
		
		cout << "Point " << close1->name() << " and Point " << close2->name() << " has the shortest distance " << shortest << endl
		}
	
	};
	
int main(){
	
	Ppoint p1('A', Polar, 8, M_PI/6.0); 
    Ppoint p2('B', Polar, 10, M_PI/3.0); 
    Cpoint p3('C', Cartesian, 2.0, 8.0);
    Cpoint p4('D', Cartesian, 4.0, 10.0);
    
    point* Pp1 = &p1;
    point* Pp2 = &p2;
    point* Pp3 = &p3;
    point* Pp4 = &p4;
    
	PointDistances pd;
	pd.add(p1);
	pd.add(p2);
	pd.add(p3);
    pd.add(p4);
    
    pd.closest();
	return 0;
	}


My compiler errors:


cs.cpp:76: error: ISO C++ forbids declaration of âvectorâ with no type
cs.cpp:76: error: expected â;â before â<â token
cs.cpp: In constructor âPointDistances::PointDistances()â:
cs.cpp:80: error: no matching function for call to âpoint::point()â
cs.cpp:19: note: candidates are: point::point(const point&)
cs.cpp:18: note:                 point::point(char, int)
cs.cpp: In member function âdouble PointDistances::dist(point*, point*)â:
cs.cpp:89: error: âclass pointâ has no member named ârâ
cs.cpp:89: error: âclass pointâ has no member named âthetaâ
cs.cpp:90: error: âclass pointâ has no member named ârâ
cs.cpp:90: error: âclass pointâ has no member named âthetaâ
cs.cpp:94: error: âclass pointâ has no member named âxâ
cs.cpp:95: error: âclass pointâ has no member named âyâ
cs.cpp:99: error: âclass pointâ has no member named ârâ
cs.cpp:99: error: âclass pointâ has no member named âthetaâ
cs.cpp:100: error: âclass pointâ has no member named ârâ
cs.cpp:100: error: âclass pointâ has no member named âthetaâ
cs.cpp:104: error: âclass pointâ has no member named âxâ
cs.cpp:105: error: âclass pointâ has no member named âyâ
cs.cpp: In member function âvoid PointDistances::add(point*)â:
cs.cpp:112: error: âptvecâ was not declared in this scope
cs.cpp: In member function âdouble PointDistances::closest()â:
cs.cpp:116: error: âptvecâ was not declared in this scope
cs.cpp:119: error: no match for âoperator==â in â((PointDistances*)this)->PointDistances::close2 == 0lâ
cs.cpp:127: error: base operand of â->â has non-pointer type âpointâ
cs.cpp:128: error: expected `;' before â}â token
cs.cpp: In function âint main()â:
cs.cpp:145: error: no matching function for call to âPointDistances::add(Ppoint&)â
cs.cpp:112: note: candidates are: void PointDistances::add(point*)
cs.cpp:146: error: no matching function for call to âPointDistances::add(Ppoint&)â
cs.cpp:112: note: candidates are: void PointDistances::add(point*)
cs.cpp:147: error: no matching function for call to âPointDistances::add(Cpoint&)â
cs.cpp:112: note: candidates are: void PointDistances::add(point*)
cs.cpp:148: error: no matching function for call to âPointDistances::add(Cpoint&)â
cs.cpp:112: note: candidates are: void PointDistances::add(point*)


My questions:

1. In line 76, I want to define a vector which can hold pointers to both Ppoint and Cpoint. So I used
vector<point*> ptvec;
. In line 86 I defined a function
dist
where a and b can be pointers to either derived type:
Ppoint
or
Cpoint
. But the error given by the compiler shows that the compiler strictly take a and b as pointers to the base class. Can anyone tell me how to modify this?

2. I have absolutely no idea on how to define the constructor of
PointDistances
in line 80. Can anyone help?

3. Is there any other way to structure the code to make it more efficient? I really doubt if I am on the right track, especially for the structure of the whole code. I appreciate any help and comments on this.

Here is some useful formulae:

Appendix:
Converting between polar and Cartesian coordinates.

Polar to Cartesian:

x = r * cos(theta)
y = r * sin(theta)

Cartesian to Polar:

r = sqrt(x*x + y*y)


0 if x == 0 and y == 0
theta = arcsin(y/r) if x >= 0
-arcsin(y/r) + pi radians if x < 0


Distances between 2 Cartesian points.
Given
point1 = (x1,y1)
point2 = (x2,y2)

distance = sqrt( pow((y2 - y1),2) + pow((x2 - x1),2) )


Distances between 2 Polar points.
Given
point1 = (r1,theta1)
point2 = (r2,theta2)

distance = sqrt(r1*r1 + r2*r2 - 2*r1*r2*cos(theta1 - theta2))
Include vector.h, this should remove first error.

I am not sure that the proposed hierarchy is very useful, because you cannot mix coordinates written in different systems. IMHO, I think that CartesianPoint should be a base class, PolarPoint inherits all methods of CartesianPoint + introduces new methods.

Then you can build distance class using only CartesianPoint.
tfityo,

Thank you so so much for your input. The structure you suggested is much more efficient than the one coded. Thanks!

Topic archived. No new replies allowed.