Question

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 << "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 implementation file***

#include "vector.h"
#include
#include
using namespace std;

vectorInfo::vectorInfo()
{
    x = 0;
    y = 0;
}

vectorInfo::vectorInfo(double a, double b)
{
    x = a;
    y = b;
}

void vectorInfo::set(double a, double b)
{
    x = a;
    y = b;
}

double vectorInfo::magnitude()
{
    return sqrt(x * x + y * y);
}

double vectorInfo::direction()
{
    return atan(y / x);
  
}

void vectorInfo::print()
{
    cout << x << " " << y << endl;
}

vectorInfo vectorInfo::operator-(vectorInfo const& b)
{
    return vectorInfo(x - b.x, y - b.y);
}

double vectorInfo::operator*(vectorInfo const& b)
{
    return x * b.x + y * b.y;
}

const 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:***

#ifndef VECTOR_H
#define VECTOR_H

class vectorInfo
{
    public:
        vectorInfo();

        vectorInfo(double a, double b);

        // member functions

        void set(double a, double b);

        double magnitude();

        double direction();

        void print();

        // member operators

        vectorInfo operator-(vectorInfo const& b);

        double operator*(vectorInfo const& b);

    private:
        double x;

        double y;
};

        // non member operators
        const vectorInfo operator+(vectorInfo const& a, vectorInfo const& b);

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

#endif /* VECTOR_H */

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

changes done are friend make private member accessible even when this is non-member functions

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

and void print() const; // declare it const

/* vector.h */

#ifndef VECTOR_H
#define VECTOR_H
class vectorInfo
{
public:
vectorInfo();
vectorInfo(double a, double b);
// member functions
void set(double a, double b);
double magnitude();
double direction();
void print() const; // declare it const
// member operators
vectorInfo operator-(vectorInfo const& b);
double operator*(vectorInfo const& b);
private:
double x;

double y;
   // non member operators but friend to access private members  
   friend   const vectorInfo operator+(vectorInfo const& a, vectorInfo const& b);
friend bool operator==(vectorInfo const& a, vectorInfo const& b);

};
      
#endif /* VECTOR_H */

/* vector.cpp */

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

vectorInfo::vectorInfo()
{
x = 0;
y = 0;
}

vectorInfo::vectorInfo(double a, double b)
{
x = a;
y = b;
}

void vectorInfo::set(double a, double b)
{
x = a;
y = b;
}

double vectorInfo::magnitude()
{
return sqrt(x * x + y * y);
}

double vectorInfo::direction()
{
return atan(y / x);
  
}

void vectorInfo::print() const
{
cout << x << " " << y << endl;
}

vectorInfo vectorInfo::operator-(vectorInfo const& b)
{
return vectorInfo(x - b.x, y - b.y);
}

double vectorInfo::operator*(vectorInfo const& b)
{
return x * b.x + y * b.y;
}

const 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;
}

/* main.cpp */

#include "vector.h"
#include<iostream>
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 << "v1 + v2 : ";
(v1 + v2).print();
cout << "v1 - v2 : ";
(v1 - v2).print();
cout << "v1 * v2 : " << v1 * v2 << endl;
cout << "v1 == v2 : " << (v1 == v2) << endl;
}

/* OUTPUT */

Add a comment
Know the answer?
Add Answer to:
I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated!...
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
  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • 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...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • 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...

  • 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...

  • 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...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

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
ADVERTISEMENT