I am trying to create a class Line which consists of two objects of another class Point:
class Point
{
double x, y, z;
public:
// constructor from 3 values
Point(double x, double y, double z);
// copy constructor
Point(const Point &p);
// method display
void display();
};
// constructor from 3 values
Point::Point(double x, double y, double z)
: x(x), y(y), z(z)
{}
// copy constructor
Point::Point(const Point &p)
: x(p.x), y(p.y), z(p.z)
{}
void Point::display()
{
cout << "Point(" << x << ", " << y << ", " << z << ")\n";
}
class Line
{
Point pnt1, pnt2;
public:
// constructor from 2 points
Line(Point& pnt1, Point& pnt2);
// method display line
void display();
};
// constructor from 2 points
Line::Line(Point& pnt1_, Point& pnt2_)
: pnt1(pnt1_), pnt2(pnt2_)
{}
// method display line
void Line::display()
{
cout << "Line(Point(" << pnt1.x << ", " << pnt1.y << ", " << pnt1.z << ")" << ", Point(" << pnt2.x << ", " << pnt2.y << ", " << pnt2.z << ")\n";
}
And here is the main:
#include <iostream>
#include <cstdio>
#include "geometryitems.cpp"
using namespace std;
int main()
{
// initialise object Point
cout << endl << "Point initialisation:" << endl;
Point pnt = Point(0.0, 0.0, 0.0);
cout << "pnt = "; pnt.display();
Point pnt2 = Point(1.0, 1.0, 1.0);
cout << "pnt2 = "; pnt2.display();
// initialising object Line
cout << "Line initialisation:" << endl;
Line line = Line(pnt, pnt2);
line.display();
return 0;
}
The points work fine but the line gives me errors that class "Point" has no members named a1, b1, c1, a2, b2, c2.
How to create class Line using objects of class Point? Thank you.
Update
I have updated the code using the copy constructor but it still talking about private x, y, z. Any ideas?
Pointmembers aspublic, and usex,y,z.a1,b1etc. are not members ofPoint.lineclass is the problem. Change it