DOC PREVIEW
Saddleback CS 1C - Topic 7 – Part 1

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1C – Advanced Programming in C++Saddleback College Fall 2011 – J TateyamaInheritanceTopic 7 – Part 1(shrinkwrap Chapter 5)CS1C – Saddleback CollegeTonight’s TopicsInheritanceDirect member accessInheritance Basics ReviewDerived class access Inheritance WorksheetConstructor Reviewstatic 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 studentsWe will set up a relationship with the other classes that contain the unique information associated with the other types of studentsThis 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 TerminologyInheritance – the mechanism by which one class acquires the properties of another class; it allows you to define new classes that are extensions of existing classesBase Class (superclass) – the existing, more general class; the class from which you create a new classDerived Class (subclass) – the new class created from the base class that inherits all the properties of the base class; more specialized classExample – When gradStudent is derived from student, we are adding major, TA, and grantamtCS1C – Saddleback CollegeCIN and Input-file stream exampleBoth 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 NotesDerived 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 privateDerived 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 classOther 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 classBase 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 membersNote: 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 MembersPrivate 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 aboutProtected members should be used if the derived class needs to use in its implementationCS1C – Saddleback CollegeInheritance Basics ReviewInheritance makes it easy to extend a class by creating a new class that is based on itThe new class inherits all the member methods and data except the constructors and destructors of the class it is based onThe derived class is more specialized then base classSingle inheritance – the derived class is derived from a single base classMultiple 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
Download Topic 7 – Part 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Topic 7 – Part 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Topic 7 – Part 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?