270 likes | 342 Views
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
E N D
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
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
Construction from an UML class notation • Malik’s example: Ch 12:personType.h & personTypeImp.cpp
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
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
Application: Grade • Programs under the folder “GradeReport” of Chapter 13
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
“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
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
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
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
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
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
An example: • Malik’s example: Ch 13:studentType.h & studentTypeImp.h
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
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”
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
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()?
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
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();
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();
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
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
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
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
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
Reference • Malik: 12.5, 13 • http://bdn.borland.com/article/0,1410,31863,00.html -- END --