Subclassing

Im having trouble subclassing a dog and cat as children of parent class Pet.
I am more used to writing Java and dont seem to be grasping the syntax of how to do this. Any help, advice, tips would be greatly greatly appreciated. I dont need you to do the assignment for me but point me in the right direction as how to subclass these classes using contructors(teacher said body of constructor has to be empty for full credit) and as well as how to write the virtual methods.

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
55
56
57
58
59
60
61
 #ifndef PET_H
#define PET_H
#include <iostream>
using namespace std;
namespace cs52
{
class Pet {
public:
Pet();
Pet(std::string name, int age);

void feed();
void pet(); 
void speak();

std::string getName() const;
int getAge() const;

protected:
virtual void onFeed() = 0;
virtual void onPet() = 0;
virtual void onSpeak() = 0;

private:
string my_Name;
int my_Age;
};
}
#endif
//////////////////////////////////////////
#include "pet.h"
#include <iostream>
using namespace std;
namespace cs52
{

 Pet::Pet(string name, int age)
{
  my_Name = name;
  my_Age = age;
}

void Pet::feed()
{}
void Pet::pet()
{}
void Pet::speak()
{}

string Pet::getName() const
{
	return my_Name;
}

int Pet::getAge() const
{
	return my_Age;
}

}


For simplicity i will only include the dog code cause this is conceptual once i get the syntax down i will be able to write the cat no problem.

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
#ifndef DOG_H
#define DOG_H
#include "pet.h"
#include <iostream>
using namespace std;
namespace cs52
{
class Dog : public Pet{

public:
Dog(string breed) : Pet(), my_DoggieBreed(breed) {}
Dog(string name, int age, string breed) : Pet(name,age), my_DoggieBreed(breed){}
string getBreed() const;

protected:

virtual void onFeed();
virtual void onPet();
virtual void onSpeak();

private:
string my_DoggieBreed;
};
#endif
//////////////////////////////////////////////////////////////////////
#include "Dog.h"
#include "pet.h"
#include <iostream>

using namespace std;

cs52::Dog::Dog(string name, int age, string breed) : Pet(name,age), my_DoggieBreed(breed){}

void onFeed()
{
	cout << name << "Chomps down" << endl;
}

void onPet()
{
	cout << name << "yelps!" << endl;
}

void onSpeak()
{
	cout << name << "barks!" << endl;
}


SAMPLE DRIVER AND OUTPUT GIVEN BY TEACHER

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Pet * pet = NULL;

Dog * mine = new Dog( "Muffin", 1, "Standard Poodle" );

Cat * yours = new Cat( "Sunset", 2, "Calico" );



cout << "These methods work on cat instances" << endl;

yours->feed( );

yours->pet( );

yours->speak( );

cout << "These methods work on dog instances" << endl;

mine->feed( );

mine->pet( );

mine->speak( );

pet = mine;



cout << "These methods work on pet pointers to a dog" << endl;

pet->feed( );

pet->pet( );

pet->speak( );

pet = yours;



cout << "These methods work on pet pointers to a cat" << endl;

pet->feed( );

pet->pet( );

pet->speak( );

// Why doesn't this compile???

// Pet * aPet = new Pet( "name", 12 );

////////////////////////////////////////////////////////////////////

Sample Output

These methods work on cat instances

Sunset walks away...

Sunset purrs...

Sunset walks away...

These methods work on dog instances

Muffin chows down!

Muffin yelps!

Muffin barks!

These methods work on pet pointers to a dog

Muffin chows down!

Muffin yelps!

Muffin barks!

These methods work on pet pointers to a cat

Sunset walks away...

Sunset purrs...

Sunset walks away...


I'm getting errors and things that i believe are easily fixable and messed up by syntax

Thank you guys a lot in advanced!!! I appreciate all the help!
Topic archived. No new replies allowed.