CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Exclamation Accessing private members of a base class

    When creating a new derived class how do you access the private data members of the base class?
    Below is the base class. I am trying to create a new derived class called dateType. I've included the dateType class header and the entire base class together in one file. I need the operators to access the private data member, day, of dayType. Is any of the code correct, or is there any thing more that needs pointing out. I know this is long, but thank you in advance for your time.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class dayType
    {
    public:
    	void printDayString() const;
    		// precondition: A name must exist for a name to be printed.
    		// postcondition: The name of the week is printed.
    		// Function: To print the name of the day.
    
    	void printDayNum() const;
    		// precondition: An integer must exist for a number to be printed.
    		// postcondition: The number corresponding to the weekday is printed.
    		// Function: To print the number corresponding to the weekday.
    
    	dayType nextDay();
    		// precondition: Day must exist.
    		// postcondition: Day is incremented by one.
    		// Function: To add one day to the current day.
    
    	dayType previousDay();
    		// precondition: Day must exist.
    		// postcondition: Day is decremented by one.
    		// Function: To subtract one day from the current day.
    
    	void setDay(int i);
    		// precondition: Day must exist.
    		// postcondition: Day is set.
    		// Function: To set the current day of the classes object.
    
    	void add2Day(int d);
    		// precondition: Day must exist.
    		// postcondition: Corresponding days are either added to, or subtracted from day.
    		// Function: To add or subtract from day.
    
    	bool equalDay(const dayType & k) const;
    		// precondition: Day and newDay must exist.
    		// postcondition: True or False is returned.
    		// Function: To check if day and newDay are equal.
    
    	dayType(int d);
    		// preconditions d is an integer
    		// postconditions: d is set to proper day of week regardless of d negative or positive where
    		// -1 is equivalent to 6 = Saturday, and -7 is equivalent to 0 = Sunday etc.
    
    	dayType();
    		// default constructor
    
    private:
    	//string day2string(int, string) const;
    		// preconditions d is an integer is the range 0...6. (This is why the invariant must hold.
    		// postcondition value of d as a string "Sun" ... "Sat" is returned
    
    	int day;
    		// Stores day.
    
    	int day2;
    		// Stores day2.
    
    	string dayString;
    		// Stores the days of the week.
    };
    
    class dateType:public dayType
    {
    public:
        dateType operator++();
        //
        //
        //
    
        dateType operator--();
        //
        //
        //
    
        dateType operator++(int);
        //
        //
        //
    
        dateType operator--(int);
        //
        //
        //
    
        void addDay(int);
        //
        //
        //
    
        friend ostream & operator <<(ostream & os, const dateType & t);
        //
        //
        //
    
        friend istream & operator >>(istream & os, const dateType & t);
        //
        //
        //
    
        const dateType & operator = (const dateType &);
        //
        //
        //
    
        bool operator==(const dateType &)const;
        //
        //
        //
    
        dateType(const dateType &);
        //
        //
        //
    
        dateType(int dayofweek, int dayofyear, int year);
        //
        //
        //
    
        dateType();
        //
        //
        //
    
    private:
        int dayofyear;
        //
        //
        //
    
        int year;
        //
        //
        //
    };
    
    dateType dateType::operator++():private dayType(day)
    {
        return day;
    }
    
    dateType dateType::operator--() : dayType(day)
    {
        --day;
        return day;
    }
    
    dateType dateType::operator++(int) : dayType(day)
    {
        ++day;
        return day;
    }
    
    dateType dateType::operator--(int) : dayType(day)
    {
        --day;
        return day;
    }
    
    
    
    void dayType::printDayString() const
    {
    	if (day <= 6)
    
    	switch (day)
    		{
    		case 0:
    			  cout << "Sunday";
    			  break;
    		case 1:
    			  cout << "Monday";
    			  break;
    		case 2:
    			  cout << "Tuesday";
    			  break;
    		case 3:
    			  cout << "Wednesday";
    			  break;
    		case 4:
    			  cout << "Thursday";
    			  break;
    		case 5:
    			  cout << "Friday";
    			  break;
    		case 6:
    			  cout << "Saturday";
    			  break;
    		default:
    			  break;
    		}
    
    }
    
    void dayType::printDayNum() const
    {
    
    	cout << day;
    	return;
    }
    
     dayType dayType::nextDay()
     {
    	if ( day == 6)
    	{
    		 day = 0;
    		 return day;
    	}
    	else
    	return ++day;
    }
    
     dayType dayType::previousDay()
    {
    	if ( day == 0)
    	{
    		 day = 6;
    		 return day;
    	}
    	else
        return --day;
    }
    
     void dayType::setDay(int i)
    {
    	day = i;
    }
    
     void dayType::add2Day(int d) // Attention
    {
    
    	if (d > 0)
    	{
    		day = ((day + d) % 7);
    	}
    
    }
    
    bool dayType::equalDay(const dayType& newday) const  // Attention
    {
    	if (newday.day == day)
    	return 1;
    
    	else
    	return 0;
    }
    
    dayType::dayType(int i)
    {
    	day = i;
    }
    
    dayType::dayType()
    {
    	day = 0;
    }
    
    int main()
    {
    	dayType day;
    	dayType tempDay;
    
    	for (int i = 0; i < 20; i++)
    	{
    	day.printDayNum();
    	cout << " ";
    	day.printDayString();
    
    	cout << endl;
    	day = day.nextDay();
    	}
    
    	dayType newday(5);
    	for(int i = 0; i < 20; i++)
    	{
    	newday.printDayNum();
    	cout << " ";
    	newday.printDayString();
    	cout << endl;
    	newday = newday.previousDay();
    	}
    
    	day.setDay(6);
    	day.add2Day(23);
    	day.printDayString();
    
    	cout << endl;
    
    	newday.setDay(5);
    	newday.add2Day(24);
    	if (day.equalDay(newday))
    		cout << "equal days" << endl;
    	else
    		cout << "not equal days" << endl;
    
    	newday.setDay(5);
    	if (day.equalDay(newday))
    		cout << "equal days" << endl;
    	else
    		cout << "not equal days" << endl;
    
    	//system ("pause");
    	return 0;
    }
    This new class dateType will eventually, for example, end up outputting:
    "The date is Sun, Nov 21, 2012 the 332 day of the year."

  2. #2
    Join Date
    Dec 2009
    Posts
    145

    Re: Accessing private members of a base class

    When creating a new derived class how do you access the private data members of the base class?
    1. You can try to offer them a 'friend' keyword Or
    2. Provide a public get method for each of the private members in the base class.

  3. #3
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Re: Accessing private members of a base class

    Does this mean that this is wrong, I mean I do get errors. One in particular, "only constructors can take base initializes" can anyone explain this? I am not going to use "friends" or "get" is there any way to access them without having to use these? I thought that if you wanted to define a constructor of the derived class that needed parameters which were private in the base class it had to look something like this(boxType::boxType(double l, double w, double h) : rectangleType(l, w). Am I confused or am I not saying this correctly?
    If this code is excluded the program runs fine :
    Code:
    dateType dateType::operator++():private dayType(day)
    {
        return day;
    }
    
    dateType dateType::operator--() : dayType(day)
    {
        --day;
        return day;
    }
    
    dateType dateType::operator++(int) : dayType(day)
    {
        ++day;
        return day;
    }
    
    dateType dateType::operator--(int) : dayType(day)
    {
        --day;
        return day;
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    145

    Re: Accessing private members of a base class

    They are all functions'definitions not class constructors' definitions, initialization list is out of place
    Code:
    dateType dateType:perator--() : dayType(day)
    {
    --day;
    return day;
    }
    This is a good tutorial on operator overloading too.
    http://courses.cms.caltech.edu/cs11/...e/cpp-ops.html
    Last edited by Ledidas; February 11th, 2012 at 02:41 AM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing private members of a base class

    Quote Originally Posted by Wh1t3gh0st View Post
    When creating a new derived class how do you access the private data members of the base class?
    If those data members are private, then there must have been a reason for making them private. Otherwise your base class is not designed correctly and you're defeating the entire purpose of what private means.

    So let's say your base class decides to change the names of those variables -- then all of your derived classes, if they were allowed direct access to those variables, would be broken.

    Just provide public get() functions that get and set the values of those private variables. This protects any derived classes from having to change every time the base class changes.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Accessing private members of a base class

    Quote Originally Posted by Wh1t3gh0st View Post
    [...] how do you access the private data members of the base class?
    ... or simply make them protected rather than private. That way they are visible to derived classes but not to "the outside world", which may be exactly what you're looking for.

    However, similar to Paul, I tend to question some fundamental aspect of your design: IMO it doesn't make much sense to derive a date class from a day class. I'd rather use composition, i.e. make the day a member of the date class.

    Also I don't quite see a need for a day class in the first place. If I would write a date class, the day would probably either be an int member, an accessor method to such a member or a method that calculates the day based on some other (i.e. no integral year month and day components) internal representation of the date.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Re: Accessing private members of a base class

    Yeah well this is an assingment and the teacher wanted to do it this way. Here is the UML if you want to see what is actually going on. See attached file.
    Attached Images Attached Images

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing private members of a base class

    Quote Originally Posted by Wh1t3gh0st View Post
    Yeah well this is an assingment and the teacher wanted to do it this way. Here is the UML if you want to see what is actually going on. See attached file.
    You posted your question in a programming forum as to accessing private member variables, and we gave you answers as to why it is impossible, and why it's the totally wrong approach even if you could do it. A teacher's assignment does not change those facts.

    Secondly, where in that design does it state to make variables private and to access them directly? A UML design is just that -- a design. It doesn't say what C++ syntax to use to mimic the design. To access private member variables, you should create public or protected accessor methods (get, set), or make those variables protected instead of private.

    Also from that document you posted:
    Note that 1904 is a leap year, and any year up to 2100 (but not including) that is divisible by 4 is a leap year
    This is not the entire definition of a leap year. The year 2000 was not a leap year, even though it is evenly divisible by 4.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 11th, 2012 at 10:30 PM.

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Accessing private members of a base class

    Quote Originally Posted by Paul McKenzie View Post
    [...] The year 2000 was not a leap year, even though it is evenly divisible by 4.
    Actually, 2000 was a leap year because it not only was divisible by 100 but also by 400 (see http://en.wikipedia.org/wiki/Leap_ye...orian_calendar). And that's probably the reason why the assignment limits the date range to be covered to the years from 1904 to 2099 to simplify things: That way both the 100 year rule and the 400 year rule can be safely ignored.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing private members of a base class

    Quote Originally Posted by Eri523 View Post
    Actually, 2000 was a leap year because it not only was divisible by 100 but also by 400 (see http://en.wikipedia.org/wiki/Leap_ye...orian_calendar). And that's probably the reason why the assignment limits the date range to be covered to the years from 1904 to 2099 to simplify things: That way both the 100 year rule and the 400 year rule can be safely ignored.
    Yes, you're right. 1900 is not a leap year and 2100 isn't one.

    But at least the OP is not led into believing that any year that is evenly divisible by 4 is a leap year.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured