In this lecture, you will: y Learn about classes y Learn about private, protected, and public members of a class
Fall 2004
CLASSES
y A class is a collection of a fixed number of components. y The components of a class are called members of the class. The general syntax of defining a class is: class classIdentifier { classMemberList };
y A member of a class can either be a variable (that is, to store some data) or a function.
Fall 2004
y If a member of a class is a variable, you declare it just like any other variable. Also, in the definition of the class, you cannot initialize a variable when you declare it. y If a member of a class is a function, you typically use the function prototype to define that member. y If a member of a class is a function, it can (directly) access any member of the classdata 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. y In C++, class is a reserved word and it only defines a data type, no memory is allocated. y The semicolon after the right brace is part of the syntax. y A missing semicolon, therefore, will result in a syntax error.
CSci 152: Prog II Fall 2004
y The members of a class are classified into three categories: private, public, and protected. This chapter mainly discusses the first two typesthat is, private and public. y Following are some facts about public and private members of a class: y By default, all members of a class are private. y If a member of a class is private, you cannot access it outside the class. y A public member is accessible outside the class. y To make a member of a class public, you use the label public with a colon. y In C++, private, protected, and public are reserved words. (member access specifiers)
Fall 2004
y Define a class, clockType, to implement the time of day in a program. y 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. y Perform the following operations on the time: 1. Set the time. 2. Return the time. 3. Print the time. 4. Increment the time by one second. 5. Increment the time by one minute. 6. Increment the time by one hour. 7. Compare the two times for equality.
Fall 2004
Fall 2004
y Some members of the class clockType will be private; others will be public. y 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. y The user should be able to set the time and print the time. Therefore, the members that set the time and print the time should be declared public.
Fall 2004
class clockType { public: void setTime(int, int, int); void getTime(int&, int&, int&); void printTime() const; void incrementSeconds(); void incrementMinutes(); void incrementHours(); bool equalTime(const clockType& otherTime) const; private: int hr; int min; int sec; };
CSci 152: Prog II Fall 2004
y y y
The class clockType has seven function members: setTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime. It has three data members: hr, min, and sec. The three data membershr, min, and sec, are private to the class and cannot be accessed outside the class. The seven function memberssetTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTimecan directly access the data members (hr, min, and sec). In the function equalTime, the parameter otherTime is a constant reference parameter. That is, in a call to the function equalTime, the parameter otherTime receives the address of the actual parameter, but otherTime cannot modify the value of the actual parameter. The word const at the end of the member functions printTime and equalTime specifies that these functions cannot modify the data members of a variable of the type clockType.
Fall 2004
Fall 2004
Fall 2004
Fall 2004
Example 13-1
myClock.setTime(5,2,30); myClock.printTime(); yourClock.setTime(x,y,z); //Assume x, y, and //z are variables of the type int if(myClock.equalTime(yourClock)) . . .
//illegal //illegal
Fall 2004
Fall 2004
Fall 2004
The statement myClock = yourClock; copies the value of yourClock into myClock a. the value of yourClock.hr is copied into myClock.hr, b. the value of yourClock.min is copied into myClock.min c. the value of yourClock.sec is copied into myClock.sec
Fall 2004
Class Scope
A class variable has the same scope as other variables. A member of a class is local to the class. We access a class member outside the class by using class variable name and the member selection operator (.).
Fall 2004
Fall 2004
y In C++, you can pass a variable by reference and still prevent Fall 2004 the function from changing its value, by using the keyword
Fall 2004
Quick Self-Quiz
Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday. The class should be able to perform the following operations: a. Set the day b. Print the day c. Return the day d. Return the next day e. Return the previous day
Fall 2004
Fall 2004
Fall 2004