lundi 5 novembre 2018

How to properly pass a string through multiple child constructors to parent. C++

I'm a college student in their first class of C++. I'm in need of some help in the current homework.

The goal I'm trying to accomplish is to create a new object(Rabbit) and pass the name pete from Rabbit>Mammal>Animal to be set there privately and referenced later. (The pointer is referenced later to print out the name)

string pete; Rabbit* RabbitP = new Rabbit(pete);

I have the header file for Rabbit as so:

#include <string>
class Rabbit :
    public Mammal
{
public:
    Rabbit(std::string tempname) : Mammal(tempname){ }
    Rabbit();
    ~Rabbit();

I'm attempting to use the initialize list to pass the name of the Rabbit to the Mammal then to Animal from there. In the Mammal and Animal headers I have:

#include <string>
class Mammal :
    public Animal
{
public:
    Mammal(std::string tempname) : Animal(tempname) {}
    Mammal();
    ~Mammal();
};


#include <string>
class Animal // BASE
{
private:
    std::string aName;
public:
    Animal(std::string tempname);
    Animal();
    ~Animal();

The Animal.cpp containing the function is:

#include <iostream>
#include <string>

using namespace std;
Animal::Animal()
{
}
Animal::~Animal()
{
}
Animal::Animal(string tempname)
{
    aName = tempname;
}
void Animal::Breathe()
{
    std::cout << "Takes a deep breath." << std::endl;
}
void Animal::Move()
{
    std::cout << "Jiggle your limbs around" << std::endl;
}
string Animal::GetName(Animal*)
{
    return aName;
}

Using the debugger in Visual Studio I can see that it is moving up the constructors properly but not passing anything. It feels like I'm somewhat on the right track, what am I missing?

Aucun commentaire:

Enregistrer un commentaire