Dinosaur classes.

I am supposed to create a class program of dinosaurs. I have to create special attributes for each dinosaur how do i do that?

Dinosaur Class:
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
#include <iostream>
#include <string>

//Dinosaur class declaration
using namespace std;

class Dinosaur
{
private:
string name;
string diet;
string behavior;
string speed;
double height, weight;

public:
Dinosaur();//default constructor 
Dinosaur(string d, double h, double w, string s, string b) //constructor
{
diet=d;
behavior=b;
height=h;
weight=w;
speed=s;
}
void SetName(string n)
{name=n;}
void SetDiet(string n)
{diet=n;}
void SetBehav(string n)
{behavior=n;}
void SetHeight(double n)
{height=n;}
void SetWeight(double n) //COMMAND LINE?
{weight=n;}
void SetSpd(string n)
{speed=n;}
string Getname()
{return name;}
string GetDiet()
{return diet;}
string GetBehav()
{return behavior;}
double GetHeight()
{return height;}
double GetWeight()
{return weight;}
string GetSpd()
{return speed;}
};

#endif 


Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include "Apata.h"
#include "Trex.h"
#include "Hadro.h"

using namespace std;

int main()
{
string diet, beh, col, food;
int arml;
bool silly;
double h, w, spd, tail;
char gender;

Apata applejack("herbivore", 13.1, 41.6, "fast", "honest");
Trex trixie("carnivore", 1.2, 22.9, "fast", "great and powerful");
Hadro hoity("omnivore", 98.4, 3.2, "slow", "uptight");

getchar();
return 0;
}


And here is the part where I add attributes: Take the number of teeth as an example.

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
#ifndef APATA_H
#define APATA_H

#include "Dinosaur.h"

class Apata : public Dinosaur
{
private:

// add some stuff here to customize an Apata

public:
Apata() : Dinosaur()
{
SetName("Apatasaurus");
}
Apata(string d, double h, double w, string s, string b) : Dinosaur(d, h, w, s, b)
{
SetName("Apatasaurus");
cout<< "An Apata was created."<< endl;
// add stuff here to customize setting the information for the Apata
}
};

#endif 


Can someone help me enter code to customize the dinosaur?
Last edited on
You need help entering code? Is your keyboard not working correctly?
Yes. I keep getting error messages every time I try to customize the setting.
If you're able to write a class with a member, you shouldn't be having any problems with this. If you aren't, see http://cplusplus.com/doc/tutorial/classes/
Do post what you tried, so we can tell you what was wrong.
Also, spam less.
What am I doing wrong? My teacher gave me this code to add attributes. Whenever I compile it; it says that behavior is not declared? But it's declared in my dinosaur.h file right?

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
#ifndef APATA_H
#define APATA_H

#include "Dinosaur.h"

class Apata : public Dinosaur
{
private:
     string behavior;
	// add some stuff here to customize an Apata

public:
	Apata() : Dinosaur()
	{
		SetName("Apatasaurus");
		SetBehavior("Walks");
	}
	Apata(string d, double h, double w, string s, string b) : Dinosaur(d, h, w, s, b)
	{
		SetName("Apatasaurus");
		cout<< "An Apata was created."<< endl;
		SetBehavior(string b);
		cout<< "Apata likes long walks on the beach"<< endl;

    	// add stuff here to customize setting the information for the Apata
	}
};

#endif 
SetBehavior - you have called it SetBehav in the base class.

Using the initialiser Dinosaur(d,h,w,s,b) means you don't need to set these things (again) in the body of the constructor,
the initialisation list has done it for you.
Topic archived. No new replies allowed.