Chapter 6: More About Classes and Objects

Download CD Content


In this chapter, you will learn how to:

  • Organize classes into header files and source files

  • Create and destroy objects

  • Define constructors to initialize an object and a destructor to tidy up when an object is destroyed

  • Define class-wide members by using the static keyword

  • Define relationships between different objects in an application

As you saw in Chapter 2, C++ is an object-oriented programming language. You define classes to represent the important types of entities in your application, and you create objects as instances of these classes. For example, a Human Resources application might define classes such as Employee and Contract. When the application is running, it might create a new Employee object every time a new employee joins the company and a new Contract object to describe the employee’s terms of employment.

This chapter builds on the introduction to classes and objects in Chapter 2. In this chapter, you’ll see how to organize classes into header files and source files, which enables you to keep a clean separation between a class definition and its implementation. You’ll also learn how to provide constructors to initialize new objects when they’re created. Likewise, you’ll provide destructors to tidy up objects just before they are destroyed.

Most of the data members and member functions in a class are instance members because they pertain to specific instances of the class. It’s also possible to define class members, which pertain to the class as a whole. You’ll see how to define class members in this chapter using the static keyword.

Finally you’ll see how to create object relationships in C++. This concept is important in object-oriented programming because it allows objects to communicate with each other in a running application.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: