370 likes | 460 Views
Chapter 13. INHERITANCE AND COMPOSITION. Inheritance and Composition. The two common ways to relate two classes in a meaningful way are: 1. Inheritance (“is-a” relationship) (Every employee is a person) 2. Composition (“has-a” relationship) (Every person has date of birth). Inheritance.
E N D
Chapter 13 INHERITANCE AND COMPOSITION
Inheritance and Composition • The two common ways to relate two classes in a meaningful way are: • 1. Inheritance (“is-a” relationship) (Every employee is a person) • 2. Composition (“has-a” relationship) (Every person has date of birth)
Inheritance • Design a class, partTimeEmployee, to implement and process the characteristics of a part-time employee • Every part-time employee has a name, pay rate, and number of hours worked • Every part-time employee is a person • Rather than design the class partTimeEmployee from scratch, we want to be able to extend the definition of the class personType by adding additional members (data and/or function)
Inheritance • We do not want to make the necessary changes directly to the class personType—that is, edit the class personType, and add and/or delete members • We want to create the class partTimeEmployee without making any physical changes to the class personType, by adding only the members that are necessary • The new class that we create from existing classes is called the derived class and the existing classes are called the base classes
Inheritance • Single inheritance- the derived class is derived from a single base class • Multiple inheritance- the derived class is derived from more than one base class • Inheritance can be viewed as a tree-like, or hierarchical, structure wherein a base class is shown with its derived classes
Inheritance class circle: public shape { . . }; • The word public in a heading, called the member access specifier, specifies that all public members of the class shape are inherited as public members by the class circle
Inheritance class circle: private shape { . . }; • In this definition the public members of shape become private members of the class circle. So any object of type circle cannot directly access these members.
Inheritance class circle: shape { . . }; • If we do not use the memberAccessSpecifier public or private, the public members of a base class are inherited as private members.
Inheritance class className: memberAccesSpecifier baseClassName { member list };
Facts • The following facts about the base and the derived classes should be kept in mind: • 1. The private members of the base class are private to the base class; hence, the members of the derived class cannot directly access them • 2. The public members of a base class can be inherited either as public members or as private members by the derived class • 3. The derived class can include additional data and/or function members
Facts • 4. The derived class can redefine the public member functions of the base class. However, this redefinition applies only to the objects of the derived class, not to the objects of the base class • 5. All data members of the base class are also data members of the derived class. Similarly, the member functions of the base class (unless redefined) are also the member functions of the derived class • Example 1
NOTE When we declare a derived class object, this object inherits the members of the base class, but the derived class cannot directly access the private data members of the base class. The same is true for the member functions of the derived class. That is, the member functions of the derived class cannot directly access the private members of the base class.
Constructors • A derived class can have its own private data members • Member functions of the derived class cannot directly access the private members of the base class • The constructors of the derived class can (directly) initialize only the private data members of the derived class • When a derived class object is declared, it must also automatically execute one of the constructors of the base class
Constructors • The execution of a derived class’s constructor must trigger the execution of one of the base class’s constructors • A call to the base class’s constructor is specified in the heading part of the derived class constructor’s definition • Example 2
Protected Members of a Class • The private members of a class are private to the class and cannot be directly accessed outside the class • The derived class cannot access private members of a class • It is sometimes necessary for a derived class to access a private member of a base class • If you make a private member become public, then anyone can access that member
Protected Members of a Class • For a base class to give access to a private member to its derived class and still prevent its direct access outside the class, you must declare that member under the memberAccessSpecifier protected • The accessibility of a protected member of a class is in between public and private • A derived class can directly access the protected member of a base class
Inheritance Class B: memberAccessSpecifier A { . . };
Inheritance • 1. If memberAccessSpecifier is public, then • a. The public members of A are public members of B and they can be directly accessed in class B • b. The protected members of A are protected members of B and they can be directly accessed by the member functions (and friend functions) of B • c. The private members of A are hidden in B and they can be accessed by the member functions (and friend functions) of B through the public or protected members of A
Inheritance • 2. If memberAccessSpecifier is protected, then • a. The public members of A are protected members of B and they can be accessed by the member functions (and friend functions) of B • b. The protected members of A are protected members of B and they can be accessed by the member functions (and friend functions) of B • c. The private members of A are hidden in B and they can be accessed by the member functions (and friend functions) of B through the public or protected members of A
Inheritance • 3. If memberAccessSpecifier is private, then • a. The public members of A are private members of B and they can be accessed by the member functions (and friend functions) of B • b. The protected members of A are private members of B and they can be accessed by the member functions (and friend functions) of B • c. The private members of A are hidden in B and they can be accessed by the member functions (and friend functions) of B through the public or protected members of A • Example 3
Composition • In composition one or more member(s) of a class is an object of another class type • Composition is a “has-a” relation • The arguments to the constructor of a member-object are specified in the heading part of the definition of the constructor of the class • Member-objects of a class are constructed in the order they are declared, not in the order they are listed in the constructor’s member initialization list, and before the enclosing class objects are constructed. • Example 4
Programming Example • This programming example further illustrates the concepts of inheritance and composition • The mid-semester point at your local university is approaching • The registrar’s office wants to prepare the grade reports as soon as the students’ grades are recorded
Programming Example • Some of the students enrolled have not yet paid their tuition, however, • 1. If a student has paid the tuition, the grades are shown on the grade report together with the grade-point average (GPA) • 2. If a student has not paid the tuition, the grades are not printed • For these students, the grade report contains a message indicating that the grades have been held for nonpayment of the tuition • The grade report also shows the billing amount
The Data File • The data are stored in a file in the following form: 15000 345 studentName studentID isTuitionPaid numberOfCourses courseName courseNumber creditHours grade courseName courseNumber creditHours grade . studentName studentID isTuitionPaid numberOfCourses courseName courseNumber creditHours grade courseName courseNumber creditHours grade .
The Data File • The first line indicates the number of students enrolled and the tuition rate per credit hour • The students’ data is given thereafter • A sample-input file is: 3 345 Lisa Miller 890238 Y 4 Mathematics MTH345 4 A Physics PHY357 3 B ComputerSci CSC478 3 B History HIS356 3 A .
Output The desired output for each student is of the form: Student Name: Lisa Miller Student ID: 890238 Number of courses enrolled: 4 Course No Course Name Credits Grade CSC478 ComputerSci 3 B HIS356 History 3 A MTH345 Mathematics 4 A PHY357 Physics 3 B Total number of credits: 13 Mid-Semester GPA: 3.54
Input and Output • Input - a file containing data in the form given above • For easy reference in the rest of the discussion, we assume that the name of the input file is "a:stData.txt" • Output - a file containing output of the form given above
Problem Analysis • The two main components that we see are student and course • Course • the main characteristics of a course are the course name, course number, and number of credit hours • although the grade a student receives is not really a characteristic of a course, to simplify the program this component also includes the student’s grade
Problem Analysis • Operations on an object of the course type are: • 1. Set the course information • 2. Print the course information • 3. Show the credit hours • 4. Show the course number • 5. Show the grade • Student • The main characteristics of a student are the student name, student ID, number of courses in which enrolled, courses in which enrolled, and grade for each course
Problem Analysis • Because every student has to pay tuition, we also include a member to indicate whether the student has paid the tuition • Every student is a person, and every student takes courses • We have already designed a class personType to process a person’s first name and last name • We can derive the class studentType to keep track of a student’s information from the class personType, and one member of this class is of the type courseType
Algorithm Design • The basic operations to be performed on an object of the type studentType are as follows: • 1. Set the student information • 2. Print the student information • 3. Calculate the number of credit hours taken • 4. Calculate the GPA • 5. Calculate the billing amount • 6. Because the grade report will print the courses in ascending order, sort the courses according to the course number
Main Program • 1. Declare the variables • 2. Open the input file • 3. If the input file does not exist, exit the program • 4. Open the output file • 5. Get the number of students registered and the tuition rate • 6. Load the students’ data • 7. Print the grade reports
Variables • studentType studentList[maxNumberOfStudents]; //array to store the students’ data • int noOfStudents; //variable to store the //number of students • double tuitionRate; //variable to store the tuition rate • ifstream infile; //input stream variable • ofstream outfile; //output stream variable