Hey everybody I want to ask a question about inheritance .
From the child classes could I access a protected data member in the parent class without using a member function or even friend function ?
if so , please Tell me How?.
#include<iostream>
#include<conio.h>
usingnamespace std;
class Parrent{
protected:
int aaa;
};
class Child : public Parrent{
public:
Child(Parrent x){
aaa=x.aaa; // here is the error
cout<<aaa;
}
};
here is the error
c:\users\f.b.m\desktop\c++\try\try\ddd.cpp(13): error C2248: 'Parrent::aaa' : cannot access protected member declared in class 'Parrent'
1> c:\users\f.b.m\desktop\c++\try\try\ddd.cpp(7) : see declaration of 'Parrent::aaa'
1> c:\users\f.b.m\desktop\c++\try\try\ddd.cpp(5) : see declaration of 'Parrent'
Or if you want to access a protected member in someone else's, of the same type as the base class..
There's something called friends, they let you have access to candy, slobber on candy (change), even though it does not belong to you. It belongs to a mother, not yours.
1 2 3 4 5 6 7 8 9 10 11 12 13
class Parent{
protected:
friendclass Child; //I like friends :)
int aaa;
};
class Child : public Parent{
public:
Child(Parent x){
aaa = x.aaa;
cout << aaa << " I have friends";
}
};