JDoe JDoe - 8 days ago 5
C++ Question

Inheriting private members from base class to derived classes c++

I'm trying to inherit the name,skeletonType, and numLegs from my base class to my derived class.
I have a base class named Invertebrates and derived class named Spider. I want to be able to use the private variables with the derived class from my base class. I keep getting a compiling error on line 47 and it says expected primary-expression before ','.

I'm trying to get my output to look like this:

Spider:Brown Recluse, number of legs = 8, skeleton type = EXOSKELETON


Could someone help me out and point me in the right direction please. Here is what I put together so far.

// invertebrates.h
// invertebrate specifications

#ifndef _INVERTEBRATE_H_
#define _INVERTEBRATE_H_

#include <iostream>

using namespace std;

enum Skeleton_Type { NONE, HYDROSTATIC, EXOSKELETON };

class Invertebrate {
private:
string name;
Skeleton_Type skeletonType;
int numLegs;
protected:
void setSkeletonType(Skeleton_Type skeletonType);
void setNumLegs(int numLegs);
public:
Invertebrate();
Invertebrate(string name, Skeleton_Type skeletonType, int numLegs);
string getName();
Skeleton_Type getSkeletonType();
int getNumLegs();
virtual void print() = 0;
};

class Spider : public Invertebrate {
private:
const string NAME_PREFIX = "Spider: ";
public:
Spider();
Spider(string name);
virtual void print();
};
#endif // _INVERTEBRATE_H_
**********************************************************************

//invertebrates.cpp

#include "invertebrate.h"

void Invertebrate::setSkeletonType(Skeleton_Type skeletonType)
{
this->skeletonType = skeletonType;
}

void Invertebrate::setNumLegs(int numLegs)
{
this->numLegs = numLegs;
}
Invertebrate::Invertebrate()
{
name = "noName";
skeletonType = NONE;
numLegs = 0;
}

Invertebrate::Invertebrate(string name, Skeleton_Type skeletonType, int numLegs)
{
this->name = name;
this->skeletonType = skeletonType;
this->numLegs = numLegs;
}

string Invertebrate::getName()
{
return this->name;
}

Skeleton_Type Invertebrate::getSkeletonType()
{
return this->skeletonType;
}

int Invertebrate::getNumLegs()
{
return this->numLegs;
}


Spider::Spider(string name):Invertebrate(NAME_PREFIX + name,EXOSKELETON,8)
{
}

void Spider::print()
{
cout << this->getName();
cout << ", number of legs =" << this->getNumLegs() << ", skeleton type = " << this->getSkeletonType()<< endl;
}


int main()
{
Spider *sp = new Spider("Brown Recluse");
sp->print();

return 0;
}

Answer

Here are the issues in your code :-

  1. In .h file -

const string NAME_PREFIX = "Spider: ";

is not a valid way to initialize cont members - const should be initialized through constructor initializer list.

Correct way in .cpp

Spider::Spider(string name):NAME_PREFIX(name),Invertebrate(name,EXOSKELETON,8)
{
    name = NAME_PREFIX + Invertebrate::getName();
}
  1. In the same constructor you have written name&, which is wrong, just pass name.

Spider::Spider(string name):NAME_PREFIX(name),Invertebrate(name,EXOSKELETON,8)