Table of contents
1.
Introduction
2.
Syntax
3.
Public Constructors
4.
Public Methods
5.
Public Operators
6.
Requirements
7.
CPoint::CPoint
8.
CPoint::Offset
9.
CPoint::operator ==
10.
CPoint::operator !=
11.
CPoint::operator +=
12.
CPoint::operator -=
13.
CPoint::operator +
14.
CPoint::operator -
15.
Frequently Asked Questions
15.1.
What is a utility class?
15.2.
What is ATL?
15.3.
What is MFC?
15.4.
What are the classes shared by MFC and ATL?
16.
Conclusion
Last Updated: Mar 27, 2024
Easy

UtilityClass-CPoint

Author Shubham Das
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CPoint is a class which is present in the ATL(Active Template Library). The ATL(Active Template Library) is a set of template-based C++ classes. In this article we will discuss Syntax, Constructors, Methods and Operators related to CPoint.

UtilityClass CPoint

Are you ready? Let us learn about UtilityClass-CPoint.

Syntax

CPoint is a class which is present in the Active Template Library. It is very similar to the Windows POINT structure. 

The syntax for CPoint Class is as follows:

class CPoint : public tagPOINT
You can also try this code with Online C++ Compiler
Run Code

Public Constructors

CPoint::CPoint : It constructs a CPoint.

Public Methods

CPoint::Offset : It adds value to the x and y members of the CPoint.

Public Operators

Name Description
CPoint::operator - It returns the difference of a SIZE and of a CPoint, or the CSize difference between two POINTs, or the negation of a POINT, or the offset by a negative SIZE.
CPoint::operator != It checks for the inequality between two POINTs.
CPoint::operator + It returns the sum of CPoint and POINT or a SIZE, or a CRect offset by a SIZE.
CPoint::operator += It offsets CPoint by adding a POINT or SIZE.
CPoint::operator -= It offsets CPoint by subtracting a POINT or SIZE.
CPoint::operator ==  It checks equality between two POINTs.

 

Requirements

We need a header named atltypes.h to perform these operations. 

CPoint::CPoint

It will construct a CPoint object.

CPoint() throw();
CPoint(int initX, int initY) throw();
CPoint(POINT initPt) throw();
CPoint(SIZE initSize) throw();
CPoint(LPARAM dwPoint) throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. initX: It specifies the value of the x member of CPoint.
     
  2. initY: It specifies the value of the y member of CPoint.
     
  3. initPt: It is a POINT structure or CPoint which specifies the values used to initialize the CPoint.
     
  4. initSize: It is the SIZE structure or CSize that specifies the values to be used to initialize CPoint.
     
  5. dwPoint: It sets the x member and the y member to the low-order word of dwPoint and to the high-order word of dwPoint respectively.


Note: x and y members are set to 0, by default.


Below is an illustration:

CPoint   ptTopLeft(0, 0);

/*works from a POINT, too*/

POINT   ptHere;
ptHere.x = 48;
ptHere.y = 59;

CPoint   ptMFCHere(ptHere);

/* works from a SIZE*/
SIZE   sHowBig;
sHowBig.cx = 200;
sHowBig.cy = 20;


CPoint ptMFCBig(sHowBig);

/* or from a DWORD*/
DWORD   dwSize;
dwSize = MAKELONG(48, 59);

CPoint ptFromDouble(dwSize);
ASSERT(ptFromDouble == ptMFCHere);
You can also try this code with Online C++ Compiler
Run Code

CPoint::Offset

It will add the values to the x and y members of the CPoint.

void Offset(int xOffset, int yOffset) throw();
void Offset(POINT point) throw();
void Offset(SIZE size) throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. xOffset: It specifies the amount to the offset the x member of the CPoint.
  2. yOffset: It specifies the amount to the offset the y member of the CPoint.
  3. point: It specifies the amount (POINT or CPoint) to offset the CPoint.
  4. size: It specifies the amount (SIZE or CSize) to offset the CPoint.

 

Below is an illustration:

CPoint   ptStart(200, 200);

ptStart.Offset(45, 45);

CPoint   ptResult(125, 125);

ASSERT(ptStart == ptResult);



/* works with POINT, too*/
ptStart = CPoint(200, 200);

POINT pt;
pt.x = 45;
pt.y = 45;

ptStart.Offset(pt);

ASSERT(ptStart == ptResult);

/* works with SIZE, too*/

ptStart = CPoint(200, 200);

SIZE size;
size.cx = 45;
size.cy = 45;

ptStart.Offset(size);
ASSERT(ptStart == ptResult);
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator ==

It will check for the equality between two POINTs.

BOOL operator==(POINT point) const throw();
You can also try this code with Online C++ Compiler
Run Code

 

The parameters are:

  1. point: It contains a POINT structure or CPoint object.


Its return value is nonzero if the POINTs are equal, otherwise 0.

 

Below is an illustration:

CPoint ptFirst(256, 128);
CPoint ptTest(256, 128);
ASSERT(ptFirst == ptTest);

/* works with POINTs, too*/
POINT pt;
pt.x = 256;
pt.y = 128;

ASSERT(ptTest == pt);
/* note that pt == ptTest isn't correct!*/
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator !=

It will check the inequality between two POINTs.

BOOL operator!=(POINT point) const throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. point: It contains a POINT structure or CPoint object.


Its return value is nonzero if the POINTs are not equal, otherwise, 0.

 

Below is an illustration:

CPoint ptFirst(512, 256);
CPoint ptTest(222, 666);

ASSERT(ptFirst != ptTest);

/* works with POINTs, too*/
POINT pt;
pt.x = 666;
pt.y = 222;

ASSERT(ptTest != pt);

/* note that pt != ptTest isn't correct!*/
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator +=

The first overload will add a CPoint to the SIZE.

void operator+=(SIZE size) throw();
void operator+=(POINT point) throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. size: It contains a SIZE structure or CSize object.
  2. point: It contains a POINT structure or CPoint object.


Note: In case of a second overload, it adds a POINT to the CPoint. For example, adding CPoint(3, -3) to a variable that contains CPoint(28, 30) changes the variable to CPoint(31, 27).

 

Below is an illustration:

CPoint   ptStart(200, 200);
CSize   szOffset(70, 70);
ptStart += szOffset;
CPoint   ptResult(270, 270);

ASSERT(ptResult == ptStart);

/* It can work on SIZE*/
ptStart = CPoint(200, 200);

SIZE s;
s.cx = 70;
s.cy = 70;


ptStart += z;
ASSERT(ptResult == ptStart);
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator -=

The first overload will subtract a SIZE from the CPoint.

void operator-=(SIZE size) throw();
void operator-=(POINT point) throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. size: It contains a SIZE structure or CSize object.
  2. point: It contains a POINT structure or CPoint object.


Note: The second overload subtracts a POINT from the CPoint. For example, subtracting CPoint(7, -3) from a variable that contains CPoint(32, 73) changes the variable to CPoint(25, 76).

 

Below is an illustration:

CPoint   ptStart(200, 200);
CSize   szOffset(70, 70);
ptStart -= szOffset;
CPoint   ptResult(130, 130);

ASSERT(ptResult == ptStart);

/* also works on SIZE*/
ptStart = CPoint(200, 200);


SIZE   sz;
sz.cx = 70;
sz.cy = 70;

ptStart -= sz;
ASSERT(ptResult == ptStart);
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator +

We utilize this operator to offset CPoint by a CSize object or CPoint, or to offset a CRect by a CPoint.


Syntax

CPoint operator+(POINT point) const throw();
CPoint operator+(SIZE size) const throw();
CRect operator+(const RECT* lpRect) const throw();
You can also try this code with Online C++ Compiler
Run Code


The parameters are:

  1. size: It contains a SIZE structure or CSize object.
  2. point: It contains a POINT structure or CPoint object.
  3. lpRect: It contains a pointer to a RECT structure or CRect object.


Its return value is a CPoint that is offset by a POINT, a CPoint that is offset by a SIZE, or a CRect offset by a POINT. 
 

Note

  • Using one of the first two overloads to offset the point CPoint(17, -39) by a point CPoint(8, 2) or size CSize(8, 2) returns the value CPoint(25, -37).
  • Adding a CRect to a POINT will return the CRect after being offset by the x and y value as specified in the POINT. We use the last overload to offset a rectangle CRect(41, 74, 13, 94) by a point CPoint(12, -9) returns CRect(53, 65, 13, 94).

 

Below is an illustration:

CPoint   ptStart(15, 15);
CSize   szOffset(1, 1);
CPoint   ptEnd;

ptEnd = ptStart + szOffset;

CPoint   ptResult(16, 16);

ASSERT(ptResult == ptEnd);

/* also works on SIZE*/

ptStart = CPoint(15, 15);

SIZE   sz;
sz.cx = 1;
sz.cy = 1;

ptEnd = ptStart + sz;
ASSERT(ptResult == ptEnd);
You can also try this code with Online C++ Compiler
Run Code

CPoint::operator -

We utilize one of the first two overloads in order to subtract a CPoint or CSize object from CPoint.


Syntax

CSize operator-(POINT point) const throw();
CPoint operator-(SIZE size) const throw();
CRect operator-(const RECT* lpRect) const throw();
CPoint operator-() const throw();
You can also try this code with Online C++ Compiler
Run Code

 

The parameters are:

  1. point: It is a POINT structure or CPoint object.
  2. size: It is a SIZE structure or CSize object.
  3. lpRect: It is a pointer to a RECT structure or a CRect object.


Its return value is CSize which is the difference between two POINTs, a CPoint which is the negation of a POINT, a CPoint which is the offset by the negation of a SIZE, or a CRect which is the offset by the negation of a POINT.

 

Note:

  • The third will overload offset CRect by the negation of the CPoint. Finally, we can use the unary operator to negate CPoint. By using the second overload we can find the difference between the point CPoint(31, -21) and the size CSize(21, 7) returns CPoint(10, -28).
     
  • When we try subtracting a CSize from CPoint, it calculates as above but returns a CPoint object, not a CSize object. By using the second overload we can find the difference between the point CPoint(41, –23) and the size CSize(32, 18) returns CPoint(9, -41).
     
  • When we try subtracting a rectangle from a POINT, it will return the rectangle offset by the non-positives of the x and y values specified in given POINT. We can use the last overload to offset the rectangle CRect(31, 32, 41, 53) by the point CPoint(14, -21) returns CRect(17, 53, 41, 53).
     
  • We can use the unary operator in order to negate any POINT. For example, using the unary operator with the point CPoint(25, -19) will return the CPoint(-25, 19).

 

Below is an illustration:

CPoint   ptStart(200, 200);
CSize   szOffset(70, 70);
CPoint   ptEnd;
ptEnd = ptStart - szOffset;
CPoint   ptResult(130, 130);
ASSERT(ptResult == ptEnd);

/*also works on SIZE*/

ptStart = CPoint(200, 200);

SIZE   sz;
sz.cx = 35;
sz.cy = 35;


ptEnd = ptStart - sz;

ASSERT(ptResult == ptEnd);


/*example for CPoint unary operator*/

CPoint   pt(70, 70);
pt = -pt;


CPoint ptNeg(-70, -70);

ASSERT(pt == ptNeg);
You can also try this code with Online C++ Compiler
Run Code


Alright! Now we hope you understand the UtilityClass-CPoint.

Frequently Asked Questions

What is a utility class?

A utility class is a class that has only static methods that perform certain operations on the objects passed as parameters. Utility classes generally have no state.

What is ATL?

ATL stands for the Active Template Library. ATL is a set of template-based C++ classes developed by Microsoft and had the intention to simplify the programming of Component Object Model objects.

What is MFC?

MFC stands for Microsoft Foundation Class Library. MFC is a C++ object-oriented library for developing desktop applications for Windows. MFC was introduced by Microsoft in 1992 and quickly gained widespread use.

What are the classes shared by MFC and ATL?

The classes that are shared by MFC and ATL are CFileTime, CFileTimeSpan, CFixedStringT, CImage, COleDateTime, COleDateTimeSpan, CPoint, CRect, CSimpleStringT, CSize, CStrBufT, CStrBufT, CStringT, CTime, CTimeSpan, and IAtlStringMgr.

Conclusion

In this article, we discussed the UtilityClass-CPoint of ATL(Active Template Library). We hope this blog on UtilityClass-CPoint was helpful. You can also refer to other similar articles.


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning Ninja!

Live masterclass