|
-
February 11th, 2012, 01:28 AM
#1
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."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|