Question:
How do I access a protected member in C++?
2012-12-18 13:34:37 UTC
Please look at the constructors and classes below.

#include
#include
using namespace std;

class date;
class Time;

class date
{
friend ostream& operator <<(ostream&, date &);
//PreCondition: Function is passed the output stream and a DATE object
//Postcondition: Date is displayed in U.S. format (mm/dd/yyyy)
public:
date(); //default constructor, sets everything to 0
date(int, int, int); //constructor to initialize private member values
protected:
int month; //holds the month
int day; //holds the day
int year; //holds the year
};

class Time : public date
{
friend ostream& operator <<(ostream &, Time &);
//PreCondition: Fct is passed the output stream and an object of TIME
//PostCondition: Time is displayed in U.S. format with AM or PM (14:30:0 is displayed as 02:30:00 pm)
public:
Time(); //default constructor
Time(int, int, int, date &);
Time(int, int, int, int, int, int);

private:
int hour; //holds hours, 0-23
int minute; //holds minutes, 0-59
int second; //holds seconds 0-59
};

Time::Time()
{
hour = 0;
minute = 0;
second = 0;
}

Time::Time (int h, int m, int s, date &dobj)
{
hour = h;
minute = m;
second = s;
Time.month = dobj.month;
Time.day = dobj.day;
Time.year = dobj.year;
}

Time::Time (int h, int m, int s, int mm, int dd, int yyyy)
{
hour = h;
minute = m;
second = s;
Time.month = mm;
Time.day = dd;
Time.year = yyyy;
}

date::date()
{}

date::date (int mm , int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}

FOR MY TIME CONSTRUCTORS, it says cannot access dobj.month, dobj.day , dobj.year even though it is passed a data object..

Also, for my other time constructor with all INTs, it says that I am "missing a ; before the ." HELP!
Four answers:
peteams
2012-12-18 16:38:45 UTC
The compiler is right!



You cannot look at protected members of objects other than "this". What you should do instead is add a constructor to date that takes a date and pass it down to the base constructor.



The errors about semicolons are because you've put Time. in front of your variables; you don't want to do that.



So I believe you should change



Time::Time (int h, int m, int s, int mm, int dd, int yyyy)

{

hour = h;

minute = m;

second = s;

Time.month = mm;

Time.day = dd;

Time.year = yyyy;

}



to



Time::Time (int h, int m, int s, int mm, int dd, int yyyy)

: date(mm, dd, yyyy)

{

hour = h;

minute = m;

second = s;

}



And similarly with your other constructors. You'll need to modify the class definitions to match as well.
2012-12-18 15:25:22 UTC
Protected members are "basically" the same as private members, except that they get inherited to subclasses.



To access one, you need to actually make a (public) method to access it.
Timothy
2012-12-18 14:00:06 UTC
Apparently, in C++, you cannot access protected members of a base class except for the current instance.



http://stackoverflow.com/questions/7476117/cannot-access-protected-member
2012-12-18 14:47:15 UTC
So, wtf is Time.month? etc.

some reason month = won't work?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...