0

I've tried making a basic C++ program with some classes and ran into a problem. The program looks like:

#include<iostream>
using namespace std;

class A {
public:
    int i;
    A(int ai) {this->i = ai;}
    A() {}
};

class B : A {
public:
    A aa;
    B(A &a) : A(a.i) {
        aa = a;
    }
};

int main()
{
    A a(5);
    B b(a);

    cout << "Hello World!" << b.i;
    return 0;
}

The program fails to compile with:

In function 'int main()':
Line 6: error: 'int A::i' is inaccessible
compilation terminated due to -Wfatal-errors.

But the variable i is public in the class A. What am I doing wrong?

Barry
  • 286,269
  • 29
  • 621
  • 977

1 Answers1

4

You're inheriting A privately:

class B : A {
       ^^^^^^

You need to inherit A publicly:

class B : public A {
       ^^^^^^^^^^^^^
Barry
  • 286,269
  • 29
  • 621
  • 977