Saturday, April 19, 2025

C++ OO Scope Resolution Operator

In C++, the scope resolution operator (::) is used to access the members of a class that are defined outside of the class definition or to access a member that is hidden by a local variable or parameter.

The scope resolution operator is placed before the name of the member that we want to access and the name of the class or namespace that the member belongs to. 


#include <iostream>

using namespace std;

class Major{
    public:
        string someStr = "Some Stuff";

        void printStr();
};

void Major :: printStr() {
    cout << someStr << endl;
}

int main() {

    Major Object;
    Object.printStr();

    return 0;
}

This code defines a class called Major which has a public member variable someStr of type string initialized to the value "Some Stuff", and a public member function called printStr().

The interesting part of the code is how the member function is defined outside the class using the scope resolution operator (::). This is called defining the function out of line or implementing the function.

In the main function, an object of the Major class is created and its printStr() method is called to print the value of the member variable someStr


#include <iostream>

using namespace std;

class Numbers{
    public:
        int Num = 5;

        void doubleNum();
};

void Numbers :: doubleNum() {
    cout << Num * Num << endl;
}

int main() {

    Numbers Object;
    Object.doubleNum();

    return 0;
}

This code defines a class named Numbers, which has one public data member called Num and one public member function called doubleNum().

The Num data member is initialized to 5. The doubleNum() member function multiplies Num by itself and prints the result to the console.

In the main() function, an object of the Numbers class is created called Object, and the doubleNum() member function of the object is called. This prints the result of Num squared, which is 25, to the console. 


#include <iostream>

using namespace std;

class Numbers{
    public:
        int Num = 5;

        void doubleNum();
};

void Numbers :: doubleNum() {
    cout << (Numbers :: Num) * (Numbers :: Num) << endl;
}

int main() {

    Numbers Object;
    Object.doubleNum();

    return 0;
}

In this code, the doubleNum function of the Numbers class is defined to print the square of the Num member variable. The scope resolution operator :: is used to explicitly refer to the Num variable of the Numbers class within the function definition.

Since Num is a member variable of the Numbers class, it is necessary to use the scope resolution operator to indicate that we are referring to the Num variable of the Numbers class, not some other variable with the same name that might be in a different scope. In this case, the parentheses around Numbers :: Num are not necessary since the multiplication operator has higher precedence than the scope resolution operator, but they do not change the behavior of the code.

When the program is run, an object of the Numbers class is created and the doubleNum function is called on it, resulting in the output of 25, which is the square of the initial value of Num, which is 5.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...