Need help with my constructor (inheritence hierarchy)

I am not sure what the copy constructor in Humanoid should look like since it is inheriting Creature that has dynamic data.

This is what Creature Class looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
        class Creature {
	  public:
                Methods......
          private:
                char* name;
		int hp;
		int abilities[ABILITY_MAX];
		WpnClass* currWpn;
		ArmorClass* currArmor;
		vector<WpnClass> wpnList;
		vector<ArmorClass> armorList;
		vector<string> skillList;
         };


And this is what my copy constructor looks like, (it works)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

	DeleteDD();
	wpnList.clear();                       // clear vectors and delete
	armorList.clear();                     // dynamic data
	skillList.clear();

	if (c.name) {		
             name = new char[strlen(c.name) + 1];   // if name exists copy
	     strcpy(name, c.name);
	}
	
	for (int i = 0; i < ABILITY_MAX; i++)	// copy new abilities 
		abilities[i] = c.abilities[i];		
		
	if (c.currWpn) 	
		currWpn = new WpnClass(c.ReturnCurrWpn());
        		
	if (c.currArmor)	
		currArmor = new ArmorClass(c.ReturnCurrArmor());

	hp = c.hp;
	wpnList = c.wpnList;
	armorList = c.armorList;
	skillList = c.skillList;



Now Humanoid inherits Creature and contains no dynamic data but I am required to provide a copy constructor and assignment operator so my question is what should it contain?

Do I have to call Creature's copy constructor and then copy every encapsulated data member manually?

This is how I am guessing it would like:

1
2
3
4
5
6
7
         Humanoid::Humanoid(const Humanoid& h) : Creature(const Creature& c) {

         copy all Humanoid data manually even though it's non-pointer data

         }

 



Is this even close to correct?
The compiler provided copy-ctor/operator = will automatically call the base class's copy-ctor/operator =, so you don't have to worry about it.
So the compiler will know to call the copy constructor I provided in the base class Creature and not the shallow one that is by default?
Last edited on
new WpnClass(c.ReturnCurrWpn()); vector<WpnClass> wpnList;
I'm confused. ¿What is the purpose of wpnList? I thought it will hold the weapons in your inventary, put in that case ¿why are you creating a new one?

If you are using polymorphism you are losing it in the copy (and the container makes no sense).
If you aren't using it, then ¿why dynamic allocation?
Last edited on
Oh yea, I know it looks weird.

We do not think an empty weapon should be created, so we made the default constructor protected so that we will always create a weapon or armor by passing it a name as a string to create it.

ReturnCurrArmor( ) and ReturnCurrWpn( ) return the name of them as a string so you can use it to create a new instance.

Since you can't call the default constructor we made CurrWpn and CurrArmor pointers so that we can allocate them dynamically when we get an actual string to construct from. So a creature will have a current weapon and armor as well as a wpn and armor list.


And we won't use polymorphism until we add on to this program. It is sorely needed.
Last edited on
so we made the default constructor protected
If you don't want a default constructor just don't code it. There is no need to make it protected.

So a creature will have a current weapon and armor as well as a wpn and armor list.
¿Why don't just mantain an index? If you have nothing equiped, the index could be negative or something like that.

By the way you should use std::string instead of char*
This is all for the OOP class for my CS degree so we are doing most things just to learn them. We learned that by making default constructor protected or private the client is forced to use a parameterized constructor and that by doing that we as the programmers must also rely on using dynamic data.

And yes, I most definitely prefer C++ strings but my Prof is old school and wants us to be familiar with C-style strings. If I had my way I would be doing all of this in C# using visual studio. lol

I do not plan on coding in C++ for a living. I have much more fun doing asp.net stuff in C# or VB and whatever will take its place.

But I can definitely understand why they teach us C++. If you can code in C++, you can code in any other high level language.
Last edited on
1
2
3
4
5
6
7
8
9
struct foo{
  foo(int dummy){}
};

int main(){
  foo bar; //no matching function for call to foo::foo()
  foo baz(42); //no problemo
  return 0;
}
Topic archived. No new replies allowed.