Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #5: Pointers, Dynamic Arrays and Inheritance

Similar presentations


Presentation on theme: "Homework #5: Pointers, Dynamic Arrays and Inheritance"— Presentation transcript:

1 Homework #5: Pointers, Dynamic Arrays and Inheritance
By J. H. Wang May 26, 2014

2 Part I: Hand-Written Exercises
Answer these questions about destructors. (a) How does the compiler identify a destructor? (b) Can the destructor be called explicitly? (c) What does a destructor actually do? (d) What should a destructor do? (a) Explain carefully why no overloaded assignment operator is needed when the only data in a class consists of built-in types. (b) Same as part(a) for a copy constructor. (c) Same as part(a) for a destructor.

3 You know that an overloaded assignment operator and a copy constructor are not inherited. Does this mean that if you do not define an overloaded assignment operator or a copy constructor for a derived class, then that derived class will have no assignment operator and no copy constructor? Please explain your reasons.

4 Part II: Programming Exercises
4. Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication. Discussion: A variable in a polynomial does nothing but act as a placeholder for the coefficients. Hence, the only interesting thing about polynomials is the array of coefficients and the corresponding exponent. Think about the polynomial x*x*x + x+ 1. Where is the term in x*x? One simple way to implement the polynomial class is to use an array of doubles to store the coefficients. The index of the array is the exponent of the corresponding term. If a term is missing, then it simply has a zero coefficient. (To be continued on the next slide…)

5 (…Continued from the previous slide) [There are techniques for representing polynomials of high degree with many missing terms. These use so-called sparse matrix techniques. Unless you already know these techniques, or learn very quickly, don’t use these techniques.] (1) Provide a default constructor, a copy constructor, and a parameterized constructor that enables an arbitrary polynomial to be constructed. (2) Supply an overloaded operator = and a destructor. (To be continued on the next slide…)

6 (…Continued from the previous slide) (3) Provide these operations: polynomial+polynomial, constant+polynomial, polynomial+constant, polynomial-polynomial, constant-polynomial, polynomial-constant, polynomial*polynomial, constant*polynomial, polynomial*constant, (4) Supply functions to assign and extract coefficients, indexed by exponent. (5) Supply a function to evaluate the polynomial at a value of type double. You should decide whether to implement these functions as members, friends, or standalone functions.

7 5. Given the definition of two classes, Patient and Billing, whose objects are records for a clinic. Patient will be derived from the class Person given in Programming Project A patient record has the patient’s name (inherited from the class Person) and primary physician, of type Doctor defined in Programming Project A Billing object will contain a Patient object and a Doctor object, and an amount due of type double. (To be continued on the next slides…)

8 (… continued from the previous slide) Be sure your classes have a reasonable complement of constructors and accessor methods, an overloaded assignment operator, and a copy constructor. First write a driver program to test all your methods, then write a test program that creates at least two patients, at least two doctors, at least two Billing records, then prints out the total income from the Billing records.

9 Related definitions Project 14.4: class Person { public: Person(); Person(string theName); Person(const Person& theObj); string getName() const; Person& operator=(const Persons& rtSide); friend istream& operator>>(istream& inS, Persons& perObj); friend ostream& operator<<(ostream& outS, const Persons& perObj); private: string name; };

10 Project 14.3: Give the definition of a class named Doctor whose objects are records for a clinic’s doctors. This class will be a derived class of the class SalariedEmployee given in Display A Doctor record has the doctor’s specialty (such as Pediatrician, Obstetrician, General Practitioner, etc., so use type string), and office visit fee (use type double). Be sure your class has a reasonable complement of constructors and accessor methods, an overloaded assignment operator, and a copy constructor. Write a driver program that tests all your methods.

11 Homework Submission Due: 2 weeks (June 9, 2014) Hand-written exercises
Please write your names and answers on papers. Programs Please submit to homework submission Web site:

12 Any Questions or Comments?


Download ppt "Homework #5: Pointers, Dynamic Arrays and Inheritance"

Similar presentations


Ads by Google