Search This Blog

Wednesday, July 28, 2010

Classes

In C++, a class is declared using the class keyword. The syntax of a class declaration is similar to that of a structure. Its general form is,
class class-name {
// private functions and variables
public:
// public functions and variables
} object-list;
In a class declaration the object-list is optional.
The class-name is technically optional. From a practical point of view it is virtually always needed. The reason is that the class-name becomes a new type name that is used to declare objects of the class.
Functions and variables declared inside the class declaration are said to be members of the class.
By default, all member functions and variables are private to that class. This means that they are accessible by other members of that class.
To declare public class members, the public keyword is used, followed by a colon. All functions and variables declared after the public specifier are accessible both by other members of the class and by any part of the program that contains the class.
#include < iostream >
using namespace std;
// class declaration
class myclass {
// private members to myclass
int a;
public:
// public members to myclass
void set_a(int num);
int get_a( );
};
This class has one private variable, called a, and two public functions set_a( ) and get_a( ). Notice that the functions are declared within a class using their prototype forms. The functions that are declared to be part of a class are called member functions.

Since a is private it is not accessible by any code outside myclass. However since set_a( ) and get_a( ) are member of myclass, they have access to a and as they are declared as public member of myclass, they can be called by any part of the program that contains myclass.
The member functions need to be defined. You do this by preceding the function name with the class name followed by two colons (:: are called scope resolution operator). For example, after the class declaration, you can declare the member function as
// member functions declaration
void myclass::set_a(int num) {
a=num;
}
int myclass::get_a( ) {
return a;
}
In general to declare a member function, you use this form:
return-type class-name::func-name(parameter- list)
{
// body of function
}
Here the class-name is the name of the class to which the function belongs.
The declaration of a class does not define any objects of the type myclass. It only defines the type of object that will be created when one is actually declared. To create an object, use the class name as type specifier. For example,
// from previous examples
void main( ) {
myclass ob1, ob2;//these are object of type myclass
// ... program code
}
Remember that an object declaration creates a physical entity of that type. That is, an object occupies memory space, but a type definition does not.
Once an object of a class has been created, your program can reference its public members by using the dot operator in much the same way that structure members are accessed. Assuming the preceding object declaration, here some examples,

ob1.set_a(10); // set ob1’s version of a to 10
ob2.set_a(99); // set ob2’s version of a to 99
cout << ob1.get_a( ); << "\n";
cout << ob2.get_a( ); << "\n";
ob1.a=20; // error cannot access private member
ob2.a=80; // by non-member functions.
...
There can be public variables, for example
#include < iostream >
using namespace std;
// class declaration
class myclass {
public:
int a; //a is now public
// and there is no need for set_a( ), get_a( )
};
int main( ) {
myclass ob1, ob2;
// here a is accessed directly
ob1.a = 10;
ob2.a = 99;
cout << ob1.a << "\n";
cout << ob1.a << "\n";
return 0;
}
It is important to remember that although all objects of a class share their functions, each object creates and maintains its own data.

No comments:

Post a Comment