Class Note for EECS 168 with Professor Haverkamp at KU
Class Note for EECS 168 with Professor Haverkamp at KU
Popular in Course
Popular in Department
This 13 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at Kansas taught by a professor in Fall. Since its upload, it has received 24 views.
Reviews for Class Note for EECS 168 with Professor Haverkamp at KU
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Inheritance Motivation De ning Person Class De ning Student Class Using Person and Student Classes Sample Program Output Implementing Person Methods Implementing Student Methods Constructor Functions Destuctor Functions Shape Inheritance Hierarchy University Inheritance Hierarchy Closing Comments Motivation 0 Code reuse is an important bene t to object oriented programming We can de ne classes that contain objects of other classes For example a polygon class that contains N point objects We can also classes by extending the de nition of other classes This process is called inheritance The base class contains general purpose datamethods The derived class contains more specialized datamethods Defining Person Class 0 We can de ne a Person with typical methodsdata 0 Nothing extra needs to be done to support inheritance Person class de nition class Person Methods public Person Person string GetName string GetAddress void SetNamestring name void SetAddressstring address void Print Data private string Name string Address int Age Defining Student Class 0 We can de ne a Student by extending the Person class 0 Only need to de ne the additional methodsdata 0 Student has access to all public Person methodsdata 0 Student does not have access to private methodsdata 0 We can also rede ne methods if desired eg Print 0 The program will automatically call correct methods Student class de nition class Student public Person Methods public Student Studentstring name string address oat gpa Student oat GetGPA void SetGPA oat gpa void Print Data private oat GPA Using Person and Student Classes Main program int main Declare and initialize Person object Person p p SetName Bob Brown p SetAddress 123 Somewhere Street pSetGPA20 error pPrint Declare and initialize Student object Student s s SetName Sam Smith s SetAddress 456 Nowhere Lane sSetGPA395 sPrint cout ltlt Donenn Destructors called automatically Sample Program Output Name Bob Brown Address 123 Somewhere Street Name Sam Smith Address 456 Nowhere Lane GPA 395 Done Destroying Student Name Sam Smith Address 456 Nowhere Lane GPA 395 Destroying Person Name Sam Smith Address 456 Nowhere Lane Destroying Person Name Bob Brown Address 123 Somewhere Street Implementing Person Methods 0 Methods have normal access to private Person data 0 Should provide get set methods for derived classes string Person GetNameO return Name string Person GetAddress return Address void Person SetNamestring name Name name void Person SetAddressstring address Address address void Person Print cout ltlt quotName quot ltlt Name ltlt quot Address quot ltlt Address ltlt endl Implementing Student Methods 0 Methods have normal access to private Student data 0 Must use getset methods to access private Person data oat Student GetGPA return GPA void Student SetGPA oat gpa GPA gpa void StudentPrint cout ltlt quotName quot ltlt GetName ltlt quot Address quot ltlt GetAddress ltlt quot GPA quot ltlt GPA ltlt endl Constructor Functions 0 The constructor function for a derived class will automatically call the constructor function for all base classes 0 This will let you initialize all private variables of an object 0 There are also more complex ways to specify default values see the text book for details Person Person Initialize Person data Name quotquot Address quotquot Student Student Person constructor called automatically Initialize Student data GPA 00 Student Studentstring name string address oat gpa Person constructor called automatically Initialize Person data via get set methods SetNamename SetAddressaddress Initialize Student data GPA gpa Destructor Functions 0 The destructor function for a derived class will automatically call the destructor function for all base classes 0 This will let you close les or delete memory as needed when you are nished using an object Person Person Debug message Cout ltlt Destroying Personn Print Clear data Name Address Student Student Debug message Cout ltlt Destroying Studentn Print Clear data GPA 00 Person destructor called automatically Shape Inheritance Hierarchy 0 Consider the following object hierarchy o What methodsdata belong with each object o How can inheritance be used to de ne these classes Shape Obj ect2D Circle Square Triangle Obj ect3D Cube Sphere Tetrahedron University Inheritance Hierarchy 0 Consider the following object hierarchy o What methodsdata belong with each object o How can inheritance be used to de ne these classes UniversityMember Employee Faculty Administrator Teacher Staff Student Graduate Undergraduate Alumnus Closing Comments 0 Inheritance can be a powerful tool for software design and code reuse 0 C provides several other object oriented programming features that are not discussed in this section 0 Protected datamethods 0 Multiple inheritance o Polymorphism 0 Virtual functions
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'