Classes - Classes In this chapter, you will: Learn about...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Classes In this chapter, you will: Learn about classes Learn about private, protected, and public members of a class Explore how classes are implemented Examine constructors and destructors Learn about the abstract data type (ADT) Explore how classes are used to implement ADT Learn about information hiding Explore how information hiding is implemented in C++ CLASSES A class is a collection of a fixed number of components. The components of a class are called members of the class. The general syntax of defining a class is: class classIdentifier { classMemberList }; A member of a class can either be a variable (that is, to store some data) or a function. If a member of a class is a variable, you declare it just like any other variable. In the definition of the class, you cannot initialize a variable when you declare it. If a member of a class is a function, you typically use the function prototype to define that member. If a member of a class is a function, it can (directly) access any member of the class data members and function members. That is, when you write the definition of the member function, you can directly access any data member of the class without passing it as a parameter. The only obvious condition is that you must declare an identifier before you can use it. In C++, class is a reserved word and it only defines a data type, no memory is allocated. The semicolon after the right brace is part of the syntax. A missing semicolon, therefore, will result in a syntax error.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The members of a class are classified into three categories: private public protected This chapter mainly discusses the first two types that is, private and public. Following are some facts about public and private members of a class: By default, all members of a class are private. If a member of a class is private, you cannot access it outside the class. A public member is accessible outside the class. To make a member of a class public, you use the label public with a colon. In C++, private, protected, and public are reserved words. Define a class, clockType, to implement the time of day in a program. Time is represented as a set of three integers: one to represent the hours, one to represent the minutes, and one to represent the seconds. Perform the following operations on the time: Set the time. Return the time. Print the time. Increment the time by one second. Increment the time by one minute. Increment the time by one hour. Compare the two times for equality.
Background image of page 2
Some members of the class clockType will be private; others will be public. Any member that needs to be accessed outside the class is declared public; any member that should not be accessed directly by the user should be declared private. The user should be able to set the time and print the time. Therefore, the members
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 19

Classes - Classes In this chapter, you will: Learn about...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online