Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:27:16+00:00 2026-06-05T19:27:16+00:00

struct PT { double x, y; PT() {} PT(double x, double y) : x(x),

  • 0
struct PT 
{   
  double x, y;  
  PT() {}  
  PT(double x, double y) : x(x), y(y) {}  
  PT(const PT &p) : x(p.x), y(p.y)    {}  
  PT operator + (const PT &p)  const { return PT(x+p.x, y+p.y); }  
  PT operator - (const PT &p)  const { return PT(x-p.x, y-p.y); }  
  PT operator * (double c)     const { return PT(x*c,   y*c  ); }  
  PT operator / (double c)     const { return PT(x/c,   y/c  ); }  
};

This code snippet is from http://stanford.edu/~liszt90/acm/notebook.html#file8 . I am not able to understand this piece of code . Someone please explain this . I know that this is operator overloading but am not able to understand how exactly operator overloading is taking place.

Can someone explain these lines also:

PT() {}  
PT(double x, double y) : x(x), y(y) {}  
PT(const PT &p) : x(p.x), y(p.y)    {}

Do structures also have constructors ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:27:18+00:00Added an answer on June 5, 2026 at 7:27 pm
    double x, y;  
    

    Declares two local, class variables that make up the class.

    PT() {}  
    

    Default constructor. Allows you to create a PT without any arguments.
    e.g. –> PT myObj;

    PT(double x, double y) : x(x), y(y) {}  
    

    Constructor to create a point from two doubles.
    e.g. –> PT myObj(3.5, 9.0);
    After the declaration –> PT(double x, double y) : x(x), y(y) {}
    we have initialisation .–> PT(double x, double y) : x(x), y(y) {}

    x(x) is equivalent to this->x = x;
    i.e. initialise the class variable ‘x’ with the constructor parameter ‘x’. It is somewhat confusing that they have given the parameters the same name as the class variables. A better example might have been:

        PT(double xInit, double yInit) 
        : x(xInit)
        , y(yInit) 
        {
        }  
    

        PT(const PT &p) : x(p.x), y(p.y)    {}  
    

    Copy constructor to create a PT object from another PT object
    e.g. –> PT myOtherObj(myObj);

    PT operator + (const PT &p)  const { return PT(x+p.x, y+p.y); }  
    PT operator - (const PT &p)  const { return PT(x-p.x, y-p.y); }
    

    Addition and subtraction operators to get the sum or difference of two points to make a third.
    e.g. –>

        PT sumObj  = myObj + myOtherObj;
        PT diffObj = myObj - myOtherObj;
    

    PT operator * (double c) const { return PT(x*c, y*c ); }
    PT operator / (double c) const { return PT(x/c, y/c ); }

    Multiplication and division operators to multiply (or divide) a point by a constant.
    e.g. –>

        PT prodObj = myObj * 2.7;
        PT divObj  = myObj / 8.0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: struct DoubleVec { std::vector<double> data; }; DoubleVec operator+(const DoubleVec& lhs, const DoubleVec&
struct X{}; template<class T> decltype(X() == int()) f(T const&){ return true; } int main(void)
Consider the following: struct Point {double x; double y;}; double complexComputation(const& Point p1, const
this is my struct typedef struct { char mmsi[10]; char name[20]; double latitude; double
I have this very simple wrapper template: template<class T> struct wrapper { inline operator
typedef pair<double, double> dd; const double epsilon = 1e-6; struct sort_by_polar_angle { dd center;
With respect to the following code segment: struct Pair{ string name; double val; }
I've defined the following struct: typedef struct { double salary; } Employee; I want
I am wondering why the following two types struct { double re[2]; }; and
Assume I have a C/C++ struct struct ABCTYPE { double A; double B; double

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.