Dremendo Tag Line

Instance Methods in C++ Programming

OOPs in C++

In this lesson, we will understand what is Instance Method in C++ Programming and how to create them along with some examples.

What are Instance Methods in C++?

Instance methods are used to store or process data stored in instance variables and are used only by the object of the class.

Note: When a function is declared in a class, it is called Method.

video-poster

Example

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

using namespace std;

class record
{
  private:
    int rollno;
    char name[30];
    int age;

  public:
    // Creating an instance method to store data in private instance variables
    void inputdata()
    {
        cout<<"Roll No: ";
        cin>>rollno;
        fflush(stdin);		// clear the input buffer
        cout<<"Name: ";
        cin.get(name,30);
        cout<<"Age: ";
        cin>>age;
    }

    // Creating an instance method to display data of instance variables
    void displydata()
    {
        cout<<"Roll No: "<<rollno<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Age: "<<age;
    }
};

int main()
{
    record x;
    x.inputdata();
    x.displydata();
    return 0;
}

Output

Roll No: 1
Name: Martin
Age: 15
Roll No: 1
Name: Martin
Age: 15

In the above example, we have declared the instance variables private because we do not want to store values in instance variables from outside the class.

We have declared public methods to store and display data on the output screen. We will use these public methods using the object of the class to store and process data stored in instance variables. In this way, we can hide our instance variables from accessing outside the class.

Let's take another example that can be coded as class and object in C++ using instance variable and instance methods.

Suppose we want to store the details of a student like a roll no, name, class, marks obtained in three different subjects (English, Maths, Computer), total and percentage obtained.

For the above question, we will create a class called Student with the following instance variables:

  • roll: for storing roll no of the student.
  • name: for storing name of the student.
  • cls: for storing class of the student.
  • eng: for storing marks obtained in english.
  • math: for storing marks obtained in math.
  • comp: for storing marks obtained in computer.
  • total: for storing total marks obtained.
  • per: for storing total percentage obtained.

We will also create three instance methods inside the Student class for processing the instance variables, and they are:

  • inputdetails(): for storing information in the instance variables.
  • calculate() for calculating and storing the total and percentage obtained.
  • display(): for displaying the information stored in the instance variables on the screen.

Example

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

using namespace std;

class student
{
  private:
    int rollno;
    char name[30];
    int Class;
    int eng;
    int math;
    int comp;
    int total;
    float per;

  public:
    // Creating an instance method to store data in private instance variables
    void inputdata()
    {
        cout<<"Roll No: ";
        cin>>rollno;
        fflush(stdin);		// clear the input buffer
        cout<<"Name: ";
        cin.get(name,30);
        cout<<"Class: ";
        cin>>Class;
        cout<<"English: ";
        cin>>eng;
        cout<<"Math: ";
        cin>>math;
        cout<<"Computer: ";
        cin>>comp;
    }

    // Creating an instance method to calculate and store total and percentage
    void calculate()
    {
        total=eng+math+comp;
        per=(total/300.0)*100;
    }

    // Creating an instance method to display data of instance variables
    void displydata()
    {
        cout<<"Roll No: "<<rollno<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Class: "<<Class<<endl;
        cout<<"English: "<<eng<<endl;
        cout<<"Math: "<<math<<endl;
        cout<<"Computer: "<<comp<<endl;
        cout<<"Total Marks: "<<total<<endl;
        cout<<"Percentage: "<<per<<endl;
    }
};

int main()
{
    student x;
    x.inputdata();
    x.calculate();
    x.displydata();
    return 0;
}

Output

Roll No: 1
Name: Martin
Class: 5
English: 78
Math: 84
Computer: 91
Roll No: 1
Name: Martin
Class: 5
English: 78
Math: 84
Computer: 91
Total Marks: 253
Percentage: 84.3333 

Define Instance Method outside Class

To define an instance method outside its class, first, we must declare the instance method without a body inside its class with a semicolon at the end of its declaration. After that, we can define the body of the instance method outside the class using the scope resolution operator. See the example given below.

Example

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

using namespace std;

class record
{
  private:
    int rollno;
    char name[30];
    int age;

  public:
    // Declare instance methods inside the class without body
    void inputdata();
    void displydata();
};

// Define instance method body outside the class using scope resolution operator
void record::inputdata()
{
    cout<<"Roll No: ";
    cin>>rollno;
    fflush(stdin);		// clear the input buffer
    cout<<"Name: ";
    cin.get(name,30);
    cout<<"Age: ";
    cin>>age;
}

void record::displydata()
{
    cout<<"Roll No: "<<rollno<<endl;
    cout<<"Name: "<<name<<endl;
    cout<<"Age: "<<age;
}

int main()
{
    record x;
    x.inputdata();
    x.displydata();
    return 0;
}

Output

Roll No: 1
Name: Peter
Age: 15
Roll No: 1
Name: Peter
Age: 15

Array of Objects

Suppose we want to store the records of 10 students. In this situation, we will create a class and 10 objects of that class to store individual student records in individual objects. But that will be a lengthy process. To make it short, we will create an array of objects so that each array element will become an individual object. We can easily access any object of the object array using its index value. See the example given below.

Example

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

using namespace std;

class student
{
  private:
    int rollno;
    char name[30];
    int Class;
    int eng;
    int math;
    int comp;
    int total;
    float per;

  public:
    // Creating an instance method to store data in private instance variables
    void inputdata()
    {
        cout<<"Roll No: ";
        cin>>rollno;
        fflush(stdin);		// clear the input buffer
        cout<<"Name: ";
        cin.get(name,30);
        cout<<"Class: ";
        cin>>Class;
        cout<<"English: ";
        cin>>eng;
        cout<<"Math: ";
        cin>>math;
        cout<<"Computer: ";
        cin>>comp;
    }

    // Creating an instance method to calculate and store total and percentage
    void calculate()
    {
        total=eng+math+comp;
        per=(total/300.0)*100;
    }

    // Creating an instance method to display data of instance variables
    void displydata()
    {
        cout<<"Roll No: "<<rollno<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Class: "<<Class<<endl;
        cout<<"English: "<<eng<<endl;
        cout<<"Math: "<<math<<endl;
        cout<<"Computer: "<<comp<<endl;
        cout<<"Total Marks: "<<total<<endl;
        cout<<"Percentage: "<<per<<endl;
    }
};

int main()
{
    student x[10];	// Create an object array
    int i;

    // Store information in individual objects
    for(i=0; i<10; i++)
    {
        x[i].inputdata();
        x[i].calculate();
        cout<<endl;		// print a blank line
    }

    // Display information of each object
    for(i=0; i<10; i++)
    {
        x[i].displydata();
        cout<<endl;		// print a blank line
    }
    return 0;
}