CS1C – Advanced Programming in C++Saddleback College Fall 2011 – J TateyamaInheritanceTopic 7 – Part 1(shrinkwrap Chapter 5)CS1C – Saddleback CollegeTonight’s TopicsInheritanceDirect member accessInheritance Basics ReviewDerived class access Inheritance WorksheetConstructor Reviewstatic Class Members ReviewCS1C – Saddleback CollegeInheritance HierarchyUndergrad Student Grad Student Extension Studentnameageidmajornameageidmajorgrant amtT.A.nameageidunitsWhat do each of these types of Students have in common?A basic class Student would be useful for these categoriesCS1C – Saddleback CollegeStudent Classclass Student{public:Student();Student(string nm, int ag, string id);void SetStudent(string nm, int ag, string id);string GetName() const;int GetAge() const;string GetId() const;private:string studentName;int studentAge;string studentId;};Class Student will hold the common attributes for all types of studentsWe will set up a relationship with the other classes that contain the unique information associated with the other types of studentsThis inheritance is a “is-a” relationship; where concept inherits the properties of the concept immediately above it in the hierarchyCS1C – Saddleback College“Is-a” relationshipVehicleWheeled Vehicle BoatCar Bicycle2-door 4-doorCS1C – Saddleback CollegeInheritance TerminologyInheritance – the mechanism by which one class acquires the properties of another class; it allows you to define new classes that are extensions of existing classesBase Class (superclass) – the existing, more general class; the class from which you create a new classDerived Class (subclass) – the new class created from the base class that inherits all the properties of the base class; more specialized classExample – When gradStudent is derived from student, we are adding major, TA, and grantamtCS1C – Saddleback CollegeCIN and Input-file stream exampleBoth can use the extraction operator–cin >> age;–inFile >> age;Consider the member function “open” –Open connects a input-file stream to a file; –But cin has no member function named “open”cin is of the base class “istream” An input-file stream is of type “ifstream”They are closely related but not the same – ifstream is a derived class of the class istreamCS1C – Saddleback CollegeGradStudent Classclass GradStudent : public Student{public:GradStudent( );GradStudent(string major, float grant, bool ta, string nm, int ag, string stuId);string GetMajor( ) const;float GetGrant( ) const;bool GetTA( ) const;private:string academicMajor;float grantAmt;bool teachAssist;};General syntax of a derived class is:Class className: memberAccessSpecifier baseClassName{member list };CS1C – Saddleback CollegeStudent and GradStudent NotesDerived class constructor (GradStudent) must include necessary information to initialize data members in base class (Student)Reserved word “public” after the colon –Says that Student is a public base class of GradStudent–All public members of Student are now public members of GradStudent excluding the constructor–If not specified the class will compile; however the base class member functions would be privateDerived class GradStudent has six private data members (3 from Student and 3 from GradStudent)Inheritance does not imply accessibility!! You can not access private members in class Student directly from GradStudent. All benefits of encapsulation and information hiding would be eliminated.CS1C – Saddleback CollegeDirect member accessGeneral syntax of a derived class is:class className: classAccessSpecifier baseClassName{member list };gradStudent in our example was required to use the public methods to access members of the base class student due to the member access specification of “private ” for the member variables To allow derived classes direct access to instance variables use the “protected” member access specifier in the base classOther users of the class will not have access to the protected members of the base class; only derived classesCS1C – Saddleback CollegeDirect Member Access Exampleclass Student{public:Student( );Student(string nm, int ag, string id);string GetName() const;int GetAge() const;string GetId() const;protected:string studentName;int studentAge;string studentId;};Protected member is the same as a private member to any other class except a class derived from the base classBase class protected members are inherited by the derived class as a protected member and my be accessed directly by derived class objects as though they were private membersNote: This is not the same as changing –class GradStudent : public StudentChanged from “private”CS1C – Saddleback Collegeprotected Class Member Access GradStudentvoid GradStudent::AccessTry(GradStudent g, Student s){cout << g.studentName << endl;cout << s.studentName << endl;}and the following client code:int main(){Student stu("Sam Smart", 21, "654321");GradStudent grad("Comp Sci",50000.00,true,"Sally Smart",25,"123456");grad.AccessTry(grad,stu);}What will happen? Illegal Access from ‘Student’ to protected/private memberCS1C – Saddleback Collegeprotect vs private Class MembersPrivate members should be used if it is necessary to prevent derived classes from having access to that member –One example is with helper methods in a class that any user has no need to know aboutProtected members should be used if the derived class needs to use in its implementationCS1C – Saddleback CollegeInheritance Basics ReviewInheritance makes it easy to extend a class by creating a new class that is based on itThe new class inherits all the member methods and data except the constructors and destructors of the class it is based onThe derived class is more specialized then base classSingle inheritance – the derived class is derived from a single base classMultiple inheritance – the derived class is derived from more than one base classCS1C – Saddleback CollegeDerived Class Access Specificationsclass BaseClass{public:BaseClass( );void SetSomething(int n);int GetSomething( ) const;private:int num;};class DerivedClass : public BaseClass{public:DerivedClass( );private:int num2, num3;};The class access specification determines how members of the base class are to be inherited by the derived classclass access specificationCS1C – Saddleback
View Full Document