web123456

C++ set function and get function exploration

// Ask to learn programming #include<iostream> using namespace std; class Point { public: Point()// Constructor { x=0; y=0; } void setX(float _x){x=_x;} //(1); void setY(float _y){y=_y;} float getX(){return x;} //cout<<()<<endl; float getY(){return y;} void printPoint(){cout<<"("<<x<<","<<y<<")"<<endl;} private: float x; float y; }; int main() { Point p1;// Declare a Point object p1. p1.printPoint();//(0,0) //=1; Error! // Private members of a class cannot be modified directly outside the class. p1.setX(1); // Our private member values can only be modified by the public member functions provided in the class. //cout<<<<<endl; error! // Private members of a class cannot be accessed directly from outside the class. cout<<p1.getX()<<endl; // Our private member values can only be accessed through the public member functions provided in the class. p1.printPoint(); return 0; }