Specifying a Class:
As discussed a class is defined to develop an algorithm and bind it together in a core shell.
A class is an abstract data type (ADT). The binding of data members and members function in a class is called encapsulation. The class is declared as shown.
class ece
{ private:
int x, float y;
void f_priv(int a, int b);
z =f_over(x,y); // The value of z is 0.5
public:
int r, float s;
void f_pub(float t, float u);
}
In the above example a class is defined and it is named ece. The class ece has public and private variables and function prototypes. The most striking feature of class is variables can be defined as public or private.
The variables inside the class are called data members and the functions are called
member functions.
Basic rules of working with class:
- Only the public members can access the private members of the same class directly without using any operators like (.).
- The public members can be accessed outside the class using scope operators.
- Member function can de defined inside the class without having the prototype, this style of creating a member function is similar to inline function and it has to follow the same rules of inline function. The key word inline is not necessary.
- Member function can also be created outside the class by declaring the function prototype inside the class. The prototype name must be identical to the function definition name.
- If the members of class are not defined with key word public or private than the members are defaulted as private member and thus all the members are hidden.
- Private members cannot be accessed outside the class. Therefore the range and visibility of private member is within its class.
think about the following example to explain further about class.
class item
{ int number;
float cost;
public:
int z;
void getdata(int a, float b);
void putdata(void);
};
In the above example the member data (variable) number and cost will be private member because default scope of member is private. Where as member functions getdata and putdata are public members. The function is declared as prototype. Only these functions can access the private member data cost and number.
A class item has been defined. Now any number of objects of class item can be used with simple statement.
item x,y,z; This statement will create three fresh objects of class item. In other word object is a variable of type class. When a class is defined memory is not allocated, but when an object is created required memory is allocated for that object. Object can be declared at the definition of class but this method is not followed because objects are produced only at the time of necessity.
class item
{
float cost;
int number; public:
int z;
void getdata(int a, float b);
void putdata(void);
}x,y,z;
How to access the class members:
As discussed earlier when the class are used in the main( ) part of the program only
public members can be accessed the private members cannot be read even through any operator. It is hidden for ever and it is exclusively only by its different class members either private of public. On other hand the public members can be used outside the class.
The syntax of using the public data member and member function are: object_name.data_memeber; This is for public data member which is z. object_name.member_function (argument); This is for public member function which
are getdata and putdata.
By using above syntax the members can be accessed as follows:
item p;
p.z =10; The public data member z is assigned 10. p.getdata(10,11.22);
p.putdata( );
To hide the data usually the data member is not declared as a public member; on top of example z is public data member. This kind of declaration of data member as public should be avoided and the follow the method given below.
class item
{ int number; float cost; public:
void getdata(int a, float b);
void putdata(void);
}x,y,z;
Defining Member functions:
A member function can be declared inside the class definition or outside the class definition.
Outside the Class Definition:
A member function can be defined outside the class. Before defining the member
function outside the class a prototype of that member function should be declared in the class. There is a little difference between when defining the member function outside the class from regular function definition. That is when declaring a member function outside the class the function must recognize for which class is this member function is defined. There fore the syntax for defining the member function outside the class is;
return_type class_name : : function_name (argument)
{ statements;
}
Consider the following class:
class item
{ int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
The class item has public member function prototype getdata and putdata. This member functions can be defined outside the class as shown.
void item : : getdata(int a, float b)
{ number = a;
cost = b;
}
void item : : putdata(void)
{ cout <<"Number is " <
cout << "Cost is " << cost << "\n";
}
As discussed the public member can access the private member only of that class directly
without dot operator. In the above example the member functions are accessing the private data members' number and cost without referring to the class. A private member cannot be accessed outside the class. A similarly the member function can call the other member function of same class without any operator this is also called nesting of member functions.
Inside the Class Definition:
The member function can be defined inside the definition of class. When a member function is defined inside the class it has all the properties of inline function, in other word it is also an inline function.
class item
{ int number; float cost; public:
void getdata(int a, float b);
void putdata(void)
{ cout <<"Number is " <
cout << "Cost is " << cost << "\n";
}
};
Inline function Outside the class definition:
inline void item : : getdata(int a, float b)
{ number = a;
cost = b;
}
Therefore an inline function can be created even outside the class definition. Therefore it is always a good practice as a programmer to define function outside the class definition.