prelab5
DESCRIPTION
preparing for lab 5TRANSCRIPT
Programming Techniques Lab 1st YearCairo UniversityFaculty of EngineeringComputer Engineering Department
CMP 103 Programming Techniques Lab Spring 2014
LAB #5Relation between Classes:
Inheritance
ObjectivesAfter this lab, the student should be able to
Reuse code by inheriting from an existing class. Access base class members and functions from the derived class. Add new members and functionality to the derived class. Use upcasting and downcasting.
Lab Outline1- Creating a Class Hierarchy
Inheritance versus Composition. (has-a and is-a relationships). Inheritance as a way of code reuse. Relationship between base and derived classes. (is-a relationship). What is inherited and what is not.
Constructor & Destructor not inherited Friend functions not inherited. Other base class functions inherited Overloaded operators (except = operator) inherited
Calling a base class function from the derived class. Derived class can have its own member data and functions.
2- Construction and Destruction When creating an object of a derived class
Constructor of the derived class is called. Constructor of the base class is executed first, then Constructor of any composed objects is executed, then Constructor of the derived class is executed.
Initializer list Must be used if there is no default constructor in the base class. Doesn't affect the construction order.
Object destruction works in the inverse way of construction. See Examples – Part1
3- Function Overriding A function with the same signature in the derived class overrides that of the base. The overriding function hides all overloaded versions in the base from the derived
class. Calling an overridden base class function from the derived class. See Examples – Part2
Spring 2014 1/4 Lab #5
Programming Techniques Lab 1st Year
4- Upcasting and Downcasting Upcasting is safe: All functions/members of base class are part of the derived class. Downcasting is NOT safe:
Some functions/members may be in the derived class but not in the base class.
Can't be done for objects. See Examples – Part2
Examples (Examples.sln)
1. Inheritance2. Overriding and Casting
Exercise 1 (ungraded) Implement the following classes (add any more needed functions)
Class Engine
Variable Name
Variable type
Torque floatSpeed float
It has one constructor that takes Torque and Speed as parameters A function PrintInfo that prints Engine data
Class Car Given that car consists of an Engine and plate number A function PrintInfo() that prints the values of all the members of the Car At least 2 constructors should exist
o One that takes engine torque and speed and plate number as parameters o One that takes no parameters and sets the values to defaults of your choice.
Class RacingCar Given that the Racing car is a type of cars A racing car should have a sponsor name A function PrintInfo() that prints the values of all the members of the Racing car A constructor that takes all racing car parameters
Spring 2014 2/4 Lab #5
Programming Techniques Lab 1st Year
Test you program using the following main
Car C1(400,0.65, 112461);
Car C2;
C1.PrintInfo();
C2.PrintInfo();
C2=C1;
C2.setSpeed(200);
C2.PrintInfo();
RacingCar C3("Ferrari",350,0.5,336749);
RacingCar C4("Mercedes",250,0.45,446261);
C3.PrintInfo();
C4.PrintInfo();
C3.setSponser("Hyundai");
C4.setSpeed(300);
C3.PrintInfo();
C4.PrintInfo();
Exercise 2 (ungraded) Implement the following classes:Class Employee
- Data members: name (string), pay rate (float), pay period (integer, 1- weekly, 2- bi-weekly, 3- monthly)
- Member functions: - A default constructor to initialize the employee data. - Get_employee_data that return all employee basic data- Get_employee_salary that return the employee’s pay (number of
working hours * pay rate)
Class MangerManager is a regular employee with the difference that they may be paid with a fixed salary regardless the number of working hours.
- IsSalaried flag indicates if the manager paid a fixed salary or not- GetSalaried returns the value of Issalaried flag- get_employee_salary that returns manager salary- a constructor to set manger information.
Spring 2014 3/4 Lab #5
Programming Techniques Lab 1st Year
Class Supervisor A supervisor is responsible for employees in a specific department and must: Have a field to store the department name (as a string). Have getDept() and setDept() methods to access the department field. Always be salaried (i.e., pay for a single pay period is fixed, no matter how many hours are
worked). Have a constructor that takes initial values for all fields.What class should Supervisor inherit from?
Write the main to test your classes .
Spring 2014 4/4 Lab #5