11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20: Inheriting a Class 263Another minor side effect has to do with software modification. Suppose youinherit from some existing class. Later, you find that the base class doesn’t doexactly what the subclass needs. Or, perhaps, the class has a bug. Modifyingthe base class might break any code that uses that base class. Creating andusing a new subclass that overloads the incorrect feature solves your problemwithout causing someone else further problems.How Does a Class Inherit?Here’s the GraduateStudent example filled out into a programInheritanceExample://// InheritanceExample - demonstrate an inheritance// relationship in which the subclass// constructor passes argument information// to the constructor in the base class//#include #include #include #include using namespace std;// Advisor - empty classclass Advisor {};const int MAXNAMESIZE = 40;class Student{public:Student(char *pName = “no name”): average(0.0), semesterHours(0){strncpy(name, pName, MAXNAMESIZE);name[MAXNAMESIZE - 1] = ‘\0’;cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!