#include <iostream>
#include <cstdlib>
usingnamespace std;
class ClassA
{
public:
ClassA(int);
void print();
private:
int plus;
};
class ClassB: public ClassA
{
public:
ClassB(int,int);
void print();
void count();
private:
int minus;
int plus;
};
ClassA::ClassA(int PLUS)
{
plus=PLUS;
}
void ClassA::print()
{
cout << plus << endl;
}
//////////////////////////////////////////////////////
ClassB::ClassB(int PLUS, int MINUS) : ClassA(PLUS)
{
minus=MINUS;
}
void ClassB::print()
{
ClassA::print();
cout << minus << endl;
}
void ClassB::count()
{
++plus;
--minus;
}
int main()
{
ClassB b(5,5);
b.print(); // i get on the screen 5 and 5 like it should be
b.count(); // i do ++plus and --minus
b.print(); // so i expect to get 6 and 4. but what i get is 5 and 4. the ++plus doesnt work :(
system ("pause");
return 0;
}
Oh, didn't see that. Where you have "private", replace it with "protected". This means that inherited classes can see the variables for that class in the section, though the variables still aren't viewable outside of that. Try that.