How To Define A Method Of Class Outside Of That Class | Scope Resolution Operator In C++

 Scope Resolution Operator In C++



Scope Resolution Operator - 

In computer programming, the scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace.


#include <iostream>
using namespace std;

class calculator
{
    public:
    float num1,num2;
    void getnum()
    {
        cout<<"Enter First Number"<<endl;
        cin>>num1;
        cout<<"Enter Second Number"<<endl;
        cin>>num2;
    }

    float add(); //Defining Function Inside Class
    float sub();
    float mul();
    float div();

};


//Writing Function Outside The Class
//Adding Reference Of Inside Function
//Using Scope Resolution Operator - ::
float calculator::add() 
{
    return num1 + num2;
}
float calculator::sub()
{
    return num1 - num2;
}
float calculator::mul()
{
    return num1*num2;
}
float calculator::div()
{
    return num1/num2;
}



int main()
{
    calculator c1;
    c1.getnum();
    cout<<"Addition is: "<<c1.add()<<endl;
    cout<<"Subtraction is: "<<c1.sub()<<endl;
    cout<<"Multiplication is: "<<c1.mul()<<endl;
    cout<<"Division is: "<<c1.div()<<endl;
    return 0;
}

Comments

Popular posts from this blog

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example

Pointers In C++ | Pointer Explained With Real Life Examples | Pointers In OOPs C++

Destructor In C++ | Destructor Explained With Real Life Examples