Homework #5: Pointers, Dynamic Arrays and Inheritance

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
1 Lab Session-5 CSIT221 Spring 2003 Default and Parameterized Constructors Destructors Programming Exercise for building a template based class (demo required)
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Matrices Introducing Inheritance. Consider A matrix is a grid in which numbers can be stored. Algorithms for problems in scientific computing frequently.
Homework #3: Classes and Constructors
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang Jun. 5, 2009.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang May 31, 2011.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 Introduction to Object Oriented Programming Chapter 10.
Homework #3: Classes and Constructors
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
ECE 264 Object-Oriented Software Development
Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Constructors and Destructors
Learning Objectives Pointers as dada members
Strings: C-strings vs. Strings as Objects
Overloading Operator MySting Example
Andy Wang Object Oriented Programming in C++ COP 3330
Introducing Inheritance
A First C++ Class – a Circle
Foundational Data Structures
Strings: C-strings vs. Strings as Objects
Constructors and destructors
Constructors and Destructors
Objective Please copy on your packet
C++ support for Object-Oriented Programming
Presentation transcript:

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

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.

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.

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…)

(…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…)

(…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.

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 14.4. A patient record has the patient’s name (inherited from the class Person) and primary physician, of type Doctor defined in Programming Project 14.3. 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…)

(… 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.

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; };

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 14.4. 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.

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: http://mslin.ee.ntut.edu.tw/

Any Questions or Comments?