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 265constructing student Cy N Senseconstructing student Matt Madoxconstructing graduate student Matt Madoxadding grade to Cy N Senseadding grade to Matt MadoxMatt’s qualifier grade = 1.5Press any key to continue . . .Using a subclassThe class Student has been defined in the conventional fashion. The classGraduateStudent is a bit different, however; the colon followed by thephrase public Student at the beginning of the class definition declaresGraduateStudent to be a subclass of Student.The appearance of the keyword public implies that there is probably protectedinheritance as well. All right, it’s true, but protected inheritance isbeyond the scope of this book.Programmers love inventing new terms or giving new meaning to existingterms. Heck, programmers even invent new terms and then give them asecond meaning. Here is a set of equivalent expressions that describes thesame relationship: GraduateStudent is a subclass of Student. Student is the base class or is the parent class of GraduateStudent. GraduateStudent inherits from Student. GraduateStudent extends Student.As a subclass of Student, GraduateStudent inherits all of its members. Forexample, a GraduateStudent has a name even though that member isdeclared up in the base class. However, a subclass can add its own members,for example qualifierGrade. After all, gs quite literally IS_A Student plus alittle bit more than a Student.The main() function declares two objects, llu of type Student and gs oftype GraduateStudent. It then proceeds to access the addCourse() memberfunction for both types of students. main() then accesses the qualifier()function that is only a member of the subclass.Constructing a subclassEven though a subclass has access to the protected members of the base classand could initialize them, each subclass is responsible for initializing itself.

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

Saved successfully!

Ooh no, something went wrong!