0

In C++ I am trying to create a class Point2D that contains two double values. All data members and functions should be public.

For Public Members there should be

  • double x
  • double y

For Constructors

  • the default constructor should initialize x and y to 0.0

  • Point2D(double in_x, double in_y)

    • sets x and y to in_x and in_y

For non-member Functions

  • void GetResult(Point2D p1, Point2D p2)

    • Prints x and y values for both

This is the code I have so far, can somebody please point out my errors?

Point2D.h

#ifndef POINT2D_H
#define POINT2D_H

class Point2D
{
public:
    double x;
    double y;

Point2D();
Point2D(double,double);

};

void GetResult(Point2D, Point2D);

#endif

Point2D.cpp

#include "Point2D.h"
#include <iostream>
using namespace std;

Point2D::Point2D()
{
    x = 0.0;
    y = 0.0;
}

Point2D::P1(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

Point2D::P2(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

void GetResult(Point2D P1, Point2D P2)
{
    cout << P1.x << " " << P1.y << endl;
    cout << P2.x << " " << P2.y << endl;
}

TestCheckPoint1.cpp

#include <iostream>
#include "Point2D.h"
using namespace std;

int main()
{
    Point2D Point1;
    Point1.x = 1.0;
    Point1.y= 2.0;

    Point2D Point2;
    Point2.x= 1.0;
    Point1.y= 2.0;

    GetResult(Point1, Point2);
}
Naq Z
  • 39
  • 1
  • 7
  • 4
    What is the question? ... if you compiled the code and get errors, what are the error messages? (append them to the question, not in a comment) – Paul T. Nov 09 '19 at 04:10
  • 2
    Not sure what the P1 and P2 functions are, they look like two misnamed copies of the missing constructor. You might consider avoiding `using namespace std;` – Retired Ninja Nov 09 '19 at 04:23

1 Answers1

1

You are close, but it is clear you have a bit of misunderstanding about overloaded constructors and declaring instances of your class. For starters, you don't need the functions:

Point2D::P1(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

Point2D::P2(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

You simply need a single constructor for your Point2D class that takes two double values, e.g.

Point2D::Point2D(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

Then in main() you need to declare and initialize default constuct two-instances of class Point2D providing the desired values to x and y before calling GetResult, e.g.

#include <iostream>
#include "Point2D.h"
using namespace std;

int main()
{
    Point2D Point1 (1.0, 2.0);
    Point2D Point2 (1.0, 2.0);

    GetResult(Point1, Point2);
}

(note: You can provide an initializer lists that allow initialization of the class members, see Constructors and member initializer lists. You could provide an initializer list for your constructor with, e.g. Point2D() : x(0), y(0) {}; and the overload Point2D(double, double);. Your constructor definition would simply be Point2D::Point2D(double in_x, double in_y) : x(in_x), y(in_y) {} and the compiler would initialize x, y to 0, 0 if created with Point2D Point1; or set x, y to values provided as Point2D Point2 (1.0, 2.0);)

You did a very good job including the Header Guards around the contents of Point2D.h to prevent multiple inclusion if included in more than one file. The complete header and source files for Point2D could be:

#ifndef POINT2D_H
#define POINT2D_H

class Point2D
{
public:
    double x;
    double y;

    Point2D();
    Point2D(double,double);

};

void GetResult(Point2D, Point2D);

#endif

and

#include "Point2D.h"
#include <iostream>
using namespace std;

Point2D::Point2D()
{
    x = 0.0;
    y = 0.0;
}

Point2D::Point2D(double in_x, double in_y)
{
    x = in_x;
    y = in_y;
}

void GetResult(Point2D P1, Point2D P2)
{
    cout << P1.x << " " << P1.y << endl;
    cout << P2.x << " " << P2.y << endl;
}

Example Use/Output

Compiling and running would result in:

$ ./bin/TestCheckPoint1
1 2
1 2

Note: there is no need to using namespace std; in main() at all, and you really shouldn't include the entire standard namespace anywhere. Simply remove both calls and add std:: to your two cout calls and two calls to endl (or just use '\n' instead of std::endl;). See Why is “using namespace std;” considered bad practice?

Instead simply use:

void GetResult(Point2D P1, Point2D P2)
{
    std::cout << P1.x << " " << P1.y << '\n';
    std::cout << P2.x << " " << P2.y << '\n';
}

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Maybe also point out that currently the doubles are not being *initialized*. They are being default constructed and then assigned to. They should be initialized in the constructors initialization list. – Jesper Juhl Nov 09 '19 at 07:01
  • After including the note on the use of the *initializer list*, from a learning standpoint based on the question, the default constructor was probably the simplest introduction, or way, to clear up the confusion. The C++11 initializer lists add flexibility, but also add an additional layer complexity for setting member values. Both are covered now. – David C. Rankin Nov 09 '19 at 08:14
  • The constructors initialization list - as in `Point2D() : x(0), y(0) {};` is not new in C++11, that has existed since C++98. – Jesper Juhl Nov 09 '19 at 08:46
  • I was just going by note: (2) in the cppreference link. I'm not sure when it showed up. I started when Borland was DOS based... I'll just remove the when it showed up reference. Thanks. Now I'll have to go look up the 98 introduction. – David C. Rankin Nov 09 '19 at 08:48
  • @JesperJuhl - are you sure about that one? Cpprefernce is rarely wrong and their note (2) to [Constructors and member initializer lists](https://en.cppreference.com/w/cpp/language/initializer_list) clearly says C++11. Before I start looking backwards through past standards, do you have a reference you were thinking of that might save some time? – David C. Rankin Nov 09 '19 at 08:54
  • Brace initialization and initializer lists and initializer list constructors are new since C++11. But the constructors *initialization list* (what is after the `:` and used to initialize members before the constructor body) has existed since C++98. And that last bit is what I've been talking about the whole time. – Jesper Juhl Nov 09 '19 at 09:29