|
A Sample Program
The following program illustrates the
usage of objects and classes in C++:
//The program uses member functions to
input two numbers, add two numbers and
display the result
#include<iostream.h>
class add //Specifies the class
{
private:
int iNum1, iNum2, iNum3; //Member data
public:
void input(int iVar1, int iVar2)
//Member function
{
cout<<”Functions to assign values to
the member data”<<endl;
iNum1=iVar1;
iNum2=iVar2;
}
void sum(void) //Member function
{
cout<<”Functions to find the sum of
two numbers”<<endl;
iNum3=iNum1+iNum2;
}
void disp(void) //Member function
{
cout<<”The sum of the two numbers
is”<<iNum3<<endl;
}
};
void main()
{
add A1;
int iX, iY;
cout<<”Input two numbers”<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
}
The sample output of Program 10.2 is:
Input two number
3
2
Function to assign values to the
member data
Function to find the sum of two
numbers
The sum of the two numbers is 5
Class Specifier
The specifier for a class starts with
the keyword class followed by the
class name. In Program 10.2, the class
specifier is:
Class add
{
private:
int iNum1, iNum2, iNum3; Member data
};
As in the case of a structure, the
body of the class is delimited by
braces and is terminated by a
semicolon.
Defining the Object
In Program 10.2, the statement:
Add A1;
defines an object A1, which is an
instance of a class called add. It is
this definition that actually creates
objects and allocates memory for them.
Access Specifiers
The body of the class contains two
keywords:
• Private
• Public
The keywords private and public are
called access specifiers. The primary
mechanism used to incorporate data
hiding in C++ is by using the private
keyword. Private data or functions can
only be accesses from within the
class. Public data or functions, on
the other hand, are accessible outside
the class. This is shown in Figure
10.4
Therefore, in Program 10.2, it is not
possible to access the member data
iNum1, iNum2 and iNum3 directly from
function main() as they are private o
the add class. This data can be
accessed only from the member
functions of that class.
Usually the data within the class is
declared as private and the functions
are declared as public. Therefore, the
data is hidden and safe from
accidental manipulation, whereas
functions that operate on this data
can be accessed from outside the
class. The outcome of abstracting the
internal implementation of a class
with the use of access specifiers
produces what is known as Abstract
Data Type.
Invoking Member Functions
In Program 10.2, the statements:
Add A1;
A1.input(ix, iy);
A1.sum();
A1.disp();
Are used to invoke the member
functions of the class add :
The dot operator, also called the
class member access operator, is used
to associate the object name with the
member function.
In Program 10.2 the dot operator
associates the object A1 with the
function input(), sum() and disp().
Scope Resolution Operator
In Program 10.2, the member functions
are defined within the class specifier.
These functions can also be defined
outside the boundaries of the class
using the scope resolution
operator(::).
Program 10.2 can be modified using the
scope resolution operator in the
following way:
//This program uses the scope
resolution operator to define the
functions outside the class
#include<iostream.h>
class add
{
private:
int iNum1, iNum, iNum3;
public:
void input(int, int);
void sum(void);
void disp(void);
};
void add::input(int iVar1, int iVar2)
{
iNum1=iVar1;
iNum2=iVar2;
}
void add::sum(void)
{
iNum3=iNum1+iNum2;
}
void add:disp(void)
{
cout<<”The sum of two numbers
is”<<iNum3<<endl;
}
void main()
{
add A1;
int iX, iY;
cout<<”Input two numbers”<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
}
The member functions of the class add
are defined outside the class using
the scope resolution operators (::).
In Program 10.3, the statement:
Void add::input(int iVar1, int iVar2)
uses the scope resolution operator to
define the function outside the
boundary of the class.
In the above statement, void is the
return type of the function, add is
the class name, :: is the scope
resolution operator, input is the
function name, and int iVar1 and int
iVar2 are the function parameters.
The declaration:
Void input(int, int);
in the class informs the compiler that
this function is a member of the class
but will be defined outside the class.
|