C++ Scope Resolution Operator (::) Example: Defining Class Methods Outside the Class
CODE:
#include<iostream>
using namespace std;
class student
{
private:
int id;
public:
void setid(int i)
{
id=i;
}
int getid();
};
int student::getid()
{
cout<<"The ID of Student is : "<<id<<endl;
}
int main()
{
student s;
s.setid(24);
s.getid();
}
Explanation:
Clean Class: Class ke andar sirf function ka naam (getid();) likha gaya hai, jis se class choti aur saaf nazar aati hai.
Scope Resolution Operator (::): Ye operator Google ko batata hai ke getid() function darasal student class ka hissa hai, bhale hi wo class ke bahar likha ho.
Syntax: Iska tareeqa ye hota hai: Return_Type Class_Name :: Function_Name().
Encapsulation: Is mein bhi id private hai, yani data hiding ka concept barkarar hai.
Output:
The ID of Student is : 24
Post a Comment