The problem is that you can't include an object of a derived class in its parent . Reason being
1. During compilation first thing that the classes will
Be first compiled , there compiler checks to see that all names defined or declared in them are in the
scope of The classes
{/*see name lookup*/}
if not the then it issues an error that the
object included is not yet Defined or declared.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
class one
{
public:
one (string s1, string s2, int mrks) : marks (mrks)
{
names.setnames (s1, s2);
}
void print ()
{
cout <<"my names "<<names.getfirst ()<<" "<<names.getsecond ()
<<" my marks "<<marks;
}
private:
two names;
int marks;
}
class two
{
public:
two (string s1, s2): firstname (s1), secondname (s2){}
string getfirst() const {return firstname; }
string getsecond() const { return secondname;}
void setnames (string s1, string s2)
{
firstname=s1;
secondname=s2;
}
private:
string firstname;
string secondname;
}
int main ()
{
one student1 (string ("John"), string ("Davies"), 78);
student1.print (); /// this will not work lest you provide the pprototype of
/// the class two before the definition of class one.
}
|
2. It s obvious that a parent class have to be defined before it child classes
the during compilation the compiler runs through the step I described above
checking that all objects declared in it are in scope or that they are defined and in scope
In our case here the compiler comes accross an object
Accountant a;
in the parent class
Employee. Well it tries to find the definition or declaration of such a class and doesn't find it
and if it finds it it will check on it defination and find that an object of the Accountant class
will require complete defination of the parent class Employee....
so this will ne considered an error because you have included an incomplete object in the Employee
class {Accountant a; is incomplete coz it requires a complete defination of Employee class which
is still not defined; this checking will be redudant}
note you can't include an object of a child class in the parent class as it will be an incomplete object.
3. Solution
1 2 3
|
so i can access Accountant from my (Controller) class it gives me base class undefined
problem. Accountant.
|
This could be easily done if you could make you controller class a friend of Accountant. then you can have direct access to its members That way.