CLASS
A Class is a specially introduced data type in C++ that slight difference with structures. The only difference between a structure and a class in C++ is that, by default, the members of a class are private while the members of a structure are public.
Class allows the data and functions to be hidden. The class specification has two parts:
- class declaration
- class function definition
The class declaration describes the type and scope of its members. The class function definitions describes how the class functions are implemented.
The general form of Class declaration is:
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
}
The keywords private and public are known as visibility labels. The members that have been declared as private can be accessed only from within the class. Public members can be accessed from outside the class also.
Only the member functions can have access to the private data members and private functions.
The binding of data and functions together into a single class-type variable is referred to as encapsulation.
Example:
class item
{
int number;
float cost;
public:
void getdata(int a,float b); //function declaration
void putdata(void); //function declaration
}
OBJECTS:
The class variables are known as Objects.
Declaration of class item as shown above does not define any objects of item. We can create variables of that type by using the class name like any other built-in data type.
example: item x; //creates a variable x of type item
Objects can also be created along with declaration of class like below:
class item
{
………….
………….
………….
} x,y,z;
Accessing Members of Class:
The following is the format of calling a member function:
object-name.function-name(actual-arguments);