C++ Programming & Tutorials for Beginners

Classes & Objects

Home  »  C++ Programming  »  Classes & Objects


     A Class is that which contains the set of properties and Methods of an object in a single unit Like Animals is name of class which contains all the Properties and Methods of an Object of another Animals So if we wants to access any data from the Class then first we have to create the Object of a class Then we can use any data and method from class

     An Object is a Real Word Thing Which performs a Specific Task and which has a set of Properties and Methods. Properties of Object are Also Called as Attributes of an Object and Method is also Known as the Function of the Object Like What an Object can do For Example A Car is an Object which has a Certain Number of Properties Like Color, Model No, Average etc and a Function Known as Run When A User gives Some Race then the Car Will be Moved.

Contents

Class Definition

     A Class is a group of objects that have the same properties, common behaviour and common relationship.

The general format of a class definition is given below:



  In above given definition of class, the keyword "class" specifies the start of the class definition.
The class-name is the user define name of the class. It can also be called as the tag name of the class that acts as a type specifier for it.
The braces { and } surrounds the body of the class.
The body of the class contains the declaration of various class members. These class members may be data, functions, classes, enumerations, bit fields, friends or data type names.

There are three types of members in a class : public, private and protected.

The Private data memerbs can be accessed only from within the class i.e. by the functions declared inside the classes only. In others words, the private data of the class can be accessed only by the member functions of the class.

The Public data members can be accessed both inside and outside the class i.e. by the functions declared in the main() program also.

The Protected data members are the members that can be used only by the member functions or friend functions of the class. The protected members are same as that to private members in the sense that they cannot be accessed by the non-member functions of the class.

A simple example of class definition is given below:



Class Variable/Object Declaration

     In C++ to make the use of any specified class, the variables of that class type have to be declared. These variables are known as the objects of the class. The declaration of any class variables object is similar to that of a variable of any basic type. The objects of the class are declared after the end of class declaration before the main() function or in the main() function. The general format of the object declaration is:

class user_define_name
{
  ---------------------
  ---------------------
  class members
  ---------------------
  ---------------------
};
class user_define_name object1,object2,.......objectN;

where, ther user_define_name represents the name of the class and object1......objectN are the names of the objects.
For example, let us consider a class as given below:



Scope of a Class Object

A class object is like any other data type with respect to scope. The object comes into scope when the program defines it , and it goes out of scope when the program exits the block in which class objects is defined.

Nesting of Member Function

Generally we are calling the function through the object of the class i.e. by an object of that class using a dot operator (.). However, there is an exception to this. A member function can be called by using its name inside the another member function of the same class. This is know as nesting of member function.







Private member function

Generally we declare the data in the private section of the class to hide the data from outside function. We can also able to declare the functions in the private section of the class. We can access private function through the public methods of the class. The private member function is not even accessible through the object of the class using dot (.) operator.


class example
{   int a;
  void input();     //private member function
public :
  void display();
};

if obj is an object of example, then
obj.input();

will not work as we are not able to access the private member function of the class directly. But the function input() can be called by the function display() to read the private value of a.

void example :: display()
{
  input();
}

Class Scope

   Each class introduces its own scope called class scope. All the members declared inside a class have existance in that class scope. The class scope starts at the left brace that follows the class head and ends with the corresponding right brace for example.



  The members declared in the class scope can be accessed by using the dot & operation & the scope resolution operators (::).



Local & Global class

   Class can be local or global within the program depends on where we declare the class in the program. If we declare the class within the main() program then it is called as Local class.

Exampl Local Class
void main()
{
  class example
  {
    private:
      int a,b,c;
    public:
      void getdata();
      void showdata();
  }obj;
  obj.getdata();
  obj.showdata();
}


In the above program code class example is Local to main() program
If we declare the class outside the main() program then it is called as Global class.

Example Global Class
class example
{
  private:
    int a,b,c;
  public:
    void getdata();
    void showdata();
};

void main()
{
  example obj;
  obj.getdata();
  obj.showdata();
}




Previous
Next Post »