I m a student. Today I Give the Interview and tell me something new about inheritance
I said the derived class can not access the base class property or field because below program prove that is why I tell the derived class can not access the base class property or field:
- using namespace std;
- #include
- class baseclass
- {
- public:
- int a;
- baseclass()
- {
- std::cout << "baseclass\n";
- }
- };
- class deriveclass: public baseclass
- {
- public:
- deriveclass()
- {
- std::cout << "derived class\n";
- }
- };
- int main()
- {
- baseclass * a=new baseclass(); //ok
- deriveclass * b=new deriveclass(); //ok
- baseclass * aa=new deriveclass(); //ok
- deriveclass * bb=new baseclass(); //not ok
- return 0;
- }
Link:https://onlinegdb.com/SyfhRDF_U
error:
main.cpp: In function ‘int main()’: main.cpp:30:36: error: invalid conversion from ‘baseclass*’ to ‘deriveclass*’ [-fpermissive] deriveclass * bb=new baseclass(); //not ok
then Interviewer tells me which way to access the base class function or variable?
help?
Rajanikant HawaldarPosted Apr 19, 2020, 2:49 AM
-------Posted Apr 19, 2020, 6:26 AM
Rajanikant HawaldarPosted Apr 19, 2020, 3:05 AM