Classes and overloading!

I have a C++ issue with Classes and Overloading Functions that Im having difficulty with!

The instructions says,
Create a class named Dog with a string field for the dogs name...create a class Cat with a string field for its name. Write a program that declares one dog and one cat and assigns names to them.

- 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 2 overloaded functions named speak(...) If you pass a dog argument to speak functions, the relevant speak function should display the dogs name and a description of how a dog speaks(for example "spot says woof") if you pass a cat argument to speak functions, then the same for a cat (example "tiger says meow").

- in your main program, instantiate a dog instance and a cat instance, try to activate the member functions and the overload functions.




I am STUCK. This is the coding I've done thus far but I feel way off already. Any guidance would be much appreciated. I am clueless with the overloading/ activating functions


#include <iostream>
#include <string>
using namespace std;

class Dog
{
public:
std::string dogName;
void speak();
};

class Cat
{
public:
std::string catName;
void speak();
};

void Cat::speak()
{
cout<<"A cat speaks"<<endl;
}

void Dog::speak()
{
cout<<"A dog speaks"<<endl;
}

void main()
{
Dog oneDog;
oneDog.dogName = "Kasper";
return;

Cat oneCat;
oneCat.catName = "Jade";
return;

}


Thank you!
What you have so far is looking good, except for the void main(). You're returning after naming the dog which causes the cat never to be created since the program returns before that. Also you should use 'int main()' and 'return 0' instead of 'void main()'.
And you don't need to use the 'std::' prefix if you're using that namespace. Not wrong using it though.

Now for the overloaded functions:
What they want from you are two functions (not part of a class) of the same name that each take a different argument. One takes a dog and one a cat. The function that takes a dog prints "<dogName> says woof" and the cat one "<catName> says meow".
Thank you... that makes absolute sense with the main().

So in terms of the overload functions... I wouldnt include those in the class parameters?

So basically I would have a cout that looks like

cout<<"<dogName> says woof"<<endl; for the dog... and similar for the cat.

The only thing I cant wrap my head around from there is how to...."instantiate a dog instance and a cat instance, try to activate the member functions and the overload functions."

Is that done by using pointers? or...?
It doesnt seem like I'm supposed to use any cin>> type actions.... so how will the text appear?

(i.e. "a cat speaks" , "a dog speaks" , "Jade says meow", "Kasper says woof")

Thank you!
You would define these two overloaded function outside of any class, similar to how main is defined outside of any class and stands all by itself. main() is a function just like any other.

What they mean by 'instantiate' is simply creating objects of the classes you have defined. You have done just that in line 1 and 5 of your main function. 'Dog oneDog' created an object of the class 'Dog' and stored it in the variable 'oneDog'. That's basically what instantiating means.

By activating they mean that you call the functions you have defined. You do that by simply writing the name of the function followed by its arguments in parantheses.
For example to call the speak() member function for oneDog you'd write: 'oneDog.speak();' with no arguments inside the parantheses as speak() doesn't take any.
Calling a function causes the lines of code in the function to be executed successively. If it's a member function (a non-static function inside a class), then the function is always called on an object. In the above example 'speak()' is called on the object that is stored in the variable 'oneDog'.

No pointers or cins are needed :).

(btw I've been using those <> only to demonstrate what is supposed to be printed. You can't use these parantheses like that in the actual program, in case you didn't know.)
Wow you have been so much help I really appreciate it!

it seems like I have it covered for the member functions to produce the cat speaks, dog speaks text once I call on them with the oneDog.speak, oneCat.speak?

So basically the overloaded functions would be independent of what I've done thus far.
It also says to call them speak().... so would I have
speak()
{
cout<<dogname"says woof"<<endl;
}

and

speak()
{
cout<<catname"says meow"<<endl;
}

And then call the functions by......? haha I'm sorry Im not very good at this.
I feel like I would need to make arguments in the parantheses for the overloaded functions.
So I'm thinking....


two more functions:

void speak (Dog d)
{
//...
}
void speak (Cat c)
{
//...
}

The main should be like:

void main()
{
Dog oneDog;
oneDog.dogName = "Kasper";

Cat oneCat;
oneCat.catName = "Jade";

oneDog.speak();
oneCat.speak();
speak (oneDog);
speak (oneCat);



That look ok?
Edit: Yeah, look at me typing for minutes while you already solved that stuff all by yourself :)
Anyway you can still read the following for some clarification.

I feel like I would need to make arguments in the parantheses for the overloaded functions.

Exactly. Overloading means you define the function twice with different types of parameters. Which of the two is going to be executed is decided by the argument passed.
So your speak()s would look like this:
1
2
3
void speak(Dog aDog) { // aDog is an arbitrary name to reference the passed dog further in this function
  cout << aDog.dogName << " says woof" << endl; // the name of the dog that was passed to this function is printed followed by "says woof"
}

Now you do the same for the cat version.
If you then call the speak function in your main, the compiler will decide which function to call, depending on the type of object you passed. For example 'speak(oneDog);' will call the dog version of your function because you instantiated 'oneDog' as an object of the class 'Dog'. The actual names of the variable (=oneDog) and the function paramter (=aDog) are irrelevant. Instead their types are deciding which function is called.

Also what is important in C++: You can only reference objects/variables/classes/etc. that are declared above the point where you want to use them. In this case these overloaded speak() functions need to know the classes Dog and Cat, so they have to be placed below the class declarations. This is where forward declarations/prototypes come in handy.

Something you might want to know for further exercises:
When calling a function like I described here, the function actually creates a copy of the passed object. That means if you change a member of aDog in the speak() function, it won't affect oneDog in the main() function.
This, however is going pretty deep already. You're better off reading a book if you want to know more about that stuff.
Last edited on
Honestly I cant thank you enough... This is really so much simpler then I made it seem in my head... its so much easier to understand after you explained how to do it AND what it means.

Thanks for all your help!

Topic archived. No new replies allowed.