Home » C++ programs » C++ Class and Objects 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

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.

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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.