since Date is inherited from Pair everything in Pair will be available in Date as well. That is each Date object will have a private variable called aNum1 and aNum2, and each Date object will have public methods called setNum1() and setNum2(). Therefore, you can access aNum1 and aNum2 for a given Date object just like you would for a given Pair object.
However, each Date object will also get the public method output() available in Pair. Unfortunately, you have a public method called output() in Date as well. Both methods called output() have the return value and the same arguments, so the compiler can't tell whether to call the version inherited from Pair, or the version provided in Date. I believe that is the most likely cause for the crash. To fix this try placing the keyword virtual before the declaration output() in Pair, and it wouldn't hurt to use it in front of the declaration of output() in Date, either, although that is not absolutely necessary. Now, if output() is called by a Date object the output defined in the Date interface will be called, and if output() is called by a Pair object, then the version of output() defined in Pair will be called. If you want to call the version of output() from Pair by a Date object then you can try placing the class name Pair and the scope operator before the function call. You should probably look up the use of the keyword virtual in your favorite textbook to confirm my discussion.
Since aNum1 and aNum2 are actually part of Date, I don't believe you need to call the setNum1() and setNum2() methods in Date, although it isn't wrong to do so.
day is a variable passed to Date's constructor, but it is not a variable that can be used outside the constructor. The value of day is assigned to aNum1, so output that instead.
getNum1() and getNum2() are not declared, defined, or needed. output() acts as the public accessor function for the private member variables of Date and Pair.
Code:
#include <string.h>
#include <iostream.h>
#include "pair.h"
class Date : public Pair {
public:
int getYear () {
return aYear;
}
Date (int day, int month, int year) {
// set attributes of pair
aNum1 = day;
aNum2 = month;
// set new attribute
aYear = year;
}
virtual void output () {
cout << aNum1 << "." << aNum2 << "."
<< aYear << endl;
}
void output2 () {
cout << "the value of day is " << aNum1 << endl; }
private:
int aYear; // the year
};
main () {
Date HeidiKlum (4,8,1976); // the constructor
HeidiKlum.output2 ();
cout << "the contents of aYear is for Heidi is" << " "
<< HeidiKlum.getYear () << endl;
};
* Second file - "pair.h" *
Code:
// pair.h file
#ifndef _PAIR_H__
#define _PAIR_H__
class Pair {
public:
Pair (int num1, int num2);
Pair ();
virtual void output ();
void setNum1 (int num);
void setNum2 (int num);
private:
int aNum1;
int aNum2;
};
#endif
* Third file - "pair.cpp" *
Code:
// Pair.cpp file
#include <iostream.h>
#include "pair.h"
Pair::Pair (int num1, int num2) {
aNum1 = num1;
aNum2 = num2;
}
Pair :: Pair () {
aNum1 = 0;
aNum2 = 0;
}
void Pair::output () {
cout << aNum1 << " " << aNum2 << endl;
}
void Pair::setNum1 (int num) {
aNum1 = num;
}
void Pair::setNum2 (int num) {
aNum2 = num;
}