What is a collection of a fixed number of components?

Classes and Data Abstraction We have learned how to group of data items that are of different data type using a struct. C++ also provides another structured data type, called a classes extent structures in that they can group data as well as functions. Definition: A class is a collection of a fixed number of components; the components of a class are called the members of a class. The general syntax for defining a class is: class classIdentifier { classMembersList };

where classMembersList consists of a variable declaration and or functions. That is, a member of a class can be either a variable (to store data) or a function (to manipulate it). • If a member of a class is a variable, you declare it just like any other variable. But, variables in classes cannot be initialized 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. In C++, class is a reserved word, and it defines only a data type; no memory is allocated. the ; after the right brace is part of the syntax.

The members of a class are classified into 3 categories private, public, and protected. These are reserved words and called member access specifiers. We will concentrate on the 2 first for now. How to use them: • By default, all members of a class are private. If a member 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: General Definition of a class class class_name {

access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Suppose, we want to define a class to implement the time of day in a program, let’s call this class ClockType. To represent time, we will use three int variables: one to represent the hours, one to represent the minutes and one to represent the seconds. We also want to 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 two times for equality So, our class ClockType has 10 members: 3 data members and 7 function members

setTime  getTime  printTime  incrementSeconds  incrementMinutes  incrementHours  equalTime      hr

  min

  sec

  class ClockType and its members

Some members of the class ClockType will be private, others will be public.

Deciding which ones to make public and which ones to make private depends on the nature of the members. General Access rule: Any member that needs to be accessed outside the class is declared public, usually function members are declared public. For example, the user should be able to set the time, print the time, increment and compare times for equality. Any member that should not be accessed directly by the user should be declared private. Usually data members are declared private. To control the direct manipulation of the data member’s hr, min and sec, we will declare them private.

The following statements define the class ClockType 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& otherClock) const; private: int hr; int min; int sec; };

This class has 3 data members and 7 function members. In the function equalTime, the parameter otherClock is a constant reference parameter. The word const at the end of the functions specifies that the functions cannot modify the data members of a variable of the type ClockType. Variable (Object) definition: Once a class has been defined, you can declare variables of that type. In C++, a class variable is called a class object or a class instance or simply object. The syntax for declaring an object is the same as for declaring a variable. The following statements declare two objects of the type ClockType. ClockType myClock; ClockType yourClock; Each object has 10 members: seven function members and three data members. Each object has a separate memory allocated for hr, min, sec Accessing Class members

Once an object is declared, it can access the public members of a class. The general syntax to access the members of a class is:

classObjectName.memberName

The . (dot) operator, is an operator called the member access operator. myClock.setTime(5, 2, 30); myClock.printTime(); youClock.setTime(x, y, z); if (myClock.equalTime(yourClock)) {…..}

Illegal Operations: An object can access only public members of the class. Members that have been declared private cannot be accessed directly.

Built-in Operations on Classes Assignment: Most of the operations defined for variables cannot be used on objects. However, we can use the assignment operator to make the data members of one object equal the data members of another Example: ClockType myClock, yourClock; myClock.setTime(4, 29, 12); hr

4

min        29    sec          12

myClock yourClock=myClock; hr     min

4    29

  sec         12

    yourClock

Class Scope:

A class object can be either automatic (created at declaration time, and destroyed when control exits) or static A member of a class is local to the class. so it cannot be accessed outside of it.

Implementation of Member functions: When we defined the class ClockType, we included only the function prototype for member functions. For these functions to work properly, we need to write the related code. To write the definitions of the member functions of the class ClockType, we need to refer to the data members and

the function members. Because they are local to the class we cannot reference them directly outside of the class that is why we need to use the scope resolution operator:: For example the function setTime will be defined as: void ClockType::setTime(int hours, int minutes, int seconds){ if (0