Classes In C++

A class represents a group of similar objects. A class bears the same relationship to an object that a type does to a variable. The Class forms the basic for object oriented programming in C++.
 
Any thing you want to encapsulate should be placed with in the class. A class is a user defined data type.
 
Once defined it is used to define objects of that type. A class is template for an object and an object is an instance of a class. When defining a class , we are creating a new abstract data type that can be treated like any other built-in data type.
 
The body of the class is enclosed in the braces and terminated by semi-colon. The class body contains the declaration of variables and functions.
 
These functions and variables are collectively called data members. Their are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public.
 
The keywords private and public are known as visibility levels. Note that these keywords are followed by colons.
 
The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside the class also.
 
The variables declared inside the class are known as data members and the functions are known as member functions.
 
 
Syntax of a class:
 
Keyword class name of class
{
private:
private data;
public
public function;
};//end of class-make a note of the A class is declared by use of the key word class.
 
Program to declare a class:
 
 
//Adding two numbers
// This program can be written easily without using classes but want to illustrate concept of classes here
 
 
Out put of the program
 
 
First of all, the name of the class is add. Under this class we have the three member data: num1, num2, num3. Note that all three are made private.
 
This means that any object that is created under the class add, will have these three member data already in it.
 
Next comes the public part. In public we define any function that we want to make use of. The function definition is done in the usual way. Remember: only functions belonging to the class can access the private members.
 
We have made three functions for a simple operation of adding two numbers. If you go through the statements, you may feel that the same thing could have been put under one function itself.
 
The answer yes. If you want, you do the adding of two numbers and displaying the result within one function itself.
 
The three functions are called member functions, i.e. any object belonging to the class can make use of the member function.
 
The input function which comes under public part is changed as follows:
 
void input ( ) // input is a member function
{
cout<< “Input the two numbers”; cin>>num1>>num2;
}
 
and in the main function you can write:
 
int main( )
 
 {
 
add a1, a2; // Defining two objects a1 and a2 under the same class add
 
a1.input(x,y); // get the values for num1 and num2 for object a1
 
a1.sum( )  
 
a1.display( ); // display sum of numbers entered for a1
 
a2.input(x,y); // get the values for num1 and num2 for object a2
 
a2.sum( )
 
a2.display( ); // display the sum of two numbers entered for a2
 
return 0;
 
}
 
Hence in the result of the above program, you will get two separate answers. One is the result of adding num1 and num2 of a1 and the other is the result of adding num1 and num2 of a2.