| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
31 Jul 2005 04:32:44 AM |
| Object: |
function overloading |
hello,
I have got following code form some university site that has C++
tutorials, i read it but has some problems
1)why the following code is rejected by the the compiler?
class Pet {
protected:
string _name;
public:
Pet(string name) : _name(name) {}
virtual string sound() const = 0;
virtual void speak() const {
cout << _name << ": " << sound() << "!\n";
}
};
class Dog : public Pet {
public:
Dog(string name) : Pet(name) {}
string sound() const { return "woof"; }
void speak() const { // virtual is optional
Pet::speak();
cout << '(' << _name << " wags tail)\n";
}
};
class Cat : public Pet {
public:
Cat(string name) : Pet(name) {}
virtual string sound() const { return "miao"; }
};
void chase(Pet &x, Cat &y) { ... }
void chase(Dog &x, Pet &y) { ... }
int main() {
Dog buster;
Cat tom;
chase(buster, tom); // ambiguous error!
}
2)why slicing doesnot occur in following declarations
Cat felix;
Pet *p = &felix;
p->speak(); // miao
.
|
|
| User: "benben" |
|
| Title: Re: function overloading |
31 Jul 2005 08:06:42 AM |
|
|
<rahul8143@gmail.com> wrote in message
news:1122802364.053452.31640@g44g2000cwa.googlegroups.com...
hello,
I have got following code form some university site that has C++
tutorials, i read it but has some problems
1)why the following code is rejected by the the compiler?
class Pet {
protected:
string _name;
public:
Pet(string name) : _name(name) {}
virtual string sound() const = 0;
virtual void speak() const {
cout << _name << ": " << sound() << "!\n";
}
};
class Dog : public Pet {
public:
Dog(string name) : Pet(name) {}
string sound() const { return "woof"; }
void speak() const { // virtual is optional
Pet::speak();
cout << '(' << _name << " wags tail)\n";
}
};
class Cat : public Pet {
public:
Cat(string name) : Pet(name) {}
virtual string sound() const { return "miao"; }
};
void chase(Pet &x, Cat &y) { ... }
void chase(Dog &x, Pet &y) { ... }
int main() {
Dog buster;
Cat tom;
chase(buster, tom); // ambiguous error!
}
Because both
void chase(Pet&, Cat&);
and
void chase(Dog&, Pet&);
are valid candidates for the invocation and the compiler can't decide which
is more proper. buster is a Pet and a Dog, and tom is a Cat, and a Pet at
the same time.
2)why slicing doesnot occur in following declarations
Cat felix;
Pet *p = &felix;
p->speak(); // miao
What is slicing? You are only pointing a pointer to Pet to an instance of
Cat. p is nothing else than the memory address of the start of the Cat
object felix.
Ben
.
|
|
|
|
| User: "Old Wolf" |
|
| Title: Re: function overloading |
31 Jul 2005 06:54:47 PM |
|
|
wrote:
1)why the following code is rejected by the the compiler?
class Pet {
protected:
string _name;
Unknown symbol 'string' (it expected a type name)
public:
Pet(string name) : _name(name) {}
virtual string sound() const = 0;
virtual void speak() const {
cout << _name << ": " << sound() << "!\n";
Unknown symbol 'cout'
}
};
class Dog : public Pet {
public:
Dog(string name) : Pet(name) {}
string sound() const { return "woof"; }
void speak() const { // virtual is optional
Pet::speak();
cout << '(' << _name << " wags tail)\n";
}
};
class Cat : public Pet {
public:
Cat(string name) : Pet(name) {}
virtual string sound() const { return "miao"; }
};
void chase(Pet &x, Cat &y) { ... }
Syntax error (... is not a statement)
void chase(Dog &x, Pet &y) { ... }
int main() {
Dog buster;
Cat tom;
chase(buster, tom); // ambiguous error!
Ambiguous call: the compiler cannot tell whether you want to
call chase(Pet &, Cat &) or chase(Dog &, Pet &), and it sees
no reason to prefer one or the other.
}
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: function overloading |
31 Jul 2005 11:04:11 AM |
|
|
wrote:
hello,
I have got following code form some university site that has C++
tutorials, i read it but has some problems
1)why the following code is rejected by the the compiler?
What do you mean by "rejected by the compiler"?
class Pet {
protected:
string _name;
public:
Pet(string name) : _name(name) {}
virtual string sound() const = 0;
virtual void speak() const {
cout << _name << ": " << sound() << "!\n";
}
};
class Dog : public Pet {
public:
Dog(string name) : Pet(name) {}
string sound() const { return "woof"; }
void speak() const { // virtual is optional
Pet::speak();
cout << '(' << _name << " wags tail)\n";
}
};
class Cat : public Pet {
public:
Cat(string name) : Pet(name) {}
virtual string sound() const { return "miao"; }
};
void chase(Pet &x, Cat &y) { ... }
void chase(Dog &x, Pet &y) { ... }
int main() {
Dog buster;
Cat tom;
chase(buster, tom); // ambiguous error!
There are two possible conversions that are equally good. Which one would
you expect the compiler to choose from the two functions?
}
2)why slicing doesnot occur in following declarations
Cat felix;
Pet *p = &felix;
p->speak(); // miao
Slicing happens when you copy a derived class object into a base class
object. Nothing is copied here, so no slicing happens.
.
|
|
|
|

|
Related Articles |
|
|