function overloading

R

rahul8143

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
 
B

benben

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
 
R

Rolf Magnus

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.
 
O

Old Wolf

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top