1

Can you take a look at my overladed operator for the package class in my code below. Is it possible to access private variables of the parent class in child class? What I did is I used member functions to write a overloaded operator for the package class but it says "Variable 't' is uninitialized when used here". Go to the end and you will see overladed operator.

#include <iostream>
using namespace std;

// Ticket Class
class Ticket
{
    private:
        int    runTime;
        string movieName;

    public:
        Ticket(int, string);
        void   setMovieName(string);
        void   setRunTime(int);
        string getMovieName();
        int    getRunTime();
        friend ostream &operator <<(ostream &stream, const Ticket&);
};


// Package Class
class Package : public Ticket
{
    private:
        string snackName;

    public:
        Package(int, string, string);
        void   setSnackName(string);
        string getSnackName();
        friend ostream &operator <<(ostream &stream, const Package&);
};




int main()
{



}




// Ticket Class empty constructor
Ticket::Ticket(int rnTime, string mvTime) : runTime(rnTime), movieName(mvTime){}


// Ticket Class setter
void Ticket::setMovieName(string mv)
{
    movieName = mv;
}

// Ticket class setter
void Ticket::setRunTime(int rn)
{
    runTime = rn;
}

// Ticket class getter
string Ticket::getMovieName()
{
    return movieName;
}

// Ticket Class getter
int Ticket::getRunTime()
{
    return runTime;
}

// Ticket class friend overloaded function
ostream &operator <<(ostream &stream, const Ticket &t)
{
    stream << "Movie name: " << t.movieName << endl;
    stream << "Run time: "   << t.runTime   << endl;
    return stream;
}

// Child Class empty Constructor
Package::Package(int rnTime, string mvTime, string snTime) : Ticket(rnTime,mvTime), snackName(snTime){}


// Package Class setter
void Package::setSnackName(string sn)
{
    snackName = sn;
}

// Package Class Getter
string Package::getSnackName()
{
    return snackName;
}

// Package class friend overloaded function
ostream &operator <<(ostream &stream, const Package &p)
{
    Ticket *t; // compilers says "Variable 't' is uninitialized when used here"

    stream << "Movie: "      << t->getMovieName() << endl;
    stream << "Run time: "   << t->getRunTime()   << endl;
    stream << "Snack name: " << p.snackName       << endl;
    return stream;
}
Saad Arshad
  • 31
  • 1
  • 1
  • 5
  • 5
    You don't. I mean, you don't access private variables of a base class, you can't since they're *private*. If you want to access parent member variables, either create accessor function ("getters") or make the members `protected`. Though think a little bit about your design first. Attempting to access parent members (through getters or directly) is often a sign of a bad design. – Some programmer dude Oct 17 '18 at 06:10
  • Make the variable **protected** in the parent object. – TheArchitect Oct 17 '18 at 06:11
  • I can't make variables protected or anything. My professor said used member function but when I use them compile says "Variable 't' is uninitialized when used here". – Saad Arshad Oct 17 '18 at 06:14
  • @GaneshChowdharySadanala what you doin here. lol – Saad Arshad Oct 17 '18 at 06:15
  • @SaadArshad `Ticket t = Ticket(movie_nid, "movie_name")`... *Uninitialized* means you haven't assigned the values of the *private* section anything and then you try to receive them using member functions... – Ruks Oct 17 '18 at 06:16
  • use Ticket *t=new Ticket; //and call the functions – Ganesh Chowdhary Sadanala Oct 17 '18 at 06:16
  • 1
    @GaneshChowdharySadanala No no no, that's wrong. The OP should use e.g. `p.getMovieName()`. – Some programmer dude Oct 17 '18 at 06:17
  • @Ruks No, see my comment above. – Some programmer dude Oct 17 '18 at 06:18
  • @SaadArshad What is the use of the ***uninitialized*** variable `t`? You already have `p` which due to the inheritance *is a* `Ticket` object. It seems you missed something in you class, your tutorial or your books. And if you don't have any books, [then get some good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Oct 17 '18 at 06:18
  • Are you allowed to make any changes to the base class? – R Sahu Oct 17 '18 at 06:18
  • 2
    @Someprogrammerdude `p.getMoveName()` will not work until the OP makes the *get* functions constant... – Ruks Oct 17 '18 at 06:30

3 Answers3

4

Make your get functions constant, like so:

string getMovieName() const; // <- Note the const specifier
int    getRunTime() const;   // <- Note it here as well...

Since these functions do nothing more than just return the values... So it is always advisable to do this with all get functions that do not modify the private variables...

And do the same in the definitions as well...

The binary operator<< can simply be defined as:

ostream &operator <<(ostream &stream, const Package &p)
{
    stream << "Movie: " << p.getMovieName() << endl;
    stream << "Run time: " << p.getRunTime() << endl;
    stream << "Snack name: " << p.snackName << endl;
    return stream;
}

Note: The reason this works is that the constant const Package &p is a const-qualified parameter... (One that does not allow any modifications inside itself but can be directly assigned from outside, hence the usage of reference operator), and only allows constant member functions (Functions that don't take part in modifying the class members...) to be called from itself... And since we have declared getMovieName() and getRunTime() to be constant functions... They are run correctly...


Note: You should consider visiting here to know more about the different ways how a value can be passed to a parameter of specific type...

Ruks
  • 3,886
  • 1
  • 10
  • 22
2

You can use the operator<< function defined for the base class in the implementation of the derived class.

ostream &operator <<(ostream &stream, const Package &p)
{
    Ticket const& t = p;
    stream << t;
    stream << "Snack name: " << p.snackName << endl;
    return stream;
}

This reduces code duplication and avoids the private member access problem.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

When you use public inheritance, all public and protected members of the base class become public and protected in the derived class. In your example, runTime and movieName is private so you can't access it from outside class.

Also, runTime and movieName is already private in Parent. Once declared private, a member always remains private to the base class regardless of the type of inheritance. To access runTime and movieName change Parent to:

protected:
    int    runTime;
    string movieName;

If you want to access the size member from a derived class, but not from outside the class, then you want protected.

And you should inherit Child by:

class Package : public Ticket
sahushivam
  • 109
  • 1
  • 6