C ++ OOP Question

I am doing a C ++ quiz and I need to say what is wrong in the following code:

class Base { friend class SubClass; int n; virtual int getN() { return n; } }; class SubClass: public Base { public: SubClass() {} SubClass(const SubClass& s) {} }; int main() { SubClass sc; if(sc.getN() <= 5) { int x = sc.getN(); } return 0; } 

In addition, n is not initialized, and perhaps the object should be created using the Base class pointer, what else might be wrong?
When I run it, I get this error:

  error: 'virtual int Base::getN()' is private 
+6
source share
5 answers

As by default, each member of class 1 is private , getN in the base class is declared private .

Make getN publicly available as:

 class Base { friend class SubClass; int n; public: //<--------------------- you forgot this virtual int getN() { return n; } }; 

1. I mean the class defined with the class keyword. Note that a class can be defined using the struct and union keywords, in accordance with the C ++ standard.


EDIT:

If you think because SubClass is a friend of Base , so it can access private members of Base from the outside, then this is wrong. friend means that SubClass member SubClass can access private members of the Base class.

However, if you make main() friend of Base , then your code will work:

  class Base { friend int main(); //make main() friend of Base //... }; 

Now from main() you can access any private Base members!

Watch this demo: http://www.ideone.com/UKkCF

+13
source

Your compiler should give you some hints ...

 % 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' 
+4
source

The error message says everything: getN implicitly private , which is used by default for classes in C ++.

Using

 class Base { // ... public: virtual int getN(); }; 

to make it publicly available. The fact that SubClass is a friend of Base and that getN is called through a SubClass instance SubClass not matter here - getN simply not accessible from inside main() , where it is called from. However, you can make main a friend Base .

+3
source

function

 virtual int getN() { /*...*/ } 

is private (default) in your base class. Since it is not overloaded within you by SubClass, it remains closed when you call it in its main function.

If you want to make it legal, make it public in the class:

 public: virtual int getN() { /*...*/ } 
+2
source
 class Base { friend class SubClass; int n; public: virtual int getN() { return n; } }; 

class members are private by default

+2
source

Source: https://habr.com/ru/post/887926/


All Articles