Function Overloading

Hello we got the following assignment:

 Declare a member function speak() for each of the class with different
implementation such as cout <<”A dog speaks.”<<endl; and cout <<”A cat
speaks.”<<endl;
 Write two overloaded functions named speak(…).
If you pass a Dog argument to speak functions, the relevant speak function should display the Dog’s name and a description of how dogs speak (for example, “Spot says woof”). If you pass a Cat argument to speak functions, then it should display the Cat’s name and a description of how cats speak (for example, “Tiger says meow”).
 In your main program, instantiate a dog instance and a cat instance, try to activate the member functions and the overloaded functions


Ive got everything done except the overloaded function part. I know how to overload functions using numeric values like an int and a double but not what he is asking what is another approach to this?

This is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <conio.h>
 

using namespace std;

class Dog {
public: 
   string name;
public:
void speak(){
    cout <<"\n A dog Says woof when it speaks!!";
}

};

class Cat {
public: 
    string name;
public:
void speak(){
    cout << "\n A cat Says meow when it speaks!!";
}

};

 

//prototype for dog speak
void speak();

//prototype for cat speak
void speak();

void main(){

Dog myDog;

myDog.name ="Barry";

Cat myCat;

myCat.name ="Tiger";

myDog.speak();

myCat.speak();

getch();

}
Last edited on
1
2
void speak(Dog);
void speak(Cat);

ahh thanks. once I pass speak(myDog); is there a way to retrive the variabler myDog.name inside the function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Dog {
public: 
   string name;
public:
void speak() const {
    cout <<"\n A dog Says woof when it speaks!!";
}

};

vois speak( const Dog &dog )
{
   std::cout << dog.name << std::endl;
  dog.speak();
 }
Topic archived. No new replies allowed.