write a definition of a class named Point that might be used to store and manipulate the location of a point in the plane.

a. A member function set that sets the private data after an object of this class is created.

b. A member function to move the point by an amount along the vertical and horizontal directions specified by the first and second arguments.

c. A member function to rotate the point by 90 degrees clockwise around the origin.

d. Two const inspector functions to retrieve the current coordinates of the point.

Here is what I have for code so far ... but I am kind of lost in getting it to shift point locations as desired and I am getting errors....

[
#include <iostream>
#include <cstdlib>

using namespace std;

class Point
{
public:
void initialize(double init_x, double init_y);
void shift(double dx, double dy);
double get_x();
double get_y();
private:
double x;
double y;

};

int main()
{
Point p1;
Point p2;

p1.initialize(-1.0, 0.8);
cout << p1.get_x() << p1.get_y() << endl;
p2.initialize(p1.get_x(),p1.get_y());
cout << p2.get_x() << p2.get_y() << endl;
p2.shift(1.3, -1.4);
cout << p2.get_x() << p2.get_y() << endl;

return 0;
}

void Point::initialize(double init_x, double init_y)
{
x = init_x;
y = init_y;
}

double Point::get_x()
{
return x;
}

void Point::shift(double dx, double dy)
{
x = x - y;
y = x;
}
]

Dani AI

Generated

The two immediate problems in the posted code are (1) a missing definition for get_y() (that produces a linker error), and (2) an incorrect shift implementation — it should add the deltas, not mix x and y. As already suggested, shift(dx,dy) should do x += dx; y += dy;. Also make the inspectors const so they can be called on a const object.

A compact, safe implementation that meets the four requirements (set, move, rotate 90° clockwise, const inspectors) looks like this:

class Point {
public:
    Point(double x = 0.0, double y = 0.0) : x_(x), y_(y) {}
    void set(double x, double y) { x_ = x; y_ = y; }
    void translate(double dx, double dy) { x_ += dx; y_ += dy; }
    void rotate90Clockwise() { double t = x_; x_ = y_; y_ = -t; }
    double x() const { return x_; }
    double y() const { return y_; }
private:
    double x_, y_;
};

Example usage:

int main() {
    Point p(-1.0, 0.8);
    std::cout << p.x() << ' ' << p.y() << '\n';
    Point q = p;
    q.translate(1.3, -1.4);
    std::cout << q.x() << ' ' << q.y() << '\n';
    q.rotate90Clockwise();
    std::cout << q.x() << ' ' << q.y() << '\n';
}

Troubleshooting notes: if the class declaration is in a header and method bodies are in a .cpp, be sure to compile/link both files (e.g., g++ main.cpp point.cpp -o prog). Use -Wall -Wextra to reveal missing definitions. Avoid using namespace std in headers. The 90° clockwise rotation uses the mapping (x,y) -> (y, -x) and must use a temporary variable (or simultaneous assignment) to avoid overwriting a value you still need.

I think this function

void Point::shift(double dx, double dy)

shoudl add dx to the x member and add dy to the y member.

As to errors, please post the exact error message (or pertinent parts thereof) and where in your code you think the problem lies.

When posting code, please use the tags

[code]

your code goes here

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.