Home »
C++ programs »
C++ class and object programs
Create a class Point having X and Y Axis with getter and setter functions in C++
C++ class program: Here, we are going to learn how to implement a C++ class program to create Point class having X and Y Axis with getter and setter functions in C++.
Submitted by IncludeHelp, on September 22, 2018 [Last updated : March 01, 2023]
Creating a class "Point" having X and Y Axis with getter and setter functions
We have two declare a point class with two Axis X and Y and we have to create/design its getter and setter functions.
As we know that a class has some data members and number functions. So, here we are going to declare two private data members X and Y of integer types. X will be used for the X-axis and Y will be used for the Y-axis.
Now, let's understand what are getter and setter member functions?
Setter functions are those functions which are used to set/assign the values to the variables (class's data members). Here, in the given class, the setter function is setPoint() - it will take the value of X and Y from the main() function and assign it to the private data members X and Y.
Getter functions are those functions which are used to get the values. Here, getX() and getY() are the getter function and they are returning the values of private members X and y respectively.
C++ program to create a class "Point" having X and Y Axis with getter and setter functions
#include <iostream>
using namespace std;
// claas declaration
class point {
private:
int X, Y;
public:
//defualt constructor
point()
{
X = 0;
Y = 0;
}
//setter function
void setPoint(int a, int b)
{
X = a;
Y = b;
}
//getter functions
int getX(void)
{
return X;
}
int getY(void)
{
return Y;
}
};
//Main function
int main()
{
//object
point p1, p2;
//set points
p1.setPoint(5, 10);
p2.setPoint(50, 100);
//get points
cout << "p1: " << p1.getX() << " , " << p1.getY() << endl;
cout << "p1: " << p2.getX() << " , " << p2.getY() << endl;
return 0;
}
Output
p1: 5 , 10
p1: 50 , 100