(This question has been asked in GU Uni Dec 2018 for 7 Mark)


With the help of the Scope Resolution Operator, we can define the public member function outside of the class body.

To declare member function body outside of the class we have to take care of the below things.

  1. We need to declare the function in the class. We can use this by including a function signature with public access modifier.
    1. Say we have class called Student and we have member function namely setDetail and getDetail. We want to define this member function outside fo the class. In this case, we have to declare the function in the class using the below signatures.
    2. class Student{
    3. void getDetail();
    4. void setDetails(int, int);
    1. }
  2. We can can define function outside of the class using Scope resolution operator preceded by the class name as below
    1. void Student::getDetail() { function body }
    2. void Student::setDetails(int marks, int rollnum){ function body }

Below is the Example for the same.

#include <iostream>
using namespace std; 

class Student
    int sem;
    int rollno;

    void setData(int , int ); //function signature
    void  getData(); 

void Student::setData(int psem, int prollno)   //function header
    sem = psem;
	rollno =	 prollno;

void Student:: getData()         //function header
	cout<<"sem "<<sem<<endl;
	cout<<"Roll no"<<rollno<<endl;

int  main()
	Student  pintu;
	Student ravi;
	cout<< "Pintu's Details"<<endl;
    return 0; 


Pintu's Details
sem 3
Roll no41
How to define the Function outside the class body? Explain with an example.

Leave a Reply

Your email address will not be published. Required fields are marked *