C++ Classes and Overloaded Functions

I am trying to create a class name Dog with a string field for the Dog’s name. Create a class
Cat with a string field for the Cat’s name. Write a program that declares one Dog and one
Cat, and assign names to them.
But it not working
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
  #include <iostream>
#include <string>
using namespace std;

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

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

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

void Dog::speak()
{
dogName.speak();}
int main()
{
Dog oneDog;
oneDog.dogName = "Kasper";
return 0;

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

}
Is this what you are trying to achieve?

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
#include <iostream>
#include <string>
using namespace std;

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

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

void Cat::speak() const
{
	cout << catName << " squeaks\n";
}

void Dog::speak() const
{
	cout << dogName << " barks\n";
}

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

	Cat oneCat;
	oneCat.catName = "Jade";
	oneCat.speak();
}



Kasper barks
Jade squeaks


I don't know how far you have processed with classes, but consider also:

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
#include <iostream>
#include <string>
using namespace std;

class Dog {
	string dogName;

public:
	Dog() {}
	Dog(const string& nam) : dogName(nam) {}
	void speak() const;
};

class Cat
{
	string catName;

public:
	Cat() {}
	Cat(const string& nam) : catName(nam) {}
	void speak() const;
};

void Cat::speak() const
{
	cout << catName << " squeaks\n";
}

void Dog::speak() const
{
	cout << dogName << " barks\n";
}

int main()
{
	const Dog oneDog ("Kasper");
	oneDog.speak();

	const Cat oneCat("Jade");
	oneCat.speak();
}

Last edited on
seeplus
this is the question
Create a class name Dog with a string field for the Dog’s name. Create a class
Cat with a string field for the Cat’s name. Write a program that declares one Dog and one
Cat, and assign 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 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.
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
54
#include <iostream>
#include <string>
using namespace std;

class Dog {
	string dogName;

public:
	Dog() {}
	Dog(const string& nam) : dogName(nam) {}
	void speak() const;
};

class Cat
{
	string catName;

public:
	Cat() {}
	Cat(const string& nam) : catName(nam) {}
	void speak() const;
};

void Cat::speak() const
{
	cout << catName << " squeaks\n";
}

void Dog::speak() const
{
	cout << dogName << " barks\n";
}

void speak(const Cat& c)
{
	c.speak();
}

void speak(const Dog& d)
{
	d.speak();
}

int main()
{
	const Dog oneDog ("Kasper");
	oneDog.speak();

	const Cat oneCat("Jade");
	oneCat.speak();

	speak(oneDog);
	speak(oneCat);
}


but note there's better ways of doing this - which you'll probably come to later on.


Kasper barks
Jade squeaks
Kasper barks
Jade squeaks

I am learning also ATM. but if you make a class called animal and put all the common stuff you want all animals to have like name, speak and owner lets say. You can inherit animal and have all the functionality without having to rewrite the same code in every class.
That is to what I was referring (and polymorphism) above when I mentioned there were better ways.

Consider:

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
#include <iostream>
#include <string>
using namespace std;

class Animal {
private:
	string name;

protected:
	string getName() const { return name; }

public:
	Animal(const string& nam) : name(nam) {}
	virtual void speak() const = 0;
};

class Dog : public Animal {
public:
	Dog(const string& nam) : Animal(nam) {}
	void speak() const override final { cout << getName() << " barks\n"; }
};

class Cat : public Animal {
public:
	Cat(const string& nam) : Animal(nam) {}
	void speak() const override final { cout << getName() << " meeows\n"; }
};

void speak(const Animal& an)
{
	an.speak();
}

int main()
{
	const Dog oneDog ("Kasper");
	const Cat oneCat("Jade");

	speak(oneDog);
	speak(oneCat);
}



Kasper barks
Jade meeows

Last edited on
Topic archived. No new replies allowed.