Help building first class

Y

yogi_bear_79

This is what i have so far. I am getting the following errors:
error C2511: 'int Point::getX(void)' : overloaded member function not
found in 'Point
error C2511: 'int Point::getY(void)' : overloaded member function not
found in 'Point

I am sure there are other issues, any tutoring would be helpful,
thanks

#include<iostream>
using namespace std;

#ifndef CartesianPoint
#define CartesianPoint

class Point
{
public:
void print(ostream& out)const;
Point();
Point(int x, int y);
int getX()const;
int getY()const;
void setX(int x);
void setY(int y);

private:
int myX, myY;
};

inline Point::point()
{
myX = 0;
myY = 0;
}

inline Point::point(int x, int y)
{
myX = x;
myY = y;
}

void Point::setX(int x)
{
myX = x;
}

void Point::setY(int y)
{
myY = y;
}

int Point::getX()
{
return myX;
}

int Point::getY()
{
return myY;
}
#endif
 
I

Ian Collins

yogi_bear_79 said:
This is what i have so far. I am getting the following errors:
error C2511: 'int Point::getX(void)' : overloaded member function not
found in 'Point
error C2511: 'int Point::getY(void)' : overloaded member function not
found in 'Point
The declaration was const, the definition wasn't.
I am sure there are other issues, any tutoring would be helpful,
thanks

#include<iostream>
using namespace std;

#ifndef CartesianPoint
#define CartesianPoint

class Point
{
public:
void print(ostream& out)const;
Point();
Point(int x, int y);
int getX()const;
int getY()const;
void setX(int x);
void setY(int y);
Why not do away with these and make x and y public? The presence of
accessor functions like these is nearly always bad design.

inline Point::point()
{
myX = 0;
myY = 0;
}

inline Point::point(int x, int y)
{
myX = x;
myY = y;
}
Use initialiser lists for these. You could declare default values and
save the second constructor:

Point(int x = 0, int y = 0);
 
Y

yogi_bear_79

The declaration was const, the definition wasn't.









Why not do away with these and make x and y public?  The presence of
accessor functions like these is nearly always bad design.





Use initialiser lists for these.  You could declare default values and
save the second constructor:

Point(int x = 0, int y = 0);

Ian thanks for the tips, I used the itializers and remvoed the second
constructor. I don't think I understand what you mean by making x & y
public..
 
R

red floyd

yogi_bear_79 said:
Ian thanks for the tips, I used the itializers and remvoed the second
constructor. I don't think I understand what you mean by making x & y
public..

Essentially, given the accessors (getX/Y setX/Y), the client of your
class has full access to the members X and Y anyways, so why bother
making them private with various accessors?
 
Y

yogi_bear_79

Essentially, given the accessors (getX/Y setX/Y), the client of your
class has full access to the members X and Y anyways, so why bother
making them private with various accessors?- Hide quoted text -

- Show quoted text -

Thanks All! Here is my final product for this portion

#include<iostream>

#ifndef h_CartesianPoint
#define h_CartesianPoint

class Point
{
public:
Point(double x = 0, double y = 0);
void print();
double getX()const;
double getY()const;
void setX(double x);
void setY(double y);
double myX, myY;
};

inline Point::point(double x, double y) : myX(x), myY(y) { }

inline void Point::print()
{
cout << "(" << myX << "," << myY << ")";
return;
}

inline void Point::setX(double x)
{
myX = x;
}

inline void Point::setY(double y)
{
myY = y;
}

inline double Point::getX()const
{
return myX;
}

inline double Point::getY()const
{
return myY;
}
#endif
 
I

Ian Collins

yogi_bear_79 said:
Please trim your replies and don't quote signatures or that google nonsense.
Ian thanks for the tips, I used the itializers and remvoed the second
constructor. I don't think I understand what you mean by making x & y
public..

The accessor members do nothing except expose the two data members, they
serve no purpose. You may as well start out with Point as a struct
rather than as a class.
 
I

Ian Collins

yogi_bear_79 said:
Thanks All! Here is my final product for this portion

#include<iostream>

#ifndef h_CartesianPoint
#define h_CartesianPoint

class Point
{
public:
Point(double x = 0, double y = 0);
void print();
double getX()const;
double getY()const;
void setX(double x);
void setY(double y);
double myX, myY;

No - get rid of the accessors, there's no point in them being there if x
and y are public.

struct Point
{
double x;
double y;

Point(double x = 0, double y = 0);
void print();
};

is all you require for now.
 
Y

yogi_bear_79

struct Point
{
  double x;
  double y;

  Point(double x = 0, double y = 0);
  void print();

};

is all you require for now.
--

For the purpose of this lab, that I am supposed to write a class, and
this is the very first one, entry level, does this work as a class,
albeit your way would be better?

class Point
{
public:
Point(double x = 0, double y = 0);
void print();
double getX()const;
double getY()const;
void setX(double x);
void setY(double y);
private:
double myX, myY;
};
 
I

Ian Collins

yogi_bear_79 said:
For the purpose of this lab, that I am supposed to write a class, and
this is the very first one, entry level, does this work as a class,
albeit your way would be better?
Well yes, but don't forget a struct is basically a class with public
default access. Why do you have to have the pointless accessor
functions? Are you sure you won't loose through having them?

All you have is a container with a constructor and a print method, by
adding the accessor functions you are removing any encapsulation.

If you must you could trim your example to:
 
Y

yogi_bear_79

Well yes, but don't forget a struct is basically a class with public
default access.  Why do you have to have the pointless accessor
functions?  Are you sure you won't loose through having them?

All you have is a container with a constructor and a print method, by
adding the accessor functions you are removing any encapsulation.

If you must you could trim your example to:


Well, I built it off of the book's design with a little help from
Google and the group. I think the basic intent is to learn the class
in an ever so simple fashion, which is probbaly perfect for the level
I am. I learn better ways as I go, and I have about 7 more labs to
go, and then a data structures class. So for now I think this is a
good start.
 
Y

yogi_bear_79

functions you are removing any encapsulation.
If you must you could trim your example to:

OK, I trimmed it to this:
#include<iostream>

#ifndef h_CartesianPoint
#define h_CartesianPoint

class Point
{
public:
Point(double x = 0, double y = 0);
void print();
double myX, myY;
};

inline Point::point(double x, double y) : myX(x), myY(y) { }

inline void Point::print()
{
cout << "(" << myX << "," << myY << ")";
return;
}
#endif
 
R

red floyd

yogi_bear_79 said:
functions you are removing any encapsulation.

OK, I trimmed it to this:
#include<iostream>

#ifndef h_CartesianPoint
#define h_CartesianPoint

class Point
{
public:
Point(double x = 0, double y = 0);
void print();
double myX, myY;
};

inline Point::point(double x, double y) : myX(x), myY(y) { }

inline void Point::print()
{
cout << "(" << myX << "," << myY << ")"; std::cout << ...
return;
}
#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top