How do I access a function of a class from another class, from the main file?

I hope it doesn't sound confusing, I will try to give an example of what I am trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
using namespace 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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
using namespace 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;
}
I see, I had the wrong idea of inheritance. Appreciate it.
Topic archived. No new replies allowed.