CSC 306 Introduction to Programming with C++

More about modifying classes

Chapter 6


Objectives

  • Learn more about C++ classes
  • Learn to add to a C++ class that is already written

  • Adding to a Class in C++

    There are many reasons that you might be asked to add functionality to an existing C++ class. As businesses often expand the functionality of their products, you might find a need for a class you already designed need to add functionality to an existing class.

    In addition, open source code is a very large movement among computer programmers. "Open source code" has come to mean programmer access to working programs that include source code and allow distribution in source code as well as compiled form. According to the Open Source Initiative OSI, "The basic idea behind open source is very simple: When programmers can read, redistribute, and modify the source code for a piece of software, the software evolves. People improve it, people adapt it, people fix bugs. And this can happen at a speed that, if one is used to the slow pace of conventional software development, seems astonishing." In such cases, you can add functionality to existing classes in existing programs. You might be interested in visiting the open source initiative at http://www.opensource.org/ to find out more.


    Assignment Specifics

    This assignment must be completed individually.

    In the last assignment, you have used a class without changing it. Now, you will learn to add functionality to a C++ class.

    Suppose you wanted to answer questions like, "In 90 days, what will the date be?" or "in 45 days from April 15, 2004," what will the date be? You will write code to address these questions in this assignment.

    1. Start with a copy of DateDemo.cpp that you obtained in the last assignment.

    2. Write a main() program that prompts a user to enter a date and then an integer.

    3. Store the date the user inputs as a Date object, and then call a new public member function on this object to do the addition to this Date. Some pointers about this function:
      1. This function should be flexible enough to add any integer number of days to a Date.
        NOTE: The integer parameter can be positive or negative, you will be required to think about how to implement both cases.
      2. This function must be const-- it should NOT change the value of the calling date or the integer parameter.
      3. This function should then return a new Date object. (This means that your function will return an object of type Date.)
      4. This new function should not have any cin or cout --it should only do the computation.

    4. Output the newly obtained date in a readable fashion. You will probably find the print() member function useful here.

    Notes:

    When you are finished writing and testing your assignment, drop your source code into the CSC306_A14 dropbox on the Academic server.


    Back to Introduction to Computer Programming with C++ Homepage