Scope Resolution :: Operator in C++


The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program. The operator is represented as the double colon ( : : ) symbol.

The program defines a class named student which has private data members name, m1, m2, m3, tot and avg, and two public member functions getData() and display(). The getData() function prompts the user to enter the student's name and three marks using cin and stores them in the private data members name, m1, m2 and m3.

The display() function calculates the total marks and average marks of the student using the entered marks and the private data members tot and avg. It then outputs the student's name, the entered marks, the total marks, and the average marks to the standard output using cout. The :: scope resolution operator is used to define the display() function outside the student class.

In the main() function, an object of the student class is created using the default constructor. Then, the getData() member function of the student class is called on the object to prompt the user to enter the student's name and marks. The display() member function of the student class is then called on the object to display the entered marks, total marks and average marks of the student.

Overall, this program provides an example of using a class definition in C++ with private data members and member functions. The program demonstrates the use of the :: scope resolution operator to define a member function outside the class definition. It also shows how encapsulation of data and member functions within a class can be used to organize and manipulate data related to a student object.

Source Code

#include<iostream>
using namespace std;
// :: Scope Resolution Operator in C++
class student
{
private:
    string name;
    int m1,m2,m3,tot;
    float avg;
public:
    void getData()
    {
        cout<<"Enter The Name : ";
        cin>>name;
        cout<<"Enter 3 Marks:"<<endl;
        cin>>m1>>m2>>m3;
    }
    void display();
};
 
void student::display()
{
    tot=m1+m2+m3;
    avg=tot/3.0;
    cout<<"Name    : "<<name<<endl;
    cout<<"M1      : "<<m1<<endl;
    cout<<"M2      : "<<m2<<endl;
    cout<<"M3      : "<<m3<<endl;
    cout<<"Total   : "<<tot<<endl;
    cout<<"Average : "<<avg<<endl;
}
 
int main()
{
    student o;
    o.getData();
    o.display();
    return 0;
}
 
 

Output

Enter The Name : Pooja
Enter 3 Marks:
88
89
79
Name    : Pooja
M1      : 88
M2      : 89
M3      : 79
Total   : 256
Average : 85.3333
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples