Question

I need assistance with the C++ code below to remove the "this" pointers while maintaining the...

I need assistance with the C++ code below to remove the "this" pointers while maintaining the code's current output and functionality. Also, there should be a private class in the header file, but I'm not sure what I should include there. Any help would be greatly appreciated!

***main.cpp driver file***

#include <iostream>

#include "vector.h"

using namespace std;

int main()

{

  vectorInfo v1(7, 6);

  vectorInfo v2(5, 4);

   

  v1.set(3, 2);

  cout << "v1 : ";

  v1.print();

   

  cout << "v2 : ";

  v2.print();

   

  cout << "v1 magnitude : " << v1.magnitude() << endl;

  cout << "v1 direction : " << v1.direction() << " radians" << endl;

   

  cout << "v1 + v2 : ";

  (v1 + v2).print();

   

  cout << "v1 - v2 : ";

  (v1 - v2).print();

   

  cout << "v1 * v2 : " << v1 * v2 << endl;

   

  cout << "v1 == v2 : " << (v1 == v2) << endl;

}

***vector.cpp file***

#include "vector.h"

vectorInfo::vectorInfo()

{

  this->x = 0;

  this->y = 0;

}

vectorInfo::vectorInfo(int x, int y)

{

  this->x = x;

  this->y = y;

}

void vectorInfo::set(int x, int y)

{

  this->x = x;

  this->y = y;

}

double vectorInfo::magnitude()

{

  return sqrt(x * x + y * y);

}

double vectorInfo::direction()

{

  return atan(y / x);

  //can also use atan2(y, x) which is prefered, you can read about atan2 here : http://www.cplusplus.com/reference/cmath/atan2/

}

void vectorInfo::print()

{

  cout << x << " " << y << endl;

}

vectorInfo vectorInfo::operator-(vectorInfo const& b)

{

  return vectorInfo(this->x - b.x, this->y - b.y);

}

double vectorInfo::operator*(vectorInfo const& b)

{

  return this->x * b.x + this->y * b.y;

}

vectorInfo operator+(vectorInfo const& a, vectorInfo const& b)

{

  return vectorInfo(a.x + b.x, a.y + b.y);

}

bool operator==(vectorInfo const& a, vectorInfo const& b)

{

  return a.x == b.x && a.y == b.y;

}

***vector.h header file***

#include <iostream>

#include <cmath>

using namespace std;

class vectorInfo

{

  public:

    double x;

    double y;

   

    vectorInfo();

    vectorInfo(int x, int y);

   

    // member functions

    void set(int x, int y);

    double magnitude();

    double direction();

    void print();

   

    // member operators

    vectorInfo operator-(vectorInfo const& b);

    double operator*(vectorInfo const& b);

   

    // non member operators

    friend vectorInfo operator+(vectorInfo const& a, vectorInfo const& b);

    friend bool operator==(vectorInfo const& a, vectorInfo const& b);

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//changed vector.cpp file , removed 'this' pointer usage where ever it is used , you can check my comments with keyword CHEGGEA , given output before modifying the code and also after modifying the code. Checked both output are same or not. There are same!!

//***vector.cpp file***

#include "vector.h"

vectorInfo::vectorInfo()

{

//this->x = 0;

//changed code to remove this ,CHEGGEA

x=0;

y=0;

//this->y = 0;

}

vectorInfo::vectorInfo(int x1, int y1)

{

//commented below code, CHEGGEA

//this->x = x;

//this->y = y;

//changed code to remove this ,CHEGGEA

x = x1;

y = y1;

}

void vectorInfo::set(int x1, int y1)

{

//this->x = x;

//this->y = y;

//changed code to remove this ,CHEGGEA

x = x1;

y = y1;

}

double vectorInfo::magnitude()

{

return sqrt(x * x + y * y);

}

double vectorInfo::direction()

{

return atan(y / x);

//can also use atan2(y, x) which is prefered, you can read about atan2 here : http://www.cplusplus.com/reference/cmath/atan2/

}

void vectorInfo::print()

{

cout << x << " " << y << endl;

}

vectorInfo vectorInfo::operator-(vectorInfo const& b)

{

//return vectorInfo(this->x - b.x, this->y - b.y);

//changed code to remove this ,CHEGGEA

return vectorInfo(x - b.x, y - b.y);

}

double vectorInfo::operator*(vectorInfo const& b)

{

//return this->x * b.x + this->y * b.y;

//changed code to remove this ,CHEGGEA

return x * b.x + y * b.y;

}

vectorInfo operator+(vectorInfo const& a, vectorInfo const& b)

{

return vectorInfo(a.x + b.x, a.y + b.y);

}

bool operator==(vectorInfo const& a, vectorInfo const& b)

{

return a.x == b.x && a.y == b.y;

}

/*Output before changing code

 ./main

v1 : 3 2

v2 : 5 4

v1 magnitude : 3.60555

v1 direction : 0.588003 radians

v1 + v2 : 8 6

v1 - v2 : -2 -2

v1 * v2 : 23

v1 == v2 : 0

//output after changing code

 ./main

v1 : 3 2

v2 : 5 4

v1 magnitude : 3.60555

v1 direction : 0.588003 radians

v1 + v2 : 8 6

v1 - v2 : -2 -2

v1 * v2 : 23

v1 == v2 : 0

*/

Add a comment
Know the answer?
Add Answer to:
I need assistance with the C++ code below to remove the "this" pointers while maintaining the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated!...

    I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated! ***main.cpp driver file*** #include #include #include "vector.h" using namespace std; int main() {     vectorInfo v1(3, -1);     vectorInfo v2(2, 3);     cout << "v1 : ";     v1.print();     cout << "v2 : ";     v2.print();     cout << "v1 magnitude : " << v1.magnitude() << endl;     cout << "v1 direction : " << v1.direction() << " radians" << endl;     cout...

  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • Implementing the vector class in the following code (It's C++, not Java). You just need to...

    Implementing the vector class in the following code (It's C++, not Java). You just need to implement all these methods: PushFront, PopFront, At, Erase, Insert, Clear, Reserve, Copy, Assign, and Destroy, please do not add any new properties. You cannot use std:: functions, && (unless being used for "AND"), or pass by reference. Your solutions must conform to the Big O notations next to each method. In the following code, I am going to match the names that STL uses...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT