25
CSci 152: Programming II Fall 2004 Classes and Data Abstraciton

l8 Classes

Embed Size (px)

Citation preview

Page 1: l8 Classes

CSci 152: Programming IIFall 2004

Classes and Data Abstraciton

Page 2: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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

Page 3: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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.

Page 4: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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.

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.

Page 5: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

The members of a class are classified into three categories: private, public, and 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. (member access specifiers)

Page 6: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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:

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.

Page 7: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Page 8: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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 that set the time and print the time should be declared public.

Page 9: l8 Classes

CSci 152: Prog II Fall 2004CSci 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;

};

Page 10: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

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 members—hr, min, and sec, are private

to the class and cannot be accessed outside the class. The seven function members—setTime, getTime,

printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime—can 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.

Page 11: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Variable (Object) Declaration Once a class is defined, you can declare variables of that type. In C++ terminology, a class variable is called a class object or

class instance. To become familiar with this terminology, we will use the term

class object, or simply object, for a class variable. The syntax for declaring a class object is the same as that for

declaring any other variable.

clockType myClock;

clockType yourClock;

Page 12: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Page 13: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Accessing Class Members Once an object is declared, it can access the public

members of a class.

The general syntax to access the member of a class is

classVariableName.memberName

In C++, the dot, . (period), is an operator called the member access operator.

Page 14: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Example 13-1myClock.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; //illegal

myClock.min = yourClock.min; //illegal

Page 15: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Built-in Operations on Classes Most of C++’s built-in operations do not apply to classes.

You cannot use arithmetic operators to perform arithmetic operations on class objects (unless they are overloaded).

For example, you cannot use the operator + to add two class objects of, say, the type clockType.

Also, you cannot use relational operators to compare two class objects for equality.

The two built-in operations that are valid for class objects are member access (.) and assignment (=).

Page 16: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

The Assignment Operator and Classes

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

Page 17: l8 Classes

CSci 152: Prog II Fall 2004CSci 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

Page 18: l8 Classes

CSci 152: Prog II Fall 2004CSci 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 (.).

Page 19: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Functions and Classes Class objects can be passed as parameters to functions and

returned as function values. As parameters to functions, classes can be passed either by

value or by reference. 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.

Page 20: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Reference Parameters and Class Objects (Variables) As a parameter, a class object can be passed by value.

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.

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.

If a variable is passed by reference, then when the formal parameter changes, the actual parameter also changes.

In C++, you can pass a variable by reference and still prevent the function from changing its value, by using the keyword const in the formal parameter declaration.

Page 21: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

void testTime(const clockType& otherClock)

{

clockType dClock;

...

}

Page 22: l8 Classes

CSci 152: Prog II Fall 2004CSci 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

Page 23: l8 Classes

CSci 152: Prog II Fall 2004CSci 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;}

Page 24: l8 Classes

CSci 152: Prog II Fall 2004CSci 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;}

Page 25: l8 Classes

CSci 152: Prog II Fall 2004CSci 152: Prog II Fall 2004

Answer Quick Self-Quizstring 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;}