zhwcn Posted September 10, 2004 Share Posted September 10, 2004 (edited) class Computer { public: virtual void name() const; } class Pc: public Computer { public: virtual void name() const; } void Computer::name() const { cout<<"Computer"<<endl; } void Pc::name() const { cout<<"Pc"<<endl; } void print(Computer); main() { Computer c = new Pc(); print?; return 0; } void print(Computer cc) { cc.name(); } Please tell us the result of the program???? And Why??? Edited September 10, 2004 by zhwcn Link to comment Share on other sites More sharing options...
0 Rudy Posted September 10, 2004 Share Posted September 10, 2004 you should have left the indentation this makes it 10x harder to read Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 10, 2004 Share Posted September 10, 2004 The program should print "Computer." Since the parameter is an object of type Computer and not an object of type Pc, it will call Computer::name(). In order to call Pc::name(), the compiler would have to infer an upcast of cc to Pc, which may not be a legal cast (whereas downcasting an instance of Pc to Computer would always be legal). Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 10, 2004 Author Share Posted September 10, 2004 The program should print "Computer." Since the parameter is an object of type Computer and not an object of type Pc, it will call Computer::name(). In order to call Pc::name(), the compiler would have to infer an upcast of cc to Pc, which may not be a legal cast (whereas downcasting an instance of Pc to Computer would always be legal). ------------------------------------------ You are right. But maybe you confuse "Upcasting" and "Downcasting" . Make a derived class object to be a base class object is "Upcasting", and it is legal, contrarily it is "Downcasting". Link to comment Share on other sites More sharing options...
0 boomn Posted September 10, 2004 Share Posted September 10, 2004 How do you get a double post from 2 different people? :wacko: Link to comment Share on other sites More sharing options...
0 amoeba Posted September 10, 2004 Share Posted September 10, 2004 How do you get a double post from 2 different people? :wacko: He hasn't found the quote button. Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 10, 2004 Author Share Posted September 10, 2004 He hasn't found the quote button. :) :) :D Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 10, 2004 Share Posted September 10, 2004 How do you get a double post from 2 different people? :wacko: lol, especially since one was the guy asking the question. But why did he "quote" and not add anything to the post? Link to comment Share on other sites More sharing options...
0 mew2u Posted September 11, 2004 Share Posted September 11, 2004 accually he did post something extra, look: The program should print "Computer." Since the parameter is an object of type Computer and not an object of type Pc, it will call Computer::name(). In order to call Pc::name(), the compiler would have to infer an upcast of cc to Pc, which may not be a legal cast (whereas downcasting an instance of Pc to Computer would always be legal). ------------------------------------------ You are right. But maybe you confuse "Upcasting" and "Downcasting" . Make a derived class object to be a base class object is "Upcasting", and it is legal, contrarily it is "Downcasting". you see, pWned Link to comment Share on other sites More sharing options...
0 fault Posted September 11, 2004 Share Posted September 11, 2004 oh, haha what the... didn't even see that, looked like a signature to me at first glance! :ninja: Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 11, 2004 Share Posted September 11, 2004 oh, haha what the... didn't even see that, looked like a signature to me at first glance! :ninja: lol, yeah, ----- isn't the best way to separate :wacko: Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 11, 2004 Author Share Posted September 11, 2004 lol, yeah, ----- isn't the best way to separate :wacko: Yes , you are right. At first, I didn't know the "Quote" button mean this. :laugh: :laugh: Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 12, 2004 Author Share Posted September 12, 2004 do a little changes , the program can print "Pc" :) :) :D #include<iostream.h> class Computer { public: ? ? ?virtual ?void ?name() const; }; class Pc: public Computer { public: ? ? ?virtual void name() const; }; void Computer::name() const { ? ? ? ?cout<<"Computer"<<endl; } void Pc::name() const { ? ? ? ?cout<<"Pc"<<endl; } void print(Computer &); main() { ? ? ? ?Pc c; ? ? ? ?print(c); ? ? ? ?return 0; } void print(Computer &cc) ? ? { ? ? ? ?cc.name(); } Link to comment Share on other sites More sharing options...
0 jetru Posted September 12, 2004 Share Posted September 12, 2004 i wonder why... You're passing a Pc and the print() takes a computer ref, so should'nt the compiler call the computer name()? Or there is no computer name() in a PC?? Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 12, 2004 Share Posted September 12, 2004 i wonder why... You're passing a Pc and the print() takes a computer ref, so should'nt the compiler call the computer name()? Or there is no computer name() in a PC?? No. If you pass a reference (or by pointer), you're directly calling via the passed object's memory, so there is no casting involved. Link to comment Share on other sites More sharing options...
Question
zhwcn
class Computer { public: virtual void name() const; } class Pc: public Computer { public: virtual void name() const; } void Computer::name() const { cout<<"Computer"<<endl; } void Pc::name() const { cout<<"Pc"<<endl; } void print(Computer); main() { Computer c = new Pc(); print?; return 0; } void print(Computer cc) { cc.name(); }Please tell us the result of the program???? And Why???
Edited by zhwcnLink to comment
Share on other sites
14 answers to this question
Recommended Posts