Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
CHAP 06

CHAP 06

Ratings:

5.0

(1)
|Views: 179 |Likes:
Published by anon-143950

More info:

Published by: anon-143950 on Jun 02, 2007
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

01/01/2013

pdf

text

original

 
6.Classes
This chapter introduces the class construct of C++ for defining new datatypes.
A data type consists of two things:
A concrete representation of the objects of the type.
A set of operations for manipulating the objects.
Added to these is the restriction that, other than the designatedoperations, no other operation should be able to manipulate the objects. For this reason, we often say that the operations
characterize
the type, that is, theydecide what can and what cannot happen to the objects. For the same reason, proper data types as such are often called
abstract data types
– abstract because the internal representation of the objects is hidden from operationsthat do not belong to the type.
A
class definition
consists of two parts: header andbody. The class
header
specifies the class
name
and its
base classes
. (The latter relates to derived classes and isdiscussed in Chapter 8.) The class
body
defines the class
members
. Two types of members are supported:
Data members
have the syntax of variable definitionsand specify the representation of class objects.
Member functions
have the syntax of functionprototypes and specify the class operations, also calledthe class
interface
.Class members fall under one of three different accesspermission categories:
Public
members are accessible by all class users.
Private
members are only accessible by the classmembers.
Protected
members are only accessible by the classmembers and the members of a derived class. The data type defined by a class is used in exactly thesame way as a built-in type.
82C++ ProgrammingCopyright © 1998 Pragmatix Software
 
A Simple Class
Listing 6.1 shows the definition of a simple class forrepresenting points in two dimensions.
Listing 6.2
123456class Point {int xVal, yVal;public:void SetPt (int, int);void OffsetPt (int, int);};
Annotation
1This line contains the class header and names the classas
Point
. A class definition always begins with the keyword
class
, followed by the class name. An open brace marksthe beginning of the class body.2This line defines two data members,
xVal
and
yVal
, both of type
int
. The default access permission for a classmember is private. Both
xVal
and
yVal
are thereforeprivate.3This keyword specifies that from this point onward theclass members are public.4-5These two are public member functions. Both have twointeger parameters and a void return type.6This brace marks the end of the class body. The order in which the data and member functions of aclass are presented is largely irrelevant. The above class, forexample, may be equivalently written as:
class Point {public:void SetPt (int, int);void OffsetPt (int, int);private:int xVal, yVal;};
 The actual definition of the member functions is usuallynot part of the class and appears separately. Listing 6.3shows the separate definition of 
SetPt
and
OffsetPt
.
83C++ ProgrammingCopyright © 1998 Pragmatix Software
 
Listing 6.4
12345678910void Point::SetPt (int x, int y){xVal = x;yVal = y;}void Point::OffsetPt (int x, int y){xVal += x;yVal += y;}
Annotation
1The definition of a class member function is very similarto a normal function. The function name should bepreceded by the class name and a double-colon. Thisidentifies
SetPt
as being a member of 
Point
. The functioninterface must match its earlier interface definition withinthe class (i.e., take two integer parameters and have thereturn type void).3-4Note how
SetPt
(being a member of 
Point
) is free to refer to
xVal
and
yVal
. Non-member functions do not have thisprivilege.Once a class is defined in this way, its name denotes anew data type, allowing us to define variables of that type.For example:
Point pt;// pt is an object of class Pointpt.SetPt(10,20);// pt is set to (10,20)pt.OffsetPt(2,2);// pt becomes (12,22)
Member functions are called using the dot notation:
pt.SetPt(10,20)
calls
SetPt
for the object
pt
, that is,
pt
is an implicit argument to
SetPt
.
By making
xVal
and
yVal
private members of the class, wehave ensured that a user of the class cannot manipulatethem directly:
pt.xVal = 10;//
illegal
 This will not compile.At this stage, we should clearly distinguish betweenobject and class. A class denotes a type, of which there isonly one. An object is an element of a particular type (class),of which there may be many. For example,
Point pt1, pt2, pt3;
www.pragsoft.comChapter 6: Classes84

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->