• 0

help me, i can't find out the bug!


Question

#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()

{

 ? ? ? ? Computer c=Pc();

 ? ? ? ? print(c);

 ? ? ? ? return 0;

}

void print(Computer &cc) ? ? /* if take out the "&" symbol , the program can run correctly. I want to know WHY???*/

{

 ? ? ? ? cc.name();

}

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

void print(Computer &cc)

This function is requesting a reference to a Computer object. So if you want to pass an argument to this function, you need to pass a reference (in your calling code you are attempting to pass a copy of an object). So you can either do this:

void print (Computer &cc)
{
    cc->name();
}

//Call the function like this:
print (&c);

Or you can do this:

void print(Computer cc)
{
    cc.name();
}

//Call the function like this:
print (c);

But you can't mix and match.

Link to comment
Share on other sites

  • 0
void print(Computer &cc)

This function is requesting a reference to a Computer object. So if you want to pass an argument to this function, you need to pass a reference (in your calling code you are attempting to pass a copy of an object). So you can either do this:

void print (Computer &cc)
{
 ? ?cc->name();
}

//Call the function like this:
print (&c);

Or you can do this:

void print(Computer cc)
{
 ? ?cc.name();
}

//Call the function like this:
print (c);

But you can't mix and match.

void print (Computer &cc)

{

cc->name();

}

print(&c);

**************

I don't think you are right.

This should be right

void print (Computer *cc)

{

cc->name();

}

and call function like : print(&c)

Link to comment
Share on other sites

  • 0

Nope, I'm not talking about a pointer.

void print(Computer &cc) {...} //This is expecting a reference to a Computer
void print(Computer *cc) {...} //This is expecting a pointer to a Computer

print(&cc); //IIRC, Both are called like this

Link to comment
Share on other sites

  • 0
Nope, I'm not talking about a pointer.

void print(Computer &cc) {...} //This is expecting a reference to a Computer
void print(Computer *cc) {...} //This is expecting a pointer to a Computer

print(&cc); //IIRC, Both are called like this

Your code can't work, you can try .

Link to comment
Share on other sites

  • 0

I repaired my codes again, now it can work correctly.

#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();

}

HI dannysmurf, my codes can prove:laugh:e :pbb:pem:pic:p' alt=':)' />ong... :p :p :p :p :laugh: :)

Edited by zhwcn
Link to comment
Share on other sites

  • 0
Possibly. It's been a long time since I've used a reference. But why are you using a reference?

Use reference , the program print "Pc",else you can only print "Computer".

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.