|
|
| Classes |
|
A class is a logical method to organize data and functions in a same structure.
They are declared using keyword class, whose functionality is
similar to the one of the C keyword struct, but with the
possibility of including functions as members, moreover than only data.
|
Its Syntax is like this
class class_name // class name should be valid
indentifier
access specifier:
member1;
access specifier:
member2;
.....
}
object_name;
|
| where class_name is a name for the class (user defined type)
and the optional field object_name is one, or several,
valid object identifiers. The body of the declaration can contain members,
that can be either data or function declarations, and optionally access
specifier, that can be any of these three keywords: private:,
public: or protected:. |
| private members of a class are accessible only from other
members of its same class or from its "friend" classes |
| protected members are accessible, in addition to from members
of the same class and friend classes, also from members of its derived
classes |
| Finally, public members are accessible from anywhere where the
class is visible |
| If we donot write any access specifier in class declaration.
It is consider as private |
For Example
class CSquare
{
int x, y;
public:
void set_values (int,int);
int area (void);
}
square; |
| Declares class CSquare and
an object called square of
this class (type). This class contains four members: two variables of type int
(x and y) in the private section
(because private is the default permission) and two functions in the public
section: set_values() and area(), of which we
have only included the prototype.
Notice the difference between class name and object name: In the previous
example, CSquare was the class name (i.e., the user-defined
type), whereas square was
an object of type CSquare. Is the same difference that int
and a have in the following declaration: int a;
int is the class name (type) and a is
the object name
(variable).
On successive instructions in the body of the program we can refer to any of the
public members of the object square
as if they were normal functions or variables, just by putting the object's
name followed by a point and then the class member (like we did with C structs).
For example: square.set_value (3,4);
float myarea = square.area(); but we will not be able
to refer to x nor y
since they are private members of the class and they could only be referred
from other members of that same class. |
// classes example
#include <iostream.h>
class CSquare {
int x, y;
public:
void set_values (int,int);
int area (void) {return (x*y);}
};
void CSquare::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CSquare square;
square.set_values (3,4);
cout << "area: " << square.area();
}
|
|
area: 12
|
The new thing in this code is the operator :: of scope included
in the definition of set_values(). It is used to declare a
member of a class outside it. Notice that we have defined the behavior of
function area() within the definition of the CSquare
class - given its extreme simplicity. Whereas set_values() has
only its protype declared within the class but its definition is outside. In
this outside declaration we must use the operator of scope ::
.
The scope operator (::) allows to specify the class to which the
member being declared belongs to, granting exactly the same scope properties as
if it was directly defined within the class. For example, in the function set_values()
of the previous code, we have resorted to the variables x and y,
that are members of class CSquare and that are only visible
inside it and its members (since they are private
).
The only difference between defining a class member function completely within
its class and to include only the prototype, is that in the first case the
function will automatically be considered inline
by the compiler, while in the second it will be a normal (not-inline) class
member function.
The reason why we have made x and y private
members (remember that if nothing else is said all members of a class defined
with keyword class have private access) it is because we
have already defined a function to introduce those values in the object (set_values()
) and therefore the rest of the program does not have why to access directly to
them. Perhaps in a so simple example as this one you do not see a great utility
protecting those two variables, but in greater projects it may be very
important that values cannot be modified in an unexpected way (unexpected way,
from the point of view of the object).
One of the greater advantages of classes is that we can declare several
different objects from it. For example, following with the previous example of
class CSquare, we could have declared the object square1
in addition to the object square :
|
// class example
#include <iostream.h>
class CSquare {
int x, y;
public:
void set_values (int,int);
int area (void) {return (x*y);}
};
void CSquare::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CSquare square, square1;
square.set_values (3,4);
square1.set_values (5,6);
cout << "square area: " << square.area() << endl;
cout << "square1 area: " << square1.area() << endl;
}
|
sqaure area: 12
sqaure1 area: 30
|
|
|
From the output of this code you will see that the output of square1.area()
is different than of square1.area(). To explain it somehow,
Each object of class CSquare has
its own variables x and y, and its own
functions set_value() and area().
On that is based the concept of object and object-oriented
programming
. In that data and functions are properties of the object, instead of the usual
view of objects as function parameters in structured programming. In this and
the following sections we will discuss advantages of this methodology.
In this concrete case, the class (type of object) to which we were talking about
is CSquare, of which there are two instances, or objects: square
and sqaure1, each one with its
own member variables and member functions
Download Example of Classes: Example1
|
|
|