• 0

a C++ exercise


Question

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 zhwcn
Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0
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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.