user1329187
user1329187

Reputation:

Create a class using objects of another class

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?

Upvotes: 0

Views: 179

Answers (4)

Smii
Smii

Reputation: 38

You can get a quick and dirty solution with getters for your private data members.

   double getX() { return x ; };
   double getY() { return y ; };
   double getZ() { return z ; };

I changed as well the Line cstor.

   // constructor from 2 points
   Line::Line(Point& _pnt1, Point& _pnt2)
   : pnt1(_pnt1), pnt2(_pnt2)
   {}

And changed the call to these getters in your cout.

   cout << "Line(Point(" << pnt1.getX() << ", " << pnt1.getY() << ", " << pnt1.getZ() << ")" << ", Point(" << pnt2.getX() << ", " << pnt2.getY() << ", " << pnt2.getZ() << ")\n";

Check it out: http://ideone.com/aHmin9

Upvotes: 0

Biraj Borah
Biraj Borah

Reputation: 101

Your Question has the solution in it : class "Point" has no members named a1, b1, c1, a2, b2, c2.

So your class Contains x, y and z instead....Try to print it using x, y and z

Make sure the members are public or use getters and setters.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409442

Add a copy-constructor to your Point class:

class Point
{
public:
    Point(const Point& p)
        : x(p.x), y(p.y), z(p.z)
        {}

    ...
};

Then you can use that for the Line constructor:

Line::Line(const Point& p1, const Point& p2)
    : pnt1(p1), pnt2(p2)
{}

The Line class don't need separate point variables a1, b1, c1 etc. And of course the actual Point objects don't have member variables named like that, which you try to access. Also note that the member variable in Point are private, you the Line::display function can't access them.

Upvotes: 2

Shashwat Kumar
Shashwat Kumar

Reputation: 5297

Class point has member x,y,z instead of a1,b1,c1. It should be

void Line::display()
{
    cout << "Line(Point(" << pnt1.x << ", " << pnt1.y << ", " << pnt1.z << ")" << ",  Point(" << pnt2.x << ", " << pnt2.y << ", " << pnt2.z << ")\n";
}

Upvotes: 0

Related Questions