C++ providing default parameters to constructors - c++

In the c++ class below, I've provided default parameters to the constructor in case the user does not provide one. However, when I for example Point2d1 first(1, 0); in main() I get an error of no matching function call. I expected the behavior to default the 3rd parameter to 0?
.h
#ifndef POINT2D1_H_
#define POINT2D1_H_
class Point2d1 {
private:
int m_Object_ID;
double m_x;
double m_y;
public:
//Point2d1(int nID);
Point2d1(int nID, double x, double y);
virtual ~Point2d1();
void print() const;
friend double distanceFrom(const Point2d1& D1, const Point2d1& D2);
};
#endif /* POINT2D1_H_ */
.cpp
Point2d1::Point2d1(int nID = 0, double x = 0.0, double y = 0.0) : m_Object_ID(nID), m_x(x), m_y(y)
{
std::cout << "Constructing Point2d object " << nID << '\n';
}
Point2d1::~Point2d1() {
std::cout << "Destructing Object" << '\n';
}
void Point2d1::print() const
{
std::cout << "Point2d(" << m_x << ", " << m_y << ")\n";
}
double distanceFrom(const Point2d1& D1, const Point2d1& D2)
{
double distance = sqrt((D1.m_x - D2.m_x)*(D1.m_x - D2.m_x) + (D1.m_y - D2.m_y)*(D1.m_y - D2.m_y));
return distance;
}

Declare the default arguments in the member function declartion inside the class definition in the header file. Otherwise other compilation units will not know about the default arguments.
For example
.h
#ifndef POINT2D1_H_
#define POINT2D1_H_
class Point2d1 {
//...
public:
//Point2d1(int nID);
Point2d1(int nID = 0, double x = 0.0, double y = 0.0);
//...
};
#endif /* POINT2D1_H_ */
and
.cpp
Point2d1::Point2d1(int nID, double x, double y) : m_Object_ID(nID), m_x(x), m_y(y)
{
std::cout << "Constructing Point2d object " << nID << '\n';
}

Related

How to make object of a class with parameterized constructor in other class?

I want to make an object of DataArea class in Area class and initialize data in main function. But the only way my code works is by initializing data in Area class.
Also, I do not know if I have made the object correctly or not. Please guide me. My code is below:
#include<iostream>
using namespace std;
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l, int w, int b, int h, int r)
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
};
class Area
{
public:
DataArea* s = new DataArea(3, 4, 5, 6, 7);
float AreaCirle()
{
return 3.142 * s->radius * s->radius;
}
float AreaRectangle()
{
return s->length * s->width;
}
float AreaTraingle()
{
return (s->base * s->heigth) / 2;
}
};
class print_data : public Area
{
public:
void print()
{
cout << "Area of Circle is: " << AreaCirle() << endl;
cout << "Area of Rectangle is: " << AreaRectangle() << endl;
cout << "Area of Traingle is: " << AreaTraingle() << endl;
}
};
int main()
{
//DataArea da(3, 4, 5, 6, 7);
print_data m;
m.print();
}
Your DataArea is basically absolute if you do not use it outside of Area class. Similarly, print_data class can be replaced by an operator<< overload.
Following is the updated code, in which the comments will guide you through.
#include <iostream>
// DataArea (optionally) can be the part of Area class
struct DataArea /* final */
{
float length, width, base, height, radius;
DataArea(float l, float w, float b, float h, float r)
: length{ l } // use member initializer lists to initlize the members
, width{ w }
, base{ b }
, height{ h }
, radius{ r }
{}
};
class Area /* final */
{
DataArea mDataArea; // DataArea as member
public:
// provide a constructor which initialize the `DataArea` member
Area(float l, float w, float b, float h, float r)
: mDataArea{ l, w, b, h, r } // member initializer
{}
// camelCase naming for the functions and variables
// mark it as const as the function does not change the member
float areaCirle() const /* noexcept */
{
return 3.142f * mDataArea.radius * mDataArea.radius;
}
float areaRectangle() const /* noexcept */
{
return mDataArea.length * mDataArea.width;
}
float areaTraingle() const /* noexcept */
{
return (mDataArea.base * mDataArea.height) / 2.f;
}
// provide a operator<< for printing the results
friend std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */;
};
std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */
{
out << "Area of Circle is: " << areaObject.areaCirle() << "\n";
out << "Area of Rectangle is: " << areaObject.areaRectangle() << "\n";
out << "Area of Traingle is: " << areaObject.areaTraingle() << "\n";
return out;
}
int main()
{
// now construct the Area object like this
Area obj{ 3, 4, 5, 6, 7 };
// simply print the result which uses the operator<< overload of the Area class
std::cout << obj;
}
Output:
Area of Circle is: 153.958
Area of Rectangle is: 12
Area of Traingle is: 15
It seems to me that Area class is surplus for what you are trying to achieve. You should probably put methods directly in DataArea class. Then you can create as many of DataArea objects as you like...
Like this:
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l , int w , int b , int h , int r )
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
float AreaCirle()
{
return 3.142 * radius * radius;
}
float AreaRectangle()
{
return length * width ;
}
float AreaTraingle()
{
return (base * heigth)/2;
}
};
int main(int argc, char **argv)
{
DataArea area1 (1,2,3,4,5);
DataArea area2 (8,2,3,4,5);
std::cout << area1.AreaCirle() << std::endl;
std::cout << area2.AreaCirle() << std::endl;
}
The reason why you are probably having trouble to understand the concept:
You're defining a class and instantiating an object. Sometimes these terms are used interchangeably, but in this case, this is an important distinction.
If you would like for your methods to operate on some other class, that you should make methods that accept that class as an argument. Otherwise, it is unnecessary complex.

Why am I getting this error on encapsulation in C++?

This is my Line2D.h
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
protected:
double length;
// Set function
void setLength();
public:
// Constructor
Line2D();
Line2D(Point2D pt1, Point2D pt2);
// Get functions
Point2D getPt1();
Point2D getPt2();
double getScalarValue();
// Set functions
void setPt1(Point2D pt1);
void setPt2(Point2D pt2);
// Other functions
void printLine2D();
bool operator==(const Line2D& otherL2D) const;
};
This is my Point2D.h,
class Point2D
{
// friend bool operator==(const Point2D& otherP2D) const;
protected:
// private:
// public:
int x;
int y;
double distFrOrigin;
// This function computes the distance of
// the point to the origin (0,0) and initializes
// disFrOrigin with the distance value.
void setDistFrOrigin();
public:
// Constructor
Point2D();
Point2D(int x, int y);
// Get functions
int getX();
int getY();
// Set functions
void setX(int);
void setY(int);
// Accessor method(returns the value of distFrOrigin
double getScalarValue();
void printPoint2D();
bool operator==(const Point2D& otherP2D) const;
};
This is my Line2D.cpp,
Line2D::Line2D()
{
pt1 = Point2D();
pt2 = Point2D();
length = 0.0;
}
Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)
{
/*
// Trial 1
pt1.x = Point2D.getX();
pt1.y = Point2D.getY();
pt2.x = Point2D.getX();
pt2.y = Point2D.getY();
*/
// pt1.Point2D(x,y);
// pt2.Point2D(x,y);
// Setting for both points
// this -> pt1 = pt1;
// this -> pt2 = pt2;
// setLength();
}
Line2D::setLength()
{
// int xL = pow((pt1.x - pt2.x),2);
// int yL = pow((pt1.y - pt2.y),2);
int xL = pow((pt1.getX() - pt2.getX()),2);
int yL = pow((pt1.getY() - pt2.getY()),2);
length = sqrt(xL + yL);
this -> length = length;
}
Line2D::getScalarValue()
{
return length;
}
Line2D::getPt1()
{
return pt1;
}
Line2D::getPt2()
{
return pt2;
}
Line2D::setPt1(Point2D pt1)
{
// Setting for first point only
this -> pt1 = pt1;
}
Line2D::setPt1(Point2D pt2)
{
// Setting for second point only
this -> pt2 = pt2;
}
void Line2D::printLine2D()
{
cout << " " << pt1 << " " << pt2 << " " << length << endl;
}
bool Line2D::operator==(const Line2D& otherL2D) const
{
return(pt1 == otherL2D.pt1 &&
pt1 == otherL2D.pt2);
}
this is my code for Point2D.cpp,
`Point2D::Point2D()
{
x = 0;
y = 0;
}
`
Point2D::Point2D(int x, int y)
{
this -> x = x;
this -> y = y;
}
void Point2D::setX(int x)
{
this -> x = x;
}
void Point2D::setY(int y)
{
this -> y = y;
}
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
void Point2D::printPoint2D()
{
cout << " " << x << " " << y << " " << distFrOrigin << endl;
}
bool Point2D::operator==(const Point2D& otherP2D) const
{
return(x == otherP2D.x &&
y == otherP2D.y);
}
Here's my problem:
My Point2D works fine. Line2D encapsulates Point2D. How do I write the constructor and the setLength() function?
The error I am getting is within these 2:
In Line2D.cpp: Point2D is not a direct base of Line2D
In Line2D.cpp: ISO C++ forbids declaration of 'setLength' with no type
In Line2D.cpp: setLength() cannot be overloaded (qn, what do i need to overload and how?)
In Line2D.cpp: call of overloaded 'pow(int,int)' is ambiguous (Don't understand this)
All the include header etc. is correct so I didn't put them here.
Those are the errors I am getting. I will be grateful for any help.
Thank you!
1. What do you mean by direct base? If you want them to share the same functions etc. you need to use abstract class, other than that, I am not sure if I understood this part of your question.
2. You need to specify "void" keyword in front of your function definition. Other than that, your setLength() function declared as "protected" in your header file. I don't know if you have a specific reason for a "set" function to be protected, but usually "set" functions are declared as publicly.
3. I couldn't see any other declaration of your "setLength()" function in your header file that is gooing to be overloaded later. You need to declare all of your functions even the ones that oyu need to overload too. It is important for compile to understand that which functions that it has.
4. You need to be more specific. I presume that this is the line that you are talking about:
int xL = pow((pt1.getX() - pt2.getX()),2);
xL = (pt1's X - pt2's X) * (pt1's X - pt2's X)
Hope this helps you.

Point2D class objects

This is what I need to do: Add a new data member, string color to your Point2D class and a new getter and setter function for color. Create a Point2D object and set its color. Then create a Point3D color and try to set its color. Is this setColor behavior available for a Point3D class? Why or why not?
This is my code:
#include<iostream>
#include<vector>
using namespace std;
class Point2D
{
friend class SPALops;
protected:
float x;
float y;
protected:
float getX(){
return x;
};
float getY(){
return y;
};
void setX(float xc){
x = xc;
};
void setY(float yc){
y = yc;
};
Point2D(int xcoord, int ycoord){
x = xcoord;
y = ycoord;
};
};
class Point2D : Point2D
{
friend class SPALops;
public:
Point2D(int x, int y) : Point2D(x,y){}
Point2D() : Point2D(0,0){}
float getX(){
return this->Point2D::getX();
};
float getY(){
return this->Point2D::getY();
};
void setX(float x){
this->Point2D::setX(x);
};
void setY(float y){
this->Point2D::setY(y);
};
};
class RectangleImplementation{
friend class SPALops;
protected:
Point2D ll;
Point2D ur;
RectangleImplementation(float llx, float lly, float urx, float ury){
ll.setX(llx); ll.setY(lly);
ur.setX(urx); ur.setY(ury);
}
void setLLx(float x){
ll.setX(x);
};
void setLLy(float y){
ll.setY(y);
};
void setURx(float x){
ur.setX(x);
};
void setURy(float y){
ur.setY(y);
};
float getLLx(){
return ll.getX();
};
float getLLy(){
return ll.getY();
};
float getURx(){
return ur.getX();
};
float getURy(){
return ur.getY();
};
vector<vector<float> > getPointList(){
vector<vector<float> > v(4);
vector<float> llv(2); llv[0] = ll.getX(); llv[1] = ll.getY();
v[0] = llv;
vector<float> luv(2); luv[0] = ll.getX(); luv[1] = ur.getY();
v[1] = luv;
vector<float> ruv(2); ruv[0] = ur.getX(); ruv[1] = ur.getY();
v[2] = ruv;
vector<float> rlv(2); rlv[0] = ur.getX(); rlv[1] = ll.getY();
v[3] = rlv;
return v;
};
void printPointList(){
vector<vector<float>> v = this->getPointList();
cout << "ll = " << v[0][0] << " , " << v[0][1] << endl;
cout << "lu = " << v[1][0] << " , " << v[1][1] << endl;
cout << "ru = " << v[2][0] << " , " << v[2][1] << endl;
cout << "rl = " << v[3][0] << " , " << v[3][1] << endl;
};
};
class Rectangle : RectangleImplementation{
friend class SPALops;
public:
Rectangle(Point2D &p, Point2D &q) : RectangleImplementation(p.getX(), p.getY(), q.getX(), q.getY()){};
Rectangle(float llx, float lly, float urx, float ury): RectangleImplementation(llx,lly,urx,ury){};
float getLLx(){
return this->RectangleImplementation::getLLx();
};
float getLLy(){
return this->RectangleImplementation::getLLy();
};
float getURx(){
return this->RectangleImplementation::getURx();
};
float getURy(){
return this->RectangleImplementation::getURy();
};
void setLLx(float x){
this->RectangleImplementation::setLLx(x);
};
void setLLy(float y){
this->RectangleImplementation::setLLy(y);
};
void setURx(float x){
this->RectangleImplementation::setURx(x);
};
void setURy(float y){
this->RectangleImplementation::setURx(y);
};
void printPointList(){
cout << "In rectangle: " << endl;
cout << "ll = " << ll.getX() << " , " << this->RectangleImplementation::getLLy() << endl;
cout << "ru = " << this->getURx() << " , " << this->RectangleImplementation::getURy() << endl;
};
};
class SPALops{
public:
static bool touches(Rectangle &r, Point2D &p){
vector<vector<float> > v = r.RectangleImplementation::getPointList();
if((v[0][0] == p.getX() and v[0][1] == p.getY()) or
(v[1][0] == p.getX() and v[1][1] == p.getY()) or
(v[2][0] == p.getX() and v[2][1] == p.getY()) or
(v[3][0] == p.getX() and v[3][1] == p.getY()))
{
return true;
}
else
{
return false;
}
};
};
int main(){
Point2D p(10,10);
Point2D q(15,15);
Point2D s(20,20);
Point2D t(10,12);
Rectangle r(p,q);
r.printPointList();
cout << "Do rectangle 'r' and point 'p' touch = " << SPALops::touches(r,p) << endl;
cout << "Do rectangle 'r' and point 's' touch = " << SPALops::touches(r,s) << endl;
cout << "Do rectangle 'r' and point 't' touch = " << SPALops::touches(r,t) << endl;
return 0;
}
It seems I have many errors that stop me from running succesfully. I would appreciate if I get any feedback.
Here are some issues I found in your post:
1) Duplicate classes.
You have a Point2D class first, then a 2nd Point2D class.
Maybe you wanted the 2nd class to be Point3D.
2) Public Inheritance.
Your class Point2D: Point2D is private inheritance.
You may want to use public inheritance:
class Point3D : public Point2D
3) The this-> notation not needed.
When accessing members or functions of the class, access them directly:
int getX() const
{ return x; }
4) Don't duplicate parent functions.
No need to have parent functions repeated in the child class. For example, the child class doesn't need a getX method. This is what inheritance is for -- so you don't need to duplicate methods.
5) Child constructor calls parent constructor.
Your constructor should look something like this:
Point3D(int new_x, int new_y, int new_z)
: Point2D(new_x, new_y),
z(new_z)
{ ; }
Too many other errors to discuss in one post. The above should get you started and give you patterns to apply to other classes.

Making a custom class in c++ with constructor values

I'm still learning c++ and have a lot more experience with Java. In Java, creating a class can be as simple as this:
public class vertex
{
public double x, y, z;
public boolean eliminated = true;
public vertex(double x, double y, double z)
{
//vertex constructor
}
}
I'm trying to do something pretty much identical in C++, save for built in "get" functions for returning the values that should be set when creating an instance of the class, and want to know if my syntax is correct. Here is the class:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Vertex // Standard way of defining the class
{
public:
// This means that all of the functions below this(and any variables) are accessible to the rest of the program.
Vertex(double x, double y, double z);
//constructor
double getX();
double getY();
double getZ();
double x;
double y;
double z;
};
double Vertex:: getX()
{
return x;
}
I would also like advice on creating a class that has instances of custom classes like the one aboce within them.
public class vertex
{
public double x, y, z;
public boolean eliminated = true;
public vertex(double x, double y, double z)
{
//vertex constructor
}
}
To mimic this Java class as simply and as exactly as possible in C++ you would do this:
struct vertex{
double x, y, z;
bool eliminated = true;
vertex(double x, double y, double z){
//vertex constructor
}
};
Remarkably similar right? Note that the eliminated member is default initiated, which is new to C++11, so make sure you have an up to date compiler.
struct is used instead of class because all of your members are public anyway. You could also use class and the public: access specifier.
You do not need access methods (getters) because everything is public already, as in your Java class.
You don't need any includes in for your vertex class definitions and you definitely should NOT put a using declaration any header files.
To mimic Java method parameter passing you pass copies for primitives and non-const pointers (not references) for object instance parameters.
edit - fixed the silly issues that I missed and that the human compilers below spotted :)
This might help you out:
Vector3.h
#ifndef VECTOR3_H
#define VECTOR3_H
namespace myNamespace { // Add This Because Many Libraries may define a Vector3 Object
class Vector3 {
union {
float m_f3[3];
struct {
float m_fx;
float m_fy;
float m_fz;
};
};
inline Vector3();
inline Vector3( float x, float y, float z );
inline Vector3( float *pfv );
~Vector3();
// Add Your inline overloaded operators & inline function declarations here.
};
#include "Vector3.inl"
} // namespace myNameSpace
#endif // VECTOR3_H
Vector3.inl
// ------------------------------------------------------
// Vector3() - Default Constructor Sets All Values To 0
inline Vector3::Vector3() :
m_fx( 0 ),
m_fy( 0 ),
m_fz( 0 ) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes (x,y,z) Parameters
inline Vector3::Vector3( float x, float y, float z ) :
m_fx( x ),
m_fy( y ),
m_fz( z ) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes A Pointer To Type
inline Vector3::Vector3( float *pfv ) :
m_fx( pfv[0] ),
m_fy( pfv[1] ),
m_fz( pfv[2] ) {
} // Vector3
Vector3.cpp
#include "Vector3.h"
namespace myNamespace {
// ----------------------------------------------
// ~Vector3() - Default Destructor - Does Nothing
// Only Defined Here So That There Is Some Type Of Translation
// Unit In The Object File Generated When Compiling
Vector3::~Vector3() {
} // ~Vector3
} // namespace myNamespace
To use this:
main.cpp
#include <iostream>
#include "Vector3.h"
int main() {
using namespace myNamespace; // Forgot to add this line here.
Vector3 v1;
std::cout << "Default Constructor Used: " << std::endl;
std::cout << "v1 = ( " << v1.m_fx << ", "
<< v1.m_fy << ", "
<< v1.m_fz << " )" << std::endl;
// Or This Way Works Too
std::cout << "v1 = ( " << v1.m_f3[0] << ", "
<< v1.m_f3[1] << ", "
<< v1.m_f3[2] << " )" << std::endl;
Vector3 v2( 2.2f, 3.3f. 4.4f );
std::cout << "2nd Constructor Used: " << std::endl;
std::cout << "v2 = ( " << v2.m_f3[0] << ", "
<< v2.m_f3[1] << ", "
<< v2.m_f3[2] << " )" << std::endl;
float f3[3] = { 4.5f, 6.7f, 8.9f };
Vector3 v3( f3 );
std::cout << "3rd Constructor Used: " << std::endl;
std::cout << "v3 = ( " << v3.m_fx << ", "
<< v3.m_f3[1] << ", "
<< v3.m_fz << " )" << std::endl;
} // main
EDIT:
If you want a have a class that includes instances of another user define class then it is as simple as this:
SomeClass.h
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
namespace myNamespace {
class Vector3;
class SomeClass {
private:
Vector3 m_currentPosition;
Vector3 m_velocity;
Vector3 m_finalPosition;
public:
SomeClass( Vector3 initialPosition, Vector3 currentVelocity );
Vector3 getFinalPosition() const;
private:
void calculateFinalPosition();
};
} // namespace myNamespace
SomeClass.cpp
#include "SomeClass.h"
#include "Vector3.h"
namespace myNamespace {
SomeClass::SomeClass( Vector3 initialPosition, Vector3 currentVelocity ) :
m_currentPosition( initialPosition ),
m_velocity( currentVelocity ) {
calculateFinalPosition();
}
Vector3 SomeClass::getFinalPosition() const {
return m_finalPosition;
} // getFinalPosition
void SomeClass::calculateFinalPosition() {
// Perform The Appropriate Calculation Here To Find The Final Position
} // calculateFinalPosition
} // namespace myNamespace
However for this to work; this would require the Vector3 to have the overloaded operators, functions such as cross, dot etc., to be defined.

Undefined reference to my classes? C++ Beginner

To get a bit of practice with OOP i'm trying to make a Point class (has 2 ints, x & y) and a Line class (has 2 Points).
Now when i go to build my main.cpp i get errors like..
"undefined reference to `Point::Point(float, float)' " and
" undefined reference to `Line::Line(Point, Point)'"
At a loss as to why, perhaps you could take a brief look at my files? It'd be much appreciated!
Main.cpp
#include "Point.hpp"
#include "Line.hpp"
#include <iostream>
using namespace std;
int main()
{
Point p1(2.0f, 8.0f); // should default to (0, 0) as specified
Point p2(4.0f, 10.0f); // should override default
p1.setX(17);
if ( p1.atOrigin() && p2.atOrigin() )
cout << "Both points are at origin!" << endl;
else
{
cout << "p1 = ( " << p1.getX() << " , " << p1.getY() << " )" <<endl;
cout << "p2 = ( " << p2.getX() << " , " << p2.getY() << " )" <<endl;
}
Line line(p1, p2);
Point midpoint = line.midpoint();
cout << "p1 = ( " << midpoint.getX() << " , " << midpoint.getY() << " )" <<endl;
return 0;
}
Line.hpp
#ifndef _LINE_HPP_
#define _LINE_HPP_
#include "Point.hpp"
class Line{
public:
Line(Point p1, Point p2);
//void setp1(Point p1);
//void setp2(Point p2);
//Point getp1 finish
Point midpoint();
int length();
private:
int _length;
Point _midpoint;
Point _p1, _p2;
};
#endif
Line.cpp
#include "Line.hpp"
#include <math.h>
Line::Line(Point p1, Point p2) : _p1(p1), _p2(p2)
{
}
Point Line::midpoint()
{
_midpoint.setX() = (_p1.getX()+ _p2.getX()) /2;
_midpoint.setY() = (_p1.getY()+ _p2.getY()) /2;
}
int Line::length()
{
//a^2 + b^2 = c^2
_length = sqrt( ( (pow( _p2.getX() - _p1.getX(), 2 ))
+(pow( _p2.getY() - _p1.getY(), 2 )) ) );
}
Point.hpp
#ifndef _POINT_HPP_
#define _POINT_HPP_
class Point {
public:
Point( float x = 0, float y = 0);
float getX() const;
float getY() const;
void setX(float x = 0);
void setY(float y = 0);
void setXY(float x = 0, float y = 0);
bool atOrigin() const;
private:
float _x, _y;
};
#endif
Point.cpp
#include "Point.hpp"
Point::Point(float x, float y) : _x(x), _y(y)
{
}
float Point::getX() const
{
return _x;
}
float Point::getY() const
{
return _y;
}
void Point::setX(float x)
{
//if (x >= 0 &&
_x = x;
}
void Point::setY(float y)
{
//might want to check
_y = y;
}
void Point::setXY(float x , float y )
{
setX(x);
setY(y);
}
bool Point::atOrigin() const
{
if ( _x == 0 && _y == 0)
return true;
return false;
}
In C++, not only do you have to compile main.cpp, but you also have to compile your Line.cpp and Point.cpp files. Then, when you have them all compiled into object files, you must link the object files together. This is handled automatically by some other languages such as Java.
The exact instructions on how to do this will depend on which development environment you are using.
Your Point.cpp isn't being compiled or given to the linker, try including it in your build.