validate my understanding (polymorphism, inheritance, static class usage, heap and stack usage seperation)

Pages: 12
updated code using std::string types rather than char*. string additionsconcatenations are possible now and I see what you originally meant by dynamic types.

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
// polymorphism test.cpp : Defines the entry point for the console application.
//

#include <iostream>
using std::cout;
using std::string;

class animal
{
protected:
	string message;
public:
	string name;
	animal()
	{
		this->message = "Hello from ";
		this->name = "SOMEANIMAL. I don't have a name yet :(";
		cout << this->message + this->name + "\n";
	}
	virtual void eats() = 0;
	virtual ~animal()
	{
		this->message = "Goodbye from ";
		if (this->name == "SOMEANIMAL. I don't have a name yet :(")
		{
		    this->name = "SOMEANIMAL.  REALLY? NO ONE EVER GAVE ME A NAME!? :(";
		}
		cout << this->message + this->name + "\n";
	}
};

class dog : public animal
{
public:
	void eats()
	{
		this->message = "dog bones";
		cout << this->name + " loves: " + this->message + "\n";
	}
};

class cat : public animal
{
public:
	void eats()
	{
		this->message = "cat food";
                cout << this->name + " loves: " + this->message + "\n";
	}
};

//a helper class which sets an animals name member
class animalNamer
{
public:
	static void setName(string param, animal *obj)
	{
		obj->name = param;
		cout << "I have a name now! It is: " + obj->name + "\n";
	}
};

int main()
{
	//two animals created on the heap and their names set with the static helper
	dog *Scooby = new dog;
	animalNamer::setName("Scooby", Scooby);
	cat *Garfield = new cat;
	animalNamer::setName("Garfield", Garfield);
	//eats is a pure virtual function which must be implemented in all derived animals
	Scooby->eats();
	Garfield->eats();
	//clean up the heap
	delete Scooby;
	delete Garfield;
	//an animal created on the stack
	dog StackDog;
	StackDog.eats();
	return 0;
}
Last edited on
Why is the message a member of the animal? You set it every time that you use it, so you could use a function-local variable or just a string literal:
1
2
3
4
5
6
7
8
9
10
void dog::eats()
{
  std::string message = "dog bones";
  cout << this->name + " loves: " + message + "\n";
}
// or
void dog::eats()
{
  cout << this->name + " loves: dog bones\n";
}


Use the class initializer list:
1
2
3
4
5
animal::animal()
: message("Hello from "), name("SOMEANIMAL. I don't have a name yet :(")
{
  cout << this->message + this->name + "\n";
}


Reconsider the standard library's smart pointers:
http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
http://en.cppreference.com/w/cpp/memory/unique_ptr
http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared
http://en.cppreference.com/w/cpp/memory/shared_ptr
When you use the class initializer list, how do you know the type of message, or name, as you have written them? Or is that just explicitly defined in the declaration?

shouldn't it be:

1
2
3
4
5
animal::animal()
: string message("Hello from "), string name("SOMEANIMAL. I don't have a name yet :(")
{
  cout << this->message + this->name + "\n";
}
Those 'message' and 'name' are members of the class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class animal {
  // member declarations
  std::string message; // message is a string
  std::string name;    // name is a string
public:
  animal() // default constructor
  : message("Hello from "), name("SOMEANIMAL. I don't have a name yet :(")
  {
      cout << this->message + this->name + '\n';
  }

  animal( const std::string & name ) // another constructor
  : message("Hello from "), name( name )
  {
      cout << this->message + this->name + '\n';
  }
};


However, the C++11 did introduce new forms. See http://www.informit.com/articles/article.aspx?p=1852519

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class animal {
  // member declarations
  std::string message {"Hello from "};
  std::string name {"SOMEANIMAL. I don't have a name yet :("};
public:
  animal()
  {
      cout << this->message + this->name + '\n';
  }

  animal( const std::string & name )
  : name( name )
  {
      cout << this->message + this->name + '\n';
  }
};

Topic archived. No new replies allowed.
Pages: 12