Question

do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...

do (iii) and (iv)

(i) This is a simple Point class interface file whose objects represent points in the cartesian plane

#include <iostream>

using namespace std;

class Point

{ public:

    Point()      // default constructor

        Point(double x, double y); // another constructor

        double x() const; // get function, return _x

        double y() const; // get function, return _y

private:

        double _x, _y;

};
(ii) Here is a test driver file for the Point class

#include "Point.h" // defines Point class

#include <iostream>

using namespace std;

int main()

{

    Point p0; // invokes default constructor

        Point p1(5,-2); // invokes constructor

        Point p2 = p1;

        p0 = p1; // invokes assignment operator

        cout << "p0.x() = " << p0.x() << "\n";

        cout << "p0.y() = " << p0.y() << "\n";

}
(iii) Write the implementation file of Point class

(iv) Add the following member functions to the point class, implement them, and test them in the driver

    double magnitude() const; // returns polar coordinate r = (x^2 + y^2)^(1/2)

   void move(double dx, double dy); // move the point dx in x direction and dy in y direction;

    void print(ostream & out) const ; // print the point in the format (x, y) to ostream out

Homework Answers

Answer #1


// Point.h
#include <iostream>
using namespace std;
class Point
{ public:
Point(); // default constructor
Point(double x, double y); // another constructor
double x() const; // get function, return _x
double y() const; // get function, return _y
double magnitude() const; // returns polar coordinate r = (x^2 + y^2)^(1/2)
   void move(double dx, double dy); // move the point dx in x direction and dy in y direction;
   void print(ostream & out) const ; // print the point in the format (x, y) to ostream out
private:
double _x, _y;
};



// C++ implementation Point.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include "Point.h" // defines Point class

Point::Point() // default constructor
{
   _x = 0;
   _y = 0;
}
  
Point::Point(double x, double y) // another constructor
{  
   _x = x;
   _y = y;
}

double Point::x() const
{
   return _x;
}

double Point::y() const
{
   return _y;
}


double Point::magnitude() const // returns polar coordinate r = (x^2 + y^2)^(1/2)
{
   return sqrt(_x*_x + _y*_y);
}

void Point::move(double dx, double dy) // move the point dx in x direction and dy in y direction;
{
   _x+=dx;
   _y+=dy;
}

void Point::print(ostream & out) const // print the point in the format (x, y) to ostream out
{
   out << "(" << _x << "," << _y << ")" << endl;
}


// C++ code main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include "Point.h" // defines Point class

using namespace std;
int main()
{
Point p0; // invokes default constructor
Point p1(5,-2); // invokes constructor
Point p2 = p1;
p0 = p1; // invokes assignment operator
cout << "p0.x() = " << p0.x() << "\n";
cout << "p0.y() = " << p0.y() << "\n";
p0.print(cout);
}

/*
output:

p0.x() = 5
p0.y() = -2
(5,-2)


*/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
-For the COMPLEX class listed on the next page write code to: a) overload the addition...
-For the COMPLEX class listed on the next page write code to: a) overload the addition operator for addition between two COMPLEX values. b) Create a constructor that uses a single parameter to initialize the real member of a COMPLEX and sets the imaginary portion to 0. c) Write the code to produce the insertion operator. #include <iostream> using namespace std; class COMPLEX{    double Re;    double Im; public:    COMPLEX(double r, double im);       COMPLEX(const COMPLEX &c)...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
What is wrong with this header file? The program wont compile but if i transfer the...
What is wrong with this header file? The program wont compile but if i transfer the body of the header file into the main file it will compile just fine. This is the error message that pops up: [Error] 'cout' was not declared in this scope // Specification file for the Account class. #ifndef ITEM_H #define ITEM_H #include <string> using std::string; class Can {    private:        double price = 0.0;        string company;        string content;...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...