Class Definition in C++




Class definition in C++:



The following is the general format of defining a class template:


Usage of Private and Public

// Usage of Private:
Class class-name
{
Private:
     datatype var1;
     datatype var2;
- - - - - - - - - -
- - - - - - - - - -
Return type function_name(Arguments)
{
  - - - - - - - - - -
  - - - - - - - - - -
}
{
  - - - - - - - - - -
  - - - - - - - - - -
}

// Usage of Public:
Public:
Data members;
  - - - - - - - - - - -
  - - - - - - - - - - -
Member function
  - - - - - - - - - - -
  - - - - - - - - - - -
};

Main()
{
  - - - - - - - - - - -
  - - - - - - - - - - -
}

The keyword class is used to define a class template.
Private and Public section of a class are given by keyword ‘private’ and ‘public’ respectively. They determine the accessibility of the member.

All the variables declared in the class, whether in the private or the public section, are the members of the class.
Since the class scope is private by default, you can omit the keyword private. In such cases you must declare the variable before public, as writing public overrides the private scope of the class.


Private and Public Syntax for reference:

    Class tag_name
   {
    type member_variable_name;

private:
     - - - - - - - - - - - - - -
     - - - - - - - - - - - - - -

type member_function_name();
 - - - - - - - - - - - - - -
 - - - - - - - - - - - - - -

Public:
      type member_variable_name;
       - - - - - - - - - - - - - -
       - - - - - - - - - - - - - -

     Type member_function_name();
       - - - - - - - - - - - - - -
       - - - - - - - - - - - - - -
    };


Variables and functions from the public section are accessible to any function of the program.
However, a program can access the private members of a class only by using the public member functions of the class.
This insulation of data members from direct access in a program is called “Information  hiding”.



Example:
  
 class player
 {
  public:
          void getstats(void);
          void showstats(void);
          int no_player;

 private:
          char name[40];
          int age;
          int runs;
          int tests;
          float average;
          float calcaverage(void);
};


This above example models  cricket player. The variable  in the private section- name, age, runs, highest, and average –can be accessed only by member function of the class calcaverage(), getstats() and showstats(). The function in the public section- getstats() and showstats() can be called from the program directly, but function calcaverage() can be called from the member functions of the class-getstats() and showstats().




Working Example:

Class check                           // name of class is check
{
  Public:
          Check add(check);
          void get()
          {
             cin>>a;
          }
          void put()
          {
             Cout<<a;
          }
Private:
          int a;
};
Void main()                           //main function
{
   check c1,c2,c3;
   c1.get();
   c2.get();
   c3=c1.add(c2);
  c3.put();
}

Check check::add(check c2)      //Addition
{
  check temp;
  temp.a =a+c2.a;
  return (temp);                         //returning the temp value
}



The above example creates three objects of class check. It adds the member variable of  two classes, the invoking class c1 and the object that is passed to the function, c2 and returns to another object c3.

You can notice that the class add() the variable of the object c1 is just referred as ‘a’ where as the members of the object passed i.e. c2 is referred as ‘c2.a’

This is because the member function will be pointed by the pointer named this in the compiler where as what we pass should be accessed by the extraction operator ‘.’. We may pass more than one object and also normal variable. We can return an object or a normal variable from the function. We have made use of a temporary object in the function add() in order to facilitate return of the object
 

No comments:

Powered by Blogger.