Thanks for both of your answers. However, as a beginner :-) , I don't get your points yet . Let me elaborate a little more about my two questions, 1. Class A has a public member function - print. Why is it wrong for Class B , which is not a subclass of class A, to include class A as its private member ? 2. When the following program runs, class B object instantiates . However, the pointer pa is not initialized, therefore, it can point anywhere. Even point to NULL. However, statement b1.pa->print(); never fails to execute. Why?
Thanks again, On 6/18/08, Muhammad Umar Hayat <[EMAIL PROTECTED]> wrote: > > Ans2)data member and member function of a class are the property of it's > objects/ > > >so the print function is a also a property of pointer 'pa' . > > On 6/18/08, manav singh <[EMAIL PROTECTED] <manav861%40yahoo.com>> wrote: > > > > ans1) By default, all members of a class declared with the class > > keyword have private access for all its members. Therefore, any member > > that is declared before one other class specifier automatically has > > private access. thats why your code is producing error if u comment the > > public keyword > > > > ans2)who says that pa is pointing anything.It is pointing to somewhere. > but > > you see it can create problems in further program. > > --- On Wed, 6/18/08, frankzhu_2001 <[EMAIL > > PROTECTED]<frank.zhu.mingyu%40gmail.com> > <frank.zhu.mingyu%40gmail.com>> > > wrote: > > From: frankzhu_2001 <[EMAIL PROTECTED]<frank.zhu.mingyu%40gmail.com> > <frank.zhu.mingyu%40gmail.com> > > > > > Subject: [c-prog] class composition > > To: [email protected] <c-prog%40yahoogroups.com> <c-prog% > 40yahoogroups.com> > > Date: Wednesday, June 18, 2008, 7:56 PM > > > > Hi, > > > > I have two questions, > > > > 1. The following program will not compile if the 'public' is not > > > > defined. why? > > > > 2. Since pa is pointed to nowhere, is it a safe to use pa without > > > > the instanciate the class a? > > > > thanks, > > > > --- code starts here --- > > > > #include<iostream> > > > > using namespace std; > > > > class a{ > > > > public: > > > > void print(void) { cout << "a print\n";} > > > > }; > > > > class b{ > > > > public: // error if this line is commented out. > > > > a* pa; > > > > }; > > > > int main(void) > > > > { > > > > b b1; > > > > b1.pa->print( ); > > > > } > > > > > > > > > > > > > > > > > > > > > > > > [Non-text portions of this message have been removed] > > > > > > > > [Non-text portions of this message have been removed] > > > [Non-text portions of this message have been removed]
