Anda di halaman 1dari 25

CSci 152: Programming II

Fall 2004 Classes and Data Abstraciton

In this lecture, you will: y Learn about classes y Learn about private, protected, and public members of a class

CSci 152: Prog II

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.

CSci 152: Prog II

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)

CSci 152: Prog II

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.

CSci 152: Prog II

Fall 2004

CSci 152: Prog II

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.

CSci 152: Prog II

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

CSci 152: Prog II

Variable (Object) Declaration


y Once a class is defined, you can declare variables of that type. y In C++ terminology, a class variable is called a class object or class instance. y To become familiar with this terminology, we will use the term class object, or simply object, for a class variable. y The syntax for declaring a class object is the same as that for declaring any other variable. clockType myClock; clockType yourClock;

CSci 152: Prog II

Fall 2004

CSci 152: Prog II

Fall 2004

Accessing Class Members


y Once an object is declared, it can access the public members of a class. y The general syntax to access the member of a class is classVariableName.memberName y In C++, the dot, . (period), is an operator called the member access operator.

CSci 152: Prog II

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)) . . .

myClock.hr = 10; myClock.min = yourClock.min;


CSci 152: Prog II

//illegal //illegal
Fall 2004

Built-in Operations on Classes


y Most of C++s built-in operations do not apply to classes. y You cannot use arithmetic operators to perform arithmetic operations on class objects (unless they are overloaded). y For example, you cannot use the operator + to add two class objects of, say, the type clockType. y Also, you cannot use relational operators to compare two class objects for equality. y The two built-in operations that are valid for class objects are member access (.) and assignment (=).

CSci 152: Prog II

Fall 2004

The Assignment Operator and Classes


Suppose that myClock and yourClock are variables of the type clockType.

CSci 152: Prog II

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

CSci 152: Prog II

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 (.).

CSci 152: Prog II

Fall 2004

Functions and Classes


y Class objects can be passed as parameters to functions and returned as function values. y As parameters to functions, classes can be passed either by value or by reference. y If a class object is passed by value, the contents of the data members of the actual parameter are copied into the corresponding data members of the formal parameter.

CSci 152: Prog II

Fall 2004

Reference Parameters and Class Objects (Variables)


y As a parameter, a class object can be passed by value. y If a variable is passed by value, the corresponding formal parameter receives a copy of the data of the variable. This operation might require, in addition to a large amount of storage space, a considerable amount of computer time to copy the value of the actual parameter into the formal parameter. y If a variable is passed by reference, the formal parameter receives only the address of the actual parameter. Therefore, an efficient way to pass a variable as a parameter is by reference. y If a variable is passed by reference, then when the formal parameter changes, the actual parameter also changes.
CSci 152: Prog II

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

void testTime(const clockType& otherClock) { clockType dClock; ... }

CSci 152: Prog II

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

CSci 152: Prog II

Fall 2004

Answer Quick Self-Quiz


class DayType { public: void setDay(string newDay); void printDay(); string today(); string nextDay(); string prevDay(); private: string day; }

CSci 152: Prog II

Fall 2004

Answer Quick Self-Quiz


void DayType::setDay(string newDay) { day = newDay; } void DayType::printDay() { cout << day; } string DayType::today() { return day; }

CSci 152: Prog II

Fall 2004

Answer Quick Self-Quiz


string DayType::nextDay() { if ((day == Sun) || (day == SUN) || day == (Sunday)) return Mon; else if ((day == Mon) || (day == MON) || day == (Monday)) return Tue; else if ((day == Tue) || (day == TUE) || day == (Tuesday)) return Wed; else if ((day == Wed) || (day == WED) || day == (Wednesday)) return Thu; else if ((day == Thu) || (day == THU) || day == (Thursday)) return Fri; else if ((day == Fri) || (day == FRI) || day == (Friday)) return Sat; else if ((day == Sat) || (day == SAT) || day == (Saturday)) return Sun; else // should be an error return day; }
CSci 152: Prog II Fall 2004

Anda mungkin juga menyukai