Sams Teach Yourself C in 21 Days

(singke) #1
Working with C++ Classes and Objects 687

BD3



  • Three numeric values for month, day, year such as 1, 21, and 2001

  • A single string value such as “January, 21, 2001”

  • A string and two numeric values such as “January”, 21, and 2001
    For a class to be as useful as possible, try to create constructors and functions that can be
    called in as many ways as possible.


Review of the OOP Constructs in C++ ..............................................................


On the first Bonus Day, you learned that C++ is an object-oriented programming lan-
guage. You also learned that there were three features characteristic of an object-oriented
language. These are


  • Polymorphism

  • Encapsulation

  • Inheritance
    You have already covered how C++ can implement polymorphism and encapsulation.
    You learned that one of the primary ways C++ implements polymorphism is by function
    overloading. By overloading your functions, you can create routines that can react to dif-
    ferent parameters. You learned that by overloading constructors you can create objects
    that use different parameters for initializing. For example, in yesterday’s lesson there was
    a discussion on creating date objects. Such an object can be constructed in a variety of
    different formats, including text such as “December 25, 2001”, three numbers such as 12,
    25, 2001, or a combination of text and numbers such as “December”, 25, 2001. By over-
    loading an object’s constructor, you can create a function with the capability to work
    with any of these scenarios.


The more common use of polymorphism allows objects to be used as if they
were different types. Showing this use of polymorphism is beyond the scope
of this book.

Note


Encapsulation was also covered. Yesterday’s lesson taught you how to encapsulate infor-
mation and functionality into a C++ class. Using such a class, you learned that you can
instantiate objects that contain both data and functions.
The final characteristic of an object-oriented language is inheritance. In some of the fol-
lowing sections, you will learn the very basics of single inheritance. Before learning this,
you should first look at the idea of using classes as data members within other classes.

38 448201x-Bonus3 8/13/02 11:19 AM Page 687

Free download pdf