class and object

In this page we will discuss about following;

 

class

as already mentioned in the OOPs page. class is a, collection of data members and function members. These members of the class are declared in some access specifier or mode in the class.

Members of a class are made accessible using the access specifier, so that a class can be implemented.

Thus by defining access mode in a class, the syntex of a class would be as follows –

SYNTEX:

class class_name
{
  private:
    data member;
    member function;
  public:
    data member;
    member function;
};

Here, data member representing the data type (int, char, float) while member function is representing the function. It has a class-name an identifier i.e. any appropriate name (except reserved keyword).

before starting programming with class, we will discuss about these access specifier or visibility-mode.

 

Type of access mode/access specifier

  • private
  • public
  • protected

 

private mode/private member

When a member of a class is declared in private mode, it can only be accessed by other members of the same class. i.e.They cannot be accessed from outside the class. All members of a class are by default private i.e. private keyword is an optional.

If no mode is defined in a class, then all the members of the class will be private. means 

 

public mode/public member

In this mode, the declared data members and function members can be accessed outside the class from its object (class-variable).Because all members of a class are default private. Therefore, we use public keywords to make members public.

apart from two access modes, a third access mode is also found in the class which is used to inherit the property and behavior of a class in another class. This is called protected mode.

 

protected mode/protected member

This mode of declaration of members is the same as private, that is, the members declaring in both modes can not be accessed from the class object.

Protected members can be accessed by inheriting another class (feature of inheritance) while private-members can access only members of the same class.

we use the protected keyword to protect members of a class.

We will read about protected mode in inheritance. Here we will discuss private and public members.

Thus a class cannot implement a class without defining access mode, i.e. using or not treating the class as both are equal.

 

Defining class members

The private member of a class is defined within the class itself, whereas the public member can be defined in two ways –

  • inside class (inline definition)
  • outside class (outline definition)

 

public member definition inside the class (inline definition)

In this, without giving the function-declaration of members, direct given its definition inside the class.

SYNTEX:

class class_name
{
    private-member;
    ..........
   public:

   return_type function_name()
   {
     body of function;
   }
    ...........
};

 

public member definition outside the class (outline definition)

In this, the declaration of function-members is inside the class while their definition is defined outside the class by scope resolution operator.

SYNTEX:

class class_name
{      
   Private-member;
   ..............
   public:
   return_type function_name();
    ..........;
};

class_name :: return_type function_name()
  {
     body of function;
  }

Here, :: is a scope resolution operator. Which we use for outside definition of public members of a class.

In the next program, we have defined the definition of function-member using both methods(inside-definition and outside-definition). Both of these examples are given in the below accessing class member using their object.

Declaration of an object

Just as the member of the structure is accessed from the variable of the structure, the member of the class is accessed from the class variable or object as well.
An object is an identifier of any appropriate name (except reserved keyword). A class can have more than one object but with a different name.

SYNTEX

class_name object;

or

class-name object1, object2,.....n;

or

class class-name
{
    data-member;
   public:
    data-member;
   member-function;
} object1, object2, …..n;

Example:-

class Student
{
     int roll; char name[10];
   public:
     get_record();
     show_record();
}x,y;

Here, x and y are object of student class.

accessing class member using their object

only public members of the class can be accessed. Hence-

using first object x,

x.get_record();
x.show_record();

using second object y,

y.get_record()
y.show_record();

You can understand this from the program given below-

In the program, we have done two public function-member declarations. The first public-member get_record (); definition of is given inside the class while the second public-member show_record (); definition is given outside the class (by scope resolution operator).

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class student
{
     // public member by default
         int roll_no;
         char name[20];

      public:       // public member declaration
          void show_record(void);          // function declaration
          void get_record()             // function defination inside class (inline definition)
          {
               cout<<"Enter roll no: ";
               cin>>roll_no;
               cout<<"Enter Name   : ";
               gets(name);
          }
};

         void student::show_record()    // function defination outside class (outline definition)
         {
               cout<<"\nRoll no: "<<roll_no;
               cout<<"\nName   : "<<name;
          }

 student x,y;     // object declaration

 void main()    // main function
 {
        clrscr();
        x.get_record();   //first object's function call
        y.get_record();   //second object's first function call

        x.show_record();  //first object's second function call
        y.show_record();  //second object's second function call
        getch();
 }
OUTPUT:-
Enter roll no: 11
Enter Name : Rahul sherma

Roll no: 11
Name : Rahul sherma

as you can see, we have stored the record of two students from this program.

As mentioned earlier, the concept of class in C++ is the  updated version of the structure concept of C. But How?,

Difference between class and structure

All members of the structure are declared publicly by default, whereas members of a class by default are private.

All member of structure is accessed by structure-variable whereas only public member in class can be accessed by class-variable.

Class OOPs are the feature while the structure is taken from the process oriented programming (POPs).


Similarities between class and structure

Like structure, class name is also an identifier ie any unique name.

both have members, data type and function type.

both do not reserve space in memory until their variable or object declare.

just as a member of a structure is accessed by a structure-variable, the class member (public member) is accessed from the class-variable or object.

members of both class and structure are declared within a bracket and terminated with bracket semicolon.

more about class and object,


previous-Object Oriented Programming in C++

next- Friend function | Friend Class