1 / 27

DCO10105 Object-Oriented Programming and Design

DCO10105 Object-Oriented Programming and Design. Lecture 7: OOP: Inheritance Inheritance and composition Basic OOD concept “is-a” relationship & “has-a” relationship Implementation of Inheritance Syntax in C++ Construction from the UML Scope referencing The sample application: Grade

tuyet
Download Presentation

DCO10105 Object-Oriented Programming and Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DCO10105 Object-Oriented Programming and Design • Lecture 7: OOP: Inheritance • Inheritance and composition • Basic OOD concept • “is-a” relationship & “has-a” relationship • Implementation of Inheritance • Syntax in C++ • Construction from the UML • Scope referencing • The sample application: Grade -- By Rossella Lau

  2. Basic OOD concept • Three basic principles • Encapsulation • Pack data and operations into a class to hide its details from the outside world (class construction) • Inheritance • Classes can be derived from existing classes to save programmers’ effort – code reuse • Polymorphism • Same class can play as different types (of ancestors) • Same expression can perform different operations • OOP implements OOD

  3. Construction from an UML class notation • Malik’s example: Ch 12:personType.h & personTypeImp.cpp

  4. Chan Terence Tse Cyril lastName firstName person2 person1 personType Wong Patrick Lau Rossella person3 person4 Instantiation of objects • Each object instantiated from a class will have its own data members with some values

  5. Mon Tue Wed day1 day2 day3 weekDay weekDays DayType Instantiation but not static member • E.g., DayType includes a static member weekDays • Instead of a weekDays for each instance, there is only one single value residence somewhere of the program weekDays

  6. Application: Grade • Programs under the folder “GradeReport” of Chapter 13

  7. Composition • Inside studentType, courseEnrolled is in type of courseType[] • I.e., studentType is constructed with data members of the type of another class courseType • This is called composition

  8. “has-a” relationship • Composition can be interpreted as a “has-a” relationship • A student has a number of courses enrolled; an orderItem has a “Product”; a retail system has a “Catalog” and some “Cashiers” • Composition allows a class to be constructed with data members of the type of another class

  9. Construction with aggregation • When there is an aggregation in an application, the instance of the class ended at the diamond should be declared as a container (an array or a vector) • courseEnrolled in studentType • studentList in the main program

  10. Construction with generalization • When there is a specialization (vs generalization), the class definitions should be written as a sub class of the generalization class – the concept of inheritance

  11. Inheritance • Inheritance allows new classes to be created, derived from existing classes • The existing class is called “base class” or “parent class” • The new derived class is called “sub class” or “child class” • Derived classes inherit, carry or own, all the properties of the base class

  12. Syntax of Inheritance class SubClass : [AccessSpecifier] BaseClass {......}; • : (colon) indicates the relationship of inheritance • The left hand side is the new class, the sub class, and is going to be derived, i.e., to own all the properties (data members, function members, and the type), from the base class

  13. Access Specifier AccessSpecifier can be one of these three: public, private, and protected • It specifies the ability of an outsider (things other than the members of SubClass) on how to access the members in BaseClass • public allows an outsider to access every public member in BaseClassthrough an instance of SubClass • private does not allow an outsider to access any members, even if it is a public member, in BaseClass • protected allows a child class of SubClass to access public members in BaseClass but does not allow other outsiders to access any members, even if it is a public member, in BaseClass • In our course, we will focus on public inheritance

  14. An example: • Malik’s example: Ch 13:studentType.h & studentTypeImp.h

  15. Chan Judy 50123456 6 True {(OOP,DCO10105,3),…} {A,…} sID numberOfCourses isTuitionPaid courseEnrolled[] courseGrades studentType student1 Instantiation of a student • Each object instantiated from studentType will include not only the values of studentType’s data but also values of personType’s data

  16. Inheritance is “is-a” relationship • Inheritance can be interpreted as an “is-a” relationship • “Coffee” is a “Product”; “Coffee Brewer” is a “Product” • “Cash payment” is a “Payment”, “Octopus payment” is a “Payment”

  17. Protected members of a class • With inheritance, members of a class can also be protected in areas other than public and private • Protected members are public to all sub classes of the class the members belong to but private to classes not belonging to the family of the class including the protected members • In this course, we will focus on private or public members

  18. Scope referencing • For a user program which has:stuendType student; • student.getFirstName() uses the member defined in personType • student.setInfo() uses the member defined in studentType • How about student.print()?

  19. When two functions are identical …… • For student.print() , it uses the print() in studentType since the compiler finds studentType first then its base class(es) personType • Indeed, both print() are identical, i.e., they have the same signatures • print()of studentType redefines, or overrides, the print() in personType

  20. More about scope resolution operation :: • When a sub class uses an identical function (with same signature) in the base class, :: should be used • In personType, void print(){ outF << getFirstName() << " " << getLastName() << endl; } • In studentType, output of a student’s name can become: outF << "Student Name: " << personType::print();

  21. Avoid identical codes – code reuse • Therefore, identical codes in sub-classes should be avoided: • E.g., on line 48-49 in studentTypeImp.cpp: outF << "Student Name: " << getFirstName() << " " << getLastName() << endl; • part of the statement is the same as in personType::print() • It can be changed, for the sake of code reuse or redundant code elimination, as follows:outF << "Student Name: " << personType::print();

  22. A sub class can play as its base class • As a sub class would have all the properties of its base class – it can act as its base class  It can be in the type of its base class • One more example on scope referencing class: DonaldFamily.h and scopePlayer.cpp • Donald boxD(dewey) : dewey plays as a Donald object • deduct7(huey) : huey plays as a Donald object for calling

  23. static_cast<ClassName> (variable) • Temporary change a variable’s type to ClassNamee.g., line 33, 48, 50 in DonaldFamily.h • Formal C uses (ClassName) variable

  24. Function overload and override • In DonaldFamily.h: • In class Dewey and Huey, withdraw() is overloaded • Indeed, withdraw(int) in Dewey seemed redundant because usually it can be referenced automatically by type matching; however, double is a type compatible to int and cause calling to withdraw(double) even if the actual parameter is an integer from dewey.withdraw(static_cast<int> (5)); • In class Donald, getMoney(), withdraw(int), and print() are overridden by both Dewey and Huey

  25. Initialization of data members in base class • Since data members in the base class are also data members of the sub class, initialization of those members should use a constructor of the base class • Dewey(double) uses Donald(int) • Huey(double) uses Donald(), the default constructor, even if it is not explicitly written there; i.e., the compiler will automatically generate a call to the default constructor to initialize data members in the base class if no such initialization in the constructor of the sub class

  26. Summary • Members of a class can be public, private, and protected • Inheritance is an “is-a” relationship while composition is a “has-a” relationship • A derived class inherits all its base class’ members + type • Inheritance increase code reuse • C++ allows for three access types of inheritance, we have examples for public inheritance • When identical functions, with same id and signatures, in sub class and base class, instance of sub class access the function in sub class and this is called function override

  27. Reference • Malik: 12.5, 13 • http://bdn.borland.com/article/0,1410,31863,00.html -- END --

More Related