Professional Documents
Culture Documents
03 Classes
03 Classes
Applied Unviversity
CLASSES
1. Classes and structures can be used almost identically in C++. The difference
between the two is in the default accessibility associated with the members of
each (to be discussed later).
a) Members are public by default in structs.
b) Members are private by default in classes.
class class-name {
public:
member-function-prototypes
private:
data-members
};
1
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
class Time {
public:
Time();
void setTime(int, int, int);
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
2
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
f) The class definition contains prototypes for the following four member
functions after the public member access specifier -- Time, setTime,
printMilitary, and printStandard.
a) These are the public member functions, or public
services or interface of the class.
b) These functions will be used by clients (i.e. portions of a
program that are users) of the class to manipulate the data
of the class.
g) The member function with the same name as the class is called a
constructor function of that class.
i) A constructor is a special member function that initializes the
data members of a class object.
ii) A class's constructor function is called automatically when an
object of the class is created.
iii) It is common to have several constructors for a class; this is
accomplished through function overloading.
h) The three integer members appear after the private member access
specifier.
i) These data members of the class are only accessible to member
functions (and, to be discussed later, "friends" of the class).
ii) Data members are normally listed in the private portion of a
class and member functions are normally listed in the public
portion.
6. Once the class has been defined, the class name becomes a new type specifier
and can be used as a type in declarations as follows:
a) Time sunset; // object of type Time
b) Time arrayOfTimes[5]; // array of Time objects
c) Time *pointerToTime; // pointer to a Time object
d) Time &dinnerTime = sunset; // reference to (alias of) a Time object
3
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
8. The following is a listing of the file which contains the specification (i.e. the
definition of the class), the implementation (i.e. the definition of the functions
contained in the class) of the class Time, and the client routine (the driver or
main() program):
a) Note the Time constructor which initializes the data members to 0 (the
military time equivalent of 12 AM).
i) This ensure that the object is in a consistent state when it is
created.
ii) Invalid values cannot be stored in the data members of a Time
object because the constructor is automatically called when the
Time object is created and all subsequent attempts by a client to
modify the data members are scrutinized by the function
setTime.
b) Member functions can be defined inside a class, but it is a good
programming practice to define the functions outside the class
definition.
4
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
c) Note the use of the scope resolution operator (::) in each member
function definition following the class definition.
i) Once a class is defined and its member functions are declared,
the member functions must be defined.
ii) Each member function of the class can be defined directly in the
class specification (rather than including the function prototype
of the class), or the member function can be defined after the
class body.
a) When a member function is defined outside of its
corresponding class definition, the function name is
preceded by the class name and the scope resolution
operator (::).
b) Because different classes can have the same member
names, the scope resolution operator "ties" the member
name to the class name to uniquely identify the member
functions of a particular class. The member function is
considered to be within that class's scope.
d) If a member function is defined in a class definition, the member
function is automatically inlined. Member functions defined outside a
class definition may be made inline by explicitly using the keyword
inline. The compiler reserves the right not to inline any function.
5
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
// Time class.
#include <iostream.h>
6
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
7
OOP Course Al-Balqa
Applied Unviversity
CLASSES (CONTINUED)
Output is:
8
OOP Course Al-Balqa
Applied Unviversity
9
OOP Course Al-Balqa
Applied Unviversity
main()
{
int value = 7;
return 0;
}
Output is:
Local value = 7
Global value = 1.2345
6. The operators used to access class members are identical to the operators
used to access structure members.
a) The dot member selection operator (.) is combined with an object's
name or with a reference to an object to access the object's members.
b) The arrow member selection operator (->) is combined with a pointer
to an object to access that object's members.
10
OOP Course Al-Balqa
Applied Unviversity
main()
{
Count counter, // create counter object
*counterPtr = &counter, // pointer to counter
&counterRef = counter; // reference to counter
cout << "Assign 7 to x and print using the object's name: ";
counter.x = 7; // assign 7 to data member x
counter.print(); // call member function print
11
OOP Course Al-Balqa
Applied Unviversity
Output is:
12
OOP Course Al-Balqa
Applied Unviversity
13
OOP Course Al-Balqa
Applied Unviversity
// TIME1.H
// Declaration of the Time class.
// Member functions are defined in TIME1.CPP
#endif
14
OOP Course Al-Balqa
Applied Unviversity
// TIME1.CPP
// Member function definitions for Time class.
#include <iostream.h>
#include "time1.h"
15
OOP Course Al-Balqa
Applied Unviversity
// FIG6_5.CPP
// Driver for Time1 class
// NOTE: Compile with TIME1.CPP
#include <iostream.h>
#include "time1.h"
16
OOP Course Al-Balqa
Applied Unviversity
1. The member access specifiers public and private (and, to be discussed later,
protected) are used to control access to a class's data members and member
functions.
a) The default access mode for classes is private so all members after the
class header and before the first label are private.
b) After each label, the mode that was invoked by that label applies until
the next label or until the terminating right brace (}) of the class
definition.
c) The labels public, private, and protected may be repeated but such
usage is rare and can be confusing.
2. Private class members can be accessed only by member functions (and friend
functions) of that class.
5. Try to keep all of the data members of a class private. Provide public
member functions to set the values of private data members and to get the
values of private data members. This architecture:
a) Helps hide the implementation of a class from its clients, which
reduces bugs.
b) Improves program modifiability.
17
OOP Course Al-Balqa
Applied Unviversity
9. Despite the fact that public and private labels may be repeated and
intermixed, list all the public members of a class first in one group and then
list all the private members in another group. This focuses the client's
attention on the class's public interface rather than on the class's
implementation.
10. The default access for struct members is public. Access to members of a
struct also may be explicitly set to public, protected, or private.
18
OOP Course Al-Balqa
Applied Unviversity
main()
{
Time t;
return 0;
}
19
OOP Course Al-Balqa
Applied Unviversity
20
OOP Course Al-Balqa
Applied Unviversity
// TIME2.H
// Declaration of the Time class.
// Member functions defined in TIME2.CPP
// prevent multiple inclusions of header file
#ifndef TIME2_H
#define TIME2_H
class Time {
public:
Time(int = 0, int = 0, int = 0); // default constructor
void setTime(int, int, int);
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
#endif
21
OOP Course Al-Balqa
Applied Unviversity
// TIME2.CPP
// Member function definitions for Time class.
#include <iostream.h>
#include "time2.h"
22
OOP Course Al-Balqa
Applied Unviversity
main()
{
Time t1, t2(2), t3(21, 34), t4(12, 25, 42), t5(27, 74, 99);
cout << endl << "hour specified; minute and second defaulted:" << endl << " ";
t2.printMilitary();
cout << endl << " ";
t2.printStandard();
cout << endl << "hour and minute specified; second defaulted:" << endl << " ";
t3.printMilitary();
cout << endl << " ";
t3.printStandard();
cout << endl << "hour, minute, and second specified:" << endl << " ";
t4.printMilitary();
cout << endl << " ";
t4.printStandard();
cout << endl << "all invalid values specified:" << endl << " ";
t5.printMilitary();
cout << endl << " ";
t5.printStandard();
cout << endl;
return 0;
}
23
OOP Course Al-Balqa
Applied Unviversity
Output is:
Constructed with:
all arguments defaulted:
00:00:00
12:00:00 AM
hour specified; minute and second defaulted:
02:00:00
2:00:00 AM
hour and minute specified; second defaulted:
21:34:00
9:34:00 PM
hour, minute, and second specified:
12:25:42
12:25:42 PM
all invalid values specified:
00:00:00
12:00:00 AM
24
OOP Course Al-Balqa
Applied Unviversity
DESTRUCTORS
25
OOP Course Al-Balqa
Applied Unviversity
2. The order in which these function calls are made depends on the order in
which execution enters and leaves the scope in which objects are instantiated.
3. Generally, destructor calls are made in the reverse order of the constructor
calls.
a) Global scope:
i) Constructors are called for objects declared in global scope
before any other function (including main) in that file begins
execution.
ii) The corresponding destructors are called when main terminates
or the exit function is called.
b) Local objects:
i) Constructors are called for automatic local objects when
execution reaches the point where the objects are declared.
ii) The corresponding destructors are called when the objects leave
scope (i.e. the block in which they are declared is exited).
c) Static local objects:
i) Constructors are called for static local objects once when
execution reaches the point where the objects are first declared.
ii) Corresponding destructors are called when main terminates or
the exit function is called.
26
OOP Course Al-Balqa
Applied Unviversity
// CREATE.H
// Definition of class CreateAndDestroy.
// Member functions defined in CREATE.CPP.
#ifndef CREATE_H
#define CREATE_H
class CreateAndDestroy {
public:
CreateAndDestroy(int); // constructor
~CreateAndDestroy(); // destructor
private:
int data;
};
#endif
---------------------------------------------------------------------------------------------------
// CREATE.CPP
// Member function definitions for class CreateAndDestroy
#include <iostream.h>
#include "create.h"
CreateAndDestroy::CreateAndDestroy(int value)
{
data = value;
cout << "Object " << data << " constructor";
}
CreateAndDestroy::~CreateAndDestroy()
{ cout << "Object " << data << " destructor " << endl; }
27
OOP Course Al-Balqa
Applied Unviversity
main()
{
cout << " (global created before main)" << endl;
CreateAndDestroy second(2); // local object
cout << " (local automatic in main)" << endl;
CreateAndDestroy seventh(7);
cout << " (local automatic in create)" << endl;
}
28
OOP Course Al-Balqa
Applied Unviversity
Output is:
29
OOP Course Al-Balqa
Applied Unviversity
3. Memberwise copy can cause serious problems when used with a class whose
data members contain dynamically allocated storage.
30
OOP Course Al-Balqa
Applied Unviversity
main()
{
Date date1(7, 4, 1993), date2; // d2 defaults to 1/1/90
return 0;
}
Output is:
date1 = 7-4-1993
date2 = 1-1-1990
31
OOP Course Al-Balqa
Applied Unviversity
BIBLIOGRAPHY
Cantu, Marco and Steve Tendon, Borland C++ 4.0 Object-Oriented Programming,
New York: Random House, 1994.
Deitel, H.M. and P.J. Deitel, C++ How to Program, Second Edition, Eaglewood
Cliffs, New Jersey: Prentice Hall, 1998.
Gorlen, Keith E., Sanford M. Orlow, and Perry S. Plexico, Data Abstraction and
Object-Oriented Programming in C++, New York: John Wiley & Sons, 1991.
Horstmann, Cay S., Mastering Object-Oriented Design in C++, New York, NY:
John Wiley & Sons, Inc., 1995.
Savitch, Walter, Problem Solving with C++, Second Edition, Menlo Park, Ca.:
Addison-Wesley Publishing Company, Inc., 1999.
Swan, Tom, Mastering Borland C++ 4.5, Second Edition, Indianapolis, Indiana:
SAMS, 1995.
32