1. Home
  2. Computing & Technology
  3. C / C++ / C#

Learn about C++ Classes and Objects

By , About.com Guide

2 of 9

Understanding the Book Class

In the main() function a variable ABook of type Book is created with the value 128. As soon as execution reaches this point, the object ABook is constructed. On the next line the method ABook.SetPage() is called and the value 56 assigned to the object variable ABook.CurrentPage. Then cout outputs this value by calling the Abook.GetCurrentPage() method.

When execution reaches the return 0; the ABook object is no longer needed by the application. The compiler generates a call to the destructor.

Declaring Classes

Everything between Class Book and the } is the class declaration. This class has two private members, both of type int. These are private because the default access to class members is private.

The public: directive tells the compiler that access from here on is public. Without this, it would still be private and prevent the three lines in the main() function from accessing Abook members. Try commenting the public: line out and recompiling to see the ensuing compile errors.

This line below declares a Constructor. This is the function called when the object is first created.

Book( int Numpages) ; // Constructor
It is called from the line
Book ABook(128) ;
This creates an object called ABook of type Book and calls the Book() function with the parameter 128.

Note : On the next page : Learn about Constructors.

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Understanding the Book Class

©2009 About.com, a part of The New York Times Company.

All rights reserved.