classes subclasses

Here is a portion of my code. I have a class MyObjects and a subclass Human. Im not sure about the constructor for the subclass

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
class MyObjects
{
protected:
	int age; float dmt; string sub_name;			//delcare members private to this class
	class MyObjects* next;						//declare private pointer to the class
public:
	MyObjects(int _age, float _dmt, string _sub_name) : age(_age), dmt(_dmt), sub_name(_sub_name), next(0) {}	//constructor function and members
	//MyObjects(const MyObjects& obj) : age(obj.age), dmt(obj.dmt), name(obj.name), next (0) {}
	virtual ~MyObjects() { if (next != 0) delete next; }	//virtual destructor
	virtual float get_speed() const;						//
	virtual string get_name() const;
	string get_sub_name() const { return sub_name; }			//function returns name of object
	int get_age() const { return age; }						//function returns age of object	
	float get_diameter() const { return dmt; }				//function returns diameter of object
	int year_passed(int years) { return age += years; }		//function return age incremented by years
};

//Derived Human class of MyObjects
class Human : public MyObjects {
	class Human* next;
public:
	Human() : MyObjects(age,dmt,sub_name) {}
	virtual ~Human() { if (next != 0) delete next; }
	virtual float get_speed() const {
		int hAge = get_age();
		if (hAge == 0) return 0; 
		if (hAge > 1 && hAge <= 30) return hAge*0.5;		
		if (hAge > 30 && hAge <= 65) return 15;
		if (hAge > 65 && hAge <= 95) return hAge / 0.5;
		if (hAge > 95 && hAge <= 100) return 0;
		if (hAge < 0 || hAge > 100) throw invalid_age(hAge);
	}
	virtual string get_name() const { return "Human"; }
};

class Car : public MyObjects {
	class Car* next;
public:
	Car() : MyObjects(age,dmt,sub_name) {}
	virtual ~Car() { if (next != 0) delete next; }
	virtual float get_speed() const {return 50;}
	virtual  string get_name() const { return "Car"; }
};


In my main function I chose what class to construct depending on the string from input. I get a name, an age(int) and a diameter(float) from input. Then I calculate speed from the age.How should the constructor for the subclasses be ?
(there is probably some errors in my code, still needs cleaning)
Last edited on
Line 22: the variables you pass to the MyObjects constructor don't exist, maybe you meant them to be parameters of the Human constructor?
Last edited on
Hi,

I reckon you need to think about your design a bit more. Public inheritance means an "IS A" relationship, but you have a base class which tries to encompass humans and cars. It has a member dmt (diameter), do humans have a diameter ? Btw, don't over abbreviate you variable names.

There are other relationships: "HAS A" means a class has a member object which is of another class type - this is called aggregation or composition; "USES A" means a class makes use of another type (that isn't a member) as in a member function has a parameter that is of another class type.

Here are some ideas on how to do a basic analysis of what should be in which class.

If you start out by making a list of what attributes each Class (Human , Car) needs to record information about. Look at each Item in isolation.

See if there are any things in common. If so, think about creating a base class for the classes that have things in common - choose an appropriate name for it. Only things that are related to all the derived classes should go in this base class. So, the idea is too push common things as high up the inheritance tree as possible.

Be aware though you may not even need inheritance at all, you may be able to just do composition instead. One good reason to have inheritance is to achieve polymorphism, but there are other things to investigate such as design patterns.

So your 2 classes are not really related, expect that a Person could have a Car. When I say not really related, I don't mean the situation where one considers the attributes Mass (as in Weight) and Age - everything in the Universe could have those, so it's a bit dis-ingenious to say that everything is related by these two things.

Now you may want to do Polymorphism. For example, you might want to have a std::vector of "Things" which are either Person or Car. If that is what you want, then create a base class called Things from which Car and Person inherit from. Then in the derived classes provide function that take a pointer to Things. Then, somewhere there will be a container of pointers to Things Read up about that though, it's too big a topic to explain fully here, I am just putting the idea out there :+)

Some other things about classes:

Don't have public or protected data members, make every data member private, and provide an interface of public functions to deal with them. Don't have get / set functions for each member though, you already are using initialiser lists in your constructors which is great. I don't have problems with trivial get functions per say, but there are often ways one can have one function which deals with several member variables at once especially for mutator functions. One only needs a mutator function if the object needs to change after it has been created.

Good Luck !!
Topic archived. No new replies allowed.