C++ OOP quiz question
As by default every member of a class1 is private
, getN
in the base class is declared private
.
Make getN
public as:
class Base {
friend class SubClass;
int n;
public: //<--------------------- you forgot this
virtual int getN()
{
return n;
}
};
1. I mean, a class defined with the keyword class
. Note that class can be defined with the keyword struct
and union
as well, according to the C++ Standard.
EDIT:
If you think because SubClass
is a friend of Base
, so it can access private members of Base
from outside, then that is wrong. friend
means member functions of SubClass
can access private members of Base
class.
However, if you make main()
friend of Base
, then your code would work:
class Base {
friend int main(); //make main() friend of Base
//...
};
Now from main()
, any private members of Base
can be accessed!
See this demo : http://www.ideone.com/UKkCF
Your compiler should give you some clues...
% g++ -Wall -Wextra -Wshadow -Weffc++ test.cpp
test.cpp:1: warning: ‘class Base’ has virtual functions but non-virtual destructor
test.cpp:10: warning: ‘class SubClass’ has virtual functions but non-virtual destructor
test.cpp: In constructor ‘Base::Base()’:
test.cpp:1: warning: ‘Base::n’ should be initialized in the member initialization list
test.cpp: In constructor ‘SubClass::SubClass()’:
test.cpp:12: note: synthesized method ‘Base::Base()’ first required here
test.cpp: In copy constructor ‘SubClass::SubClass(const SubClass&)’:
test.cpp:13: warning: base class ‘class Base’ should be explicitly initialized in the copy constructor
test.cpp: At global scope:
test.cpp:13: warning: unused parameter ‘s’
test.cpp: In function ‘int main()’:
test.cpp:4: error: ‘virtual int Base::getN()’ is private
test.cpp:19: error: within this context
test.cpp:4: error: ‘virtual int Base::getN()’ is private
test.cpp:21: error: within this context
test.cpp:21: warning: unused variable ‘x’
The error message says it all: getN
is implicitly private
, which is the default for classes in C++.
Use
class Base {
// ...
public:
virtual int getN();
};
to make it a publicly accessible member. The facts that SubClass
is a friend of Base
and that getN
is called through an instance of Subclass
don't matter here - getN
is simply not accessible from within main()
, where it is called from. You could make main
a friend
of Base
, however.