简体   繁体   中英

How do I access the Private variables of parent class in child Class in C++

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;
}

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...

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM