beginners class problem - testing constructors

I want to test the constructors I made, and I'm have trouble with Coord(int x,float d); where I am supposed to assign them all the same variable value coordTesting02(); is the one testing in

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
156
157
158
159
160
161
162
163
164
165
166
167
168
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

/****************************************
*     class definitions
****************************************/
class Coord
{
    private:
       int  xPoint;
       int  yPoint;
       int  zPoint;
       float deg;
  
    public:
       // setters
        void setxPoint(int x) {xPoint = x;}
        void setyPoint(int y) {yPoint = y;}
        void setzPoint(int z) {zPoint = z;}
        void setDeg(float d) {deg = d;}
        
        
        // getters
        int getxPoint() const {return xPoint;}
        int getyPoint() const {return yPoint;}
        int getzPoint() const {return zPoint;}
        float getDeg() const {return deg;}
        
       // 'member' functions 
        distance2origin();
        Coord(int x,int y,int z,float d);
        Coord();
	Coord(int x,float d);
        distBetweenPoints(const Coord &obj);
        void display();
                 
};
/****************************************
*     member functions for above class
****************************************/
int Coord :: distance2origin()
{
    return sqrt((pow(getxPoint(),2)) +
        (pow(getyPoint(),2)) +
        (pow(getzPoint(),2)));

}

Coord :: Coord(int x,int y,int z,float d)
{
    
    int xCoord = x;
    int yCoord = y;
    int zCoord = z;
    float degrees = d;
}

Coord :: Coord() // how do I test this?
{
    int xCoord = 0;
    int yCoord = 0;
    int zCoord = 0;
    float degrees = 0;
}


Coord :: Coord(int c,float d) //I get garbage when I run this one
{
    int xCoord = c; // garbage here
    int yCoord = c; // zero here
    int zCoord = c; //zero here
    float degrees = d;
}

int Coord :: distBetweenPoints(const Coord &obj)
{
    return sqrt((((double)(getxPoint()*getxPoint())) - ((obj.getxPoint(), 2)))  +
	(((double)(getyPoint()*getyPoint())) - ((obj.getyPoint(), 2))) +
	(((double)(getzPoint()*getzPoint())) - ((obj.getzPoint(), 2))));        
    


}



void Coord :: display()
{
    int distance = 0;
    distance = distance2origin();
   cout << "\nxCoord is " << getxPoint() << endl;
   cout << "yCoord is " << getyPoint() << endl;
   cout << "zCoord is " << getzPoint() << endl;
   cout << "degrees are " << getDeg() << endl;
   cout << "distance to origin " << distance << endl;
   cout << "\n";
}
 
/*************************************
*     function prototype
*************************************/

void coordTesting01();
void coordTesting02();
void coordTesting03();
/************************************
*      global variables
*************************************/

int main()
{
    //coordTesting01();
    coordTesting02();
    //coordTesting03();
}

/************************************
*      non-member functions
*************************************/

Coord c1;
 void coordTesting01()
{
     cout << "****Test 01***\n";
        Coord c1;   //test setters
        c1.setxPoint(12);
        c1.setyPoint(17);
        c1.setzPoint(3);
        c1.setDeg(1.6);
        c1.distance2origin();
        
                    //test getters
        c1.getxPoint();
        c1.getyPoint();
        c1.getzPoint();
        c1.getDeg();
        c1.display();
        
}         

 void coordTesting02()
 {     
     cout << "****Test 02***\n";
     Coord c2;
     
     Coord test1(3,7,21,1.9);
     c2.display();
     Coord test2();
     c2.display();
     Coord test3(31,1.9);
     c2.display();
} 

  void coordTesting03()
{
        cout << "****Test 03***\n";
        
	int d = 0;
       
    
        d = test01.distBetweenPoints(test02);
       cout << "distance is " << d << endl;
        
}
   
Last edited on
A constructor is a function that is automatically called to initialize an object.

So, we'd expect the constructors for class Coord to set values for members xPoint, yPoint, zPoint and deg. But you aren't doing that.
d = test01.distBetweenPoints(test02); // <-- how is this compiling? You haven't declared a 'test01' or a 'test02'
Last edited on
Topic archived. No new replies allowed.