Q:

Create a class Point having X and Y Axis with getter and setter functions in C++

belongs to collection: C++ programs on various topics

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Passing an object to a Non-Member function in C++... >>
<< Dynamic Initialization of Objects in C++...