Classes

I started learning and practicing classes.
Sorry for the long program. It works fine, it shows some instances of the class object and also let's you add a new one.

My questions is: Any ideas for further development of this program?

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

class Hero
{


public:
	Hero() 
	{ 
		
	}
	Hero(string, int, int ,int ,int);
	~Hero(){ cout<<"\nDestroying a hero!"; }

	void getData()
	{
		cout <<endl;
		cout <<"Enter new Hero name: "; cin>>name;
		cout <<"Enter new Hero life: "; cin>>life;
		cout <<"Enter new hero strength: "; cin>> strength;
		cout <<"Enter new hero agility:"; cin>>agility;
		cout <<"Enter new hero inteligence: "; cin>>inteligence;
	}
	void display_stats()
	{
		cout <<"\n\n\t--------------Hero Stats--------------";
		cout <<"\n\t Name: "<<name;
		cout <<"\n\t Life:  "<<life;
		cout <<"\n\t Strength:"<<strength;
		cout <<"\n\t Agility: "<<agility;
		cout <<"\n\t Inteligence: "<<inteligence;
		cout <<"\n\n\t--------------------------------------";
	}


	string getName()
	{
		return name;
	}
	void setName(string x)
	{
		name = x;
	}

	int getLife()
	{
		return life;
	}
	void setLife(int x)
	{
		life = x;
	}

	int getStrength()
	{
		return strength;
	}
	void setStrength(int x)
	{
		strength = x;
	}

	int getAgility()
	{
		return agility;
	}
	void setAgility(int x)
	{
		agility = x;
	}

	int getInteligence()
	{
		return inteligence;
	}
	void setInteligence(int x)
	{
		inteligence = x; 
	}
	

//private:
	string name;
	int life;
	int strength;
	int agility;
	int inteligence;

};

Hero :: Hero (string n, int l, int s, int a, int i)
{
	name = n;
	life = l;
	strength = s;
	agility = a;
	inteligence = i;
}



class Paladin : public Hero
{
public:
	Paladin(){cout <<"Builiding a Paladin!";}
	~Paladin(){cout <<"Destroying a Paladin!";}
};

class Demigod : public Hero
{
public:
	
	void display_stats()
	{
		Hero::display_stats();
	}
	Demigod()
	{
		cout << "\nBuilding a Demigod!";
	}
	~Demigod()
	{
		cout << "\nDestroying a Demigod!";
	}
};



int main()
{
	 Hero Hero  ;   
	 Hero.getLife(); 
	 Hero.getStrength();
	 Hero.getAgility();
	 Hero.getName();

	Paladin *Arthas = new Paladin();
	Arthas->setName("Arthas");
	Arthas->setLife(650);
	Arthas->setStrength(23);
	Arthas->setAgility(19);
	Arthas->setInteligence(21);
	Arthas->display_stats();

	Paladin *Uther = new Paladin();
	Uther->setName("Uther");
	Uther->setLife(1450);
	Uther->setStrength(43);
	Uther->setAgility(41);
	Uther->setInteligence(41);
	Uther->display_stats();
	
	Demigod * Hercules = new Demigod();
	Hercules->setName("Hercules");
	Hercules->setLife(2450);
	Hercules->setStrength(100);
	Hercules->setAgility(35);
	Hercules->setInteligence(35);
	Hercules->display_stats();


    for(int i=0; i<5; i++)
    
	cout<<endl;
	
	Demigod myHero;
	myHero.getData();

	cout <<"New Hero created: "; myHero.display_stats();

_getch();
}
1
2
3
4
5
6
7
8
9
	void getData()
	{
		cout <<endl;
		cout <<"Enter new Hero name: "; cin>>name;
		cout <<"Enter new Hero life: "; cin>>life;
		cout <<"Enter new hero strength: "; cin>> strength;
		cout <<"Enter new hero agility:"; cin>>agility;
		cout <<"Enter new hero inteligence: "; cin>>inteligence;
	}

You shouldn't put I/O in methods. You should really think about how your object is going to be used and write methods to support sensible use, rather than just thinking about what it has to do.

For example, can create files lists, vectors, strings, ... none of them start writing messages or expecting input when you use them, right?
Thank you for your answer.
I am still a beginner in C++, so I need something more specific on what to do next.
In C++, class is used to implement a number of different concepts.

In your example, you're doing object oriented programming, inheriting Paladin and Demigod from Hero.

Having established that, there are a few comments to make.
1. Object oriented programming only works thru indirection. In C++, this means holding pointers to objects. Forget about references to begin with.

2. The base class should establish a protocol; a set of things that all Hero can do. The derived classes implement these things and possibly specialise them, as well as adding things of their own. The protocol is implemented using virtual functions.

3. You then write code that uses these Heros generically. So code that uses Heros doens't know if it's a Paladin, Demigod or something else.

So I suggest you revise what you've done in this light rather than moving on to something else.
Topic archived. No new replies allowed.