Classes help

Hi All I need some help. I am trying to learn C++ and I'm not grasping classes can some one help me with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate
{
private:
    double x, y, z;
	
public:
  
	Coordinate(double xx, double yy, double zz);
	void setCoordinate (double xx, double yy, double zz);
	void printCoordinate();
	Coordinate add(const Coordinate xx);
	Coordinate subtract(const Coordinate xx);


};
#endif 


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
Coordinate.cpp

#include <iostream>
#include <iomanip>
#include "Coordinate.h"

using namespace std;

Coordinate::Coordinate(double xx, double yy, double zz)
{ 
    x = xx;
    y = yy;
    z = zz;	
}

void Coordinate::setCoordinate(double xx, double yy, double zz)
{
    Coordinate temp;
	temp.x = xx;
	temp.y = yy;
	temp.z = zz;
	
	
}

void Coordinate::printCoordinate()
{
    cout << "(" << x << ", " << y << ", " << z << ")" ;
}

Coordinate Coordinate::add(const Coordinate b)
{
    Coordinate f;
	
	f.x = x + b.x;
	f.y = y + b.y;
	f.z = z + b.z;
	
	return f;
}

Coordinate Coordinate::subtract(const Coordinate b)
{
    Coordinate e;
	
	e.x = x - b.x;
	e.y = y - b.y;
	e.z = z - b.z;
	
	return e;
}


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
main.cpp

#include <iostream>
#include "Coordinate.h"
using namespace std;

int main()
{
    Coordinate a( 1, 7, 0 ), b( 9, 8, 7 ), c;

    a.printCoordinate();
    cout << " + ";
    b.printCoordinate();
    cout << " = ";
    c = a.add( b );
    c.printCoordinate();
    
    cout << '\n';
    a.setCoordinate( 10, 1, 10 );
    b.setCoordinate( 11, 3, 20 );
    a.printCoordinate();
    cout << " - ";
    b.printCoordinate();
    cout << " = ";
    c = a.subtract( b );
    c.printCoordinate();
    cout << endl;
}


Its giving me all kinds of errors and what not. I appreciate it in advance.
You certainly need to rethink this function:
This code has no real effect.

1
2
3
4
5
6
7
8
9
void Coordinate::setCoordinate(double xx, double yy, double zz)
{
    Coordinate temp;  //why are we using  local variable here
	temp.x = xx;
	temp.y = yy;
	temp.z = zz;
	
	
}
9
10
11
12
13
14
Coordinate::Coordinate(double xx, double yy, double zz)
{ 
    x = xx;
    y = yy;
    z = zz;	
}
I would like to point out an alternate syntax, it will not make any difference really.
9
10
11
Coordinate::Coordinate(double xx, double yy, double zz) : x(xx), y(yy), z(zz)
{
}
Also, you really should implement the default constructor. Who knows what it does without your guidance?

16
17
18
19
20
21
22
23
24
void Coordinate::setCoordinate(double xx, double yy, double zz)
{
    Coordinate temp;
	temp.x = xx;
	temp.y = yy;
	temp.z = zz;
	
	
}
This function does not change its class; it does nothing. Additionally, why not use the constructor you made?

31
32
33
34
35
36
37
38
39
40
Coordinate Coordinate::add(const Coordinate b)
{
    Coordinate f;
	
	f.x = x + b.x;
	f.y = y + b.y;
	f.z = z + b.z;
	
	return f;
}
You should pass b by reference. Additionally, why not use the constructor you made?

42
43
44
45
46
47
48
49
50
51
Coordinate Coordinate::subtract(const Coordinate b)
{
    Coordinate e;
	
	e.x = x - b.x;
	e.y = y - b.y;
	e.z = z - b.z;
	
	return e;
}
You should pass b by reference and use the constructor you made. Additionally, why did you avoid giving e the same name as f from the other function? They are in separate functions; they can't interfere with each other.

blaze41 wrote:
Its giving me all kinds of errors and what not.
Care to actually tell us what these errors are? You should copy and paste them ;)
Last edited on
1
2
3
4
5
6
7
void Coordinate::setCoordinate(double xx, double yy, double zz)
{
    Coordinate temp;
	temp.x = xx;
	temp.y = yy;
	temp.z = zz;
}
In this example, you want to set the values of this object, not some temp object, so the correct code is:
1
2
3
4
5
6
void Coordinate::setCoordinate(double xx, double yy, double zz)
{
	x = xx;
	y = yy;
	z = zz;
}


Similarly, add and subtract must modify the object. A few tweaks, you pass in the value to add by const reference rather than by value. You don't have to return a value, but if you do, it's conventional to return the actual object under modification by reference.
1
2
3
4
5
6
7
8
Coordinate& Coordinate::add(const Coordinate &b)
{
	x = x + b.x;
	y = y + b.y;
	z = z + b.z;
	
	return *this;
}
Thank you for your reply, how would I use the constructor to add and subtract the different coords?

this is the error i get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 g++ Coordinate.h Coordinate.cpp pa8.cpp
Coordinate.cpp: In member function `void Coordinate::setCoordinate(double, double, double)':
Coordinate.cpp:21: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note:                 Coordinate::Coordinate(double, double, double)
Coordinate.cpp: In member function `Coordinate Coordinate::add(Coordinate)':
Coordinate.cpp:36: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note:                 Coordinate::Coordinate(double, double, double)
Coordinate.cpp: In member function `Coordinate Coordinate::subtract(Coordinate)':
Coordinate.cpp:47: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note:                 Coordinate::Coordinate(double, double, double)
pa8.cpp: In function `int main()':
pa8.cpp:9: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.h:16: note:                 Coordinate::Coordinate(double, double, double)
Last edited on
You wouldn't, because you shouldn't have been making another obejct in the first place. I just mentioned it as an additional point because you took the time to make the nice convenient constructor but then you completely circumvented it.
The constructor should only initialize the members of the class, nothing else. Similarly, the destructor should only uninitialize the members of the class. All the work must be done by class methods.

As for the compiler errors:
You don't have a default constructor, so you can write code like:
 
Coordinate c;
But that's ok.

The real problem is that your function void Coordinate::setCoordinate(double, double, double) is still creating a temporary object. I've already shown that this is incorrect.
Thank you kbw for your help! I don't fully understand what a default constructor is. I read the classes (I) tutorial but I guess im not that smart would a default constructor be something like: Coordinate(double xx, double yy, double zz) {x=xx; y=yy; z=zz;}; ?
Last edited on
A default constructor is the constructor that is called when you don't specify any arguments.

In all your legitimate code you always specify the coordinate values. It's only in your incorrect code that don't provide coordinates. So don't worry about creating a default constructor as it will make your incorrect code compile, but the results will be incorrect.
So I updated the code and got it to work. thanks for the help with the default constructor.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate
{
private:
    double x, y, z;
	
public:
    Coordinate();
	Coordinate(double, double, double);
	void setCoordinate (double xx, double yy, double zz);
	void printCoordinate();
	Coordinate& add(const Coordinate &b);
	Coordinate& subtract(const Coordinate &b);


};
#endif 


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
#include <iostream>
#include <iomanip>
#include "Coordinate.h"

using namespace std;
Coordinate::Coordinate()
{
    x = 0.0;
	y = 0.0;
	z = 0.0;
}
Coordinate::Coordinate(double xx, double yy, double zz)
{ 
    x = xx;
    y = yy;
    z = zz;	
}

void Coordinate::setCoordinate(double xx, double yy, double zz)
{
    
	x = xx;
	y = yy;
	z = zz;
	
	
}

void Coordinate::printCoordinate()
{
    cout << "(" << x << ", " << y << ", " << z << ")" ;
}

Coordinate& Coordinate::add(const Coordinate &b)
{
    	
	x = x + b.x;
	y = y + b.y;
	z = z + b.z;
	
	return *this;
}

Coordinate& Coordinate::subtract(const Coordinate &b)
{
    
	
	x = x - b.x;
	y = y - b.y;
	z = z - b.z;
	
	return *this;
}


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
#include <iostream>
#include "Coordinate.h"
using namespace std;

int main()
{
    Coordinate a( 1, 7, 0 ), b( 9, 8, 7 ), c;

    a.printCoordinate();
    cout << " + ";
    b.printCoordinate();
    cout << " = ";
    c = a.add( b );
    c.printCoordinate();
    
    cout << '\n';
    a.setCoordinate( 10, 1, 10 );
    b.setCoordinate( 11, 3, 20 );
    a.printCoordinate();
    cout << " - ";
    b.printCoordinate();
    cout << " = ";
    c = a.subtract( b );
    c.printCoordinate();
    cout << endl;
	
}


output:

1
2
(1, 7, 0) + (9, 8, 7) = (10, 15, 7)
(10, 1, 10) - (11, 3, 20) = (-1, -2, -10)
HI all, am trying to create a c++ program by using user defined functions to calculate grade point of a student in an exam
Last edited on
Thanks again KBW. Is the reason we used

*this

for return on the add and subtract because we passed the variables by reference and had to return a pointer?
this is a pointer to the object currently being dealt with; the object on which the member function was called. To return a reference you have to dereference the this pointer. (Remember that dereferencing a pointer is unrelated to C++ references)
Thanks LB!
add and subtract don't need to return anything, void is sufficient.

In C, it's common to see code like:
 
a = b = c = 0;
When C++ was developed a similar feature was designed for the assignments operator. So you can do that sort of thing with objects in C++ and it requires the assignment operator to return *this to allow this. But like I said, you don't need it.
Topic archived. No new replies allowed.