#include <iostream>
#include <string>
usingnamespace std;
class Cat
{
protected:
string catName;
public:
//Constructors
Cat()
{ catName="Empty"; }
Cat(string cn)
{ catName=cn; }
//Set function
void setName(string cn)
{ catName=cn; }
//Get function
string getName()
{ return catName; }
};
class Feline: public Cat
{
protected:
Cat cat;
public:
//Constructors
Feline(){}
Feline(string cn)
{ cat.setName(cn); }
//The two following functions are what I feel I don't need to be writing
//Set function
void setCatName(string cn)
{ cat.setName(cn); }
//Get function
string getCatName()
{ return cat.getName(); }
};
int main()
{
Feline MasterCat;
MasterCat.setCatName("Master Cat");
//I want to be able to access the "Cat" functions here
cout<<MasterCat.getCatName()<<endl;
return 0;
}
The problem is that I feel I don't need to write those get/set functions on the second (Feline) class, because the first class (Cat) already has functions that do the exact same thing. Thing is, I can't write something like...
MasterCat.cat.getName();
...on the main file, because it gives me an obvious error. What can I do, if anything?
Edit: Oh, and without making the variables public.
#include <iostream>
#include <string>
usingnamespace std; // *** if you must
class Cat
{
protected:
string catName;
public:
//Constructors
Cat()
{ catName="Empty"; }
Cat(string cn)
{ catName=cn; }
//Set function
void setName(string cn)
{ catName=cn; }
//Get function
string getName() /* const */ // ideally add const
{ return catName; }
};
class Feline: public Cat
{
// protected:
// Cat cat; /// *** get rid of this; we are inheriting from Cat
public:
//Constructors
Feline(){}
Feline(string cn)
//{ cat.setName(cn); } // use the scope resolution operator
{ Cat::setName(cn) ; } // to acccess the base class object
/*
// *** setName() and getName() are inherited from Cat; we do not
// *** need to write equivalent functions again.
//The two following functions are what I feel I don't need to be writing
//Set function
void setCatName(string cn)
{ cat.setName(cn); }
//Get function
string getCatName()
{ return cat.getName(); }
*/
};
int main()
{
Feline MasterCat;
// MasterCat.setCatName("Master Cat");
MasterCat.setName("Master Cat"); // calls Cat::setName
//I want to be able to access the "Cat" functions here
// cout << MasterCat.getCatName() << '\n' ; // endl;
cout << MasterCat.getName() << '\n' ; // endl;
//return 0;
}