0

I am trying to do an inheritance problem. I have a base class Animal with parameters int height, age, and weight. I also have a default constructor and a constructor with parameters. I have a derived class Dog with the parameter dogType and a constructor with the parameters from Animal and the new parameter, dogType. My question is, how do I use the constructor w/ parameters from Animal for the Dog class? This is how I tried to do it, however, it is not working. I will include all the header and implementation files. Any help is greatly appreciated!

#pragma once
#include <iostream>
#include <string>

class Animal
{
private:
    int height;
    int age;
    int weight;
public:
    Animal();
    Animal(int h, int a, int w);
    void print()const;
};
#include "Animal.h"
#include <iostream>

Animal::Animal()
{
    height = 0;
    age = 0; 
    weight = 0;
}
Animal::Animal(int h, int a, int w)
{
    height = h;
    age = a;
    weight = w;
}
void Animal::print()const
{
    std::cout << "Height:" << height << std::endl;
    std::cout << "Age:" << age << std::endl;
    std::cout << "Weight:" << weight << std::endl;
}
#pragma once
#include "Animal.h"
#include <iostream>

class Dog :
    public Animal
{
private:
    std::string dogType;
public:
    Dog();
    Dog(int h, int a, int w, std::string dt);
    void print()const;
};
#include "Dog.h"

Dog::Dog()
{

}
Dog::Dog(int h, int a, int w, std::string dt) 
{
    Animal::Animal(h, a, w);
    dogType = dt;

}
void Dog::print()const
{
    Animal::print();
    std::cout << "Dog Type:" << dogType << std::endl;
}
1

2 Answers 2

4

The correct syntax for inheriting is

Dog::Dog() : Animal()
{
}

Dog::Dog(int h, int a, int w, const std::string& dt) 
   : Animal(h, a, w), dogtype{dt}
{
}

Since Dog() is an empty constructor you could define with

Dog::Dog() = default

The proper way to define the Animal constructor instead is:

Animal::Animal()
   : Animal(0,0,0)
{
}

Animal::Animal(int h, int a, int w)
  : height{h}, age{a}, weight{w}
{
}

First one is using delegate constructor and second one is using constructor initializer list

Sign up to request clarification or add additional context in comments.

4 Comments

You might want to mention Initializer list
@ZachSal please mark it as accepted is the answer is fine for you
@Moia Thank you so much for the help. I plan on getting a cpp book to help me understand the syntax better. If you don't mind me asking, what exactly does the colon do? Also, why is it better to use : Animal(0,0,0) instead of how I had it? Again, thank you so much for the help. I think I marked your response as the answer.
@ZachSal read the link about the initializer list. Using the delegate constructor is to be less bug-prone. In this way only one constructor is the delegate of the initialization and the others refer to that. Multiple constructor could lead to unintended different behaviour if you implement the construction in any of those
1

@Moia already had the correct syntax you should use for initializing your Dog object.

The reason your code didn't work is because when the code goes to Animal::Animal(h, a, w), the underlying Animal of your Dog is already initialized.

In fact, all members in your class, include all member from your parent class have already been initialized when you have entered the first line of your constructor. Unless you have done a initializer list or delegate constructor like @Moia has mentioned, they will be default initialized.

By writing Animal::Animal(h, a, w) here, you are just creating a temporary Animal object, that will never be used.

While you could still changed the height, age, and other things if you have provided setter functions for them, it is better to use initializer list because you will not be initializing them first, then changing the value of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.