Thursday, July 30, 2009

In c++ how base class and child classes are referenced each other?

is base class referece variable can hold the child class object?


or


the child class obj can hold the base class obj?








another question:can we define constroctor if a class out side of class?

In c++ how base class and child classes are referenced each other?
class ChildClass : public BaseClass


{


// Class definition here


};


Yes the constructor or any other function can be defined outside, as long as it was originally prototyped in the class definition:





ChildClass::ChildClass()


{


// Function definition here


}
Reply:Super classes are generally used to define general properties, while derived classes implement the more specific details... for example





class Human{


char sex;


int age;


}





class Student:Human{


int schoolID;


}





With this kind of declaration, the sub-class inherits the attributes of the super class and add its own ones...





In terms of the constructor, in c++ you usually have two different files a .h and a .cpp file... in the .h file you do all the description of attributes and functions while in the .cpp file you actually implement all stuff...





cheers.


No comments:

Post a Comment