0

Class Point is working correctly, It is creating x, y point. Code:

point.h file

#ifndef POINT_H
#define POINT_H

namespace pt
{
    class Point
    {
        int x, y;
    public:
        Point();
        Point(int x, int y);
        int getX();
        int getY();
    };
}
#endif // POINT_H

point.cpp file

#include "point.h"

pt::Point::Point()
{
    this->x = this->y = 0;
}

pt::Point::Point(int x, int y)
{
    this->x=x;
    this->y=y;
}

int pt::Point::getX()
{
    return this->x;
}

int pt::Point::getY()
{
    return this->y;
}

Meanwhile when I try to create new Point3D class in main that will inherit from Point x, y coordinates and add z to create third dimension, new constructor cant get access to x, y of Point class. Errors are: 1. 'int pt::Point::x' is private at first and second this-> in Point3D constr. 2. Both are 'out of context'

main.cpp

#include <iostream>
#include "point.h"

int main()
{
    class Point3D : public pt::Point
    {
        int z;
    public:
        getZ()
        {
            return this->z;
        }

        Point3D(int x ,int y, int z)
        {
            this->x=x;
            this->y=y;
            this->z=z;
        }
    };
    return 0;
}

Thanks for help.

Stahp
  • 972
  • 1
  • 9
  • 16
  • I made mistake in the title - im sorry. I meant inherit class :( – Stahp Jul 04 '18 at 12:16
  • 4
    If you made a mistake in your title, [edit] your post to fix it. Commenting on it is just white noise. – StoryTeller - Unslander Monica Jul 04 '18 at 12:19
  • 2
    A derived class can't access `private` members of a base class. They can access `protected` members. Derived classes are not friends. Any parent who has had hostile children would understand the need to keep some things private, even from descendants. It goes the same way, philosophically, in software design – Peter Jul 04 '18 at 12:20
  • @Peter *Descendants can't access your privates, but good friends can!* -- It's worn out, but you deserved it :p – Quentin Jul 04 '18 at 12:49
  • @Peter any child who has hostile parents understands that need, too :) – Olivier Sohn Jul 04 '18 at 20:59

1 Answers1

2

To make x and y accessible to derived classes, you should make them protected:

class Point
{
protected:
    int x, y;
public:
    Point();
    Point(int x, int y);
    int getX();
    int getY();
};

By default, the visibility of a class member is private (note that this is different from the struct default where a struct member is public by default). On that topic, see this answer.

And as a side note, the idiomatic way to initialize x and y would be to write:

Point3D(int x ,int y, int z) : pt::Point(x,y)
{
    this->z=z;
}

Then, you don't need to make x and y protected, they can remain private.

You can even write it like that:

Point3D(int x ,int y, int z) : pt::Point(x,y), z(z)
{}
Olivier Sohn
  • 1,292
  • 8
  • 18
  • 1
    The side note is superior to the main note. – Bathsheba Jul 04 '18 at 12:28
  • 3
    I think the idiomatic way can be made more idiomatic. Or in other words: "y u ignore z"? – StoryTeller - Unslander Monica Jul 04 '18 at 12:29
  • @StoryTeller I didn't get it, what do you mean? – Olivier Sohn Jul 04 '18 at 12:33
  • Ah, apologies. I was indulging my like for internet memes. Like [this](http://knowyourmeme.com/memes/y-u-no-guy) and [this](http://knowyourmeme.com/memes/y-u-do-dis). – StoryTeller - Unslander Monica Jul 04 '18 at 12:35
  • @Bathsheba I agree, but I wanted to answer the question first, in order not for the answer to be flagged as "off-topic" ! – Olivier Sohn Jul 04 '18 at 12:35
  • 2
    @OlivierSohn: It wouldn't be. If I were you, I'd start with "The best way, by a country mile, is to write your constructor", followed by your third snippet, then the text that currently forms the first part of your answer. – Bathsheba Jul 04 '18 at 12:36
  • @Bathsheba I like it the way it is, because it gradually leads to the best solution imho, which is probably easier to follow for the author of the question. – Olivier Sohn Jul 04 '18 at 12:40
  • @OlivierSohn: Well it's your answer innit. But do bear in mind that we are building a Q & A site here; helping the OP ought to be regarded as a corollary. And I'd upvote if you re-ordered it. – Bathsheba Jul 04 '18 at 12:41
  • @Bathsheba with the current wording of the question, I think my answer is quite appropriate : the title says that a member cannot be accessed, and I explain how to access the member. Straightforward. Then, I explain that there are ways to keep encapsulation here. – Olivier Sohn Jul 04 '18 at 12:44