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<iostream>
-
- 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();
- deriveclass * b=new deriveclass();
-
- baseclass * aa=new deriveclass();
- deriveclass * bb=new baseclass();
-
- 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?