USING C++:
Referring to the header file below named coord2d.h, Implement each of the 8 operator overloads
(operators: <<, [], >, <, two versions of +, two versions of *)
#ifndef COORD2D_H #define COORD2D_H #include <iostream> using namespace std; //implement a class that keeps track of the coordinates of a point in the X-Y plane class coord2d { //overload the << operator so that if p is of type "coord2d" then "cout<<p"; //will print out (x_coord, y_coord) //e.g. if p.x_coord=3.4, p.y_coord=-7 then "cout<<p" it will print out (3.4, -7) friend ostream& operator<<(ostream&, const coord2d&); //overload the > operator as a friend function so that if p1 and p2 are of type "coord2d" //p1=(a,b), p2=(c,d) //then p1>p2 will true if a>c and b>d. It will return false otherwise //p1, p2 should not change friend bool operator>(const coord2d&, const coord2d&); //overload the < operator as a friend function so that if p1 and p2 are of type "coord2d" //p1=(a,b), p2=(c,d) //then p1<p2 will true if a<c and b<d. It will return false otherwise //p1, p2 should not change friend bool operator<(const coord2d&, const coord2d&); //overload the * operator so that if p1 and p2 are of type "coord2d" and k is a double //then p1=k*p2 will result in the coordinates of p1 be set to those of p2 multiplied by k //p2 should not change //e.g., p1=(3,5), p2=(4,-2), k=2 then p1=k*p2 will result in p1=(8,-4) and p2=(4,-2) friend coord2d operator*(double, const coord2d&); //overload the + operator so that if p1 and p2 are of type "coord2d" and x is a double //then p1=x+p2 will result in the coordinates of p1 be set to adding x to those of p2 //p2 should not change //e.g., p1=(3,5), p2=(4,-2), x=3 then p1=x+p2 will result in p1=(7,-1) and p2=(4,-2) friend coord2d operator+(double, const coord2d&); public: coord2d(double a = 0, double b = 0) { x_coord = a, y_coord = b; } //default constructor: initializes coordinates x_coord and y_coord to a and b respectively. void set(double a, double b) { x_coord = a, y_coord = b;} //sets the coordinates to (a,b) //overload the + operator as a member function so that if p1 and p2 and p3 are of type "coord2d" //then p1=p2+p3 will result in the coordinates of p1 be set to adding coordinates of p3 to those of p2 //p2, p3 should not change //e.g., p1=(3,5), p2=(4,-2), p3=(10,20) then p1=p2+p3 will result in p1=(14,18), p2=(4,-2), p3=(10,20) coord2d operator+(const coord2d&) const; //overload the * operator as a member function so that if p1 and p2 are of type "coord2d" //p1=(a,b), p2=(c,d) //then p1*p2 will return a*c+b*d //p1, p2 should not change double operator*(const coord2d&) const; //overload [] operator so if p is of type "coord2d", then //p[k] will return p_xcoord if k=0, p[k] will return p.y_coord if k=1 //and p[k] will display an error message and exit the program for other k's double& operator[](int); private: double x_coord; //x coordinate double y_coord; //y coordinate }; #endif
// coord2d.h
#ifndef COORD2D_H
#define COORD2D_H
#include <iostream>
using namespace std;
//implement a class that keeps track of the coordinates of a
point in the X-Y plane
class coord2d
{
//overload the << operator so that if p is of type "coord2d"
then "cout<<p";
//will print out (x_coord, y_coord)
//e.g. if p.x_coord=3.4, p.y_coord=-7 then "cout<<p" it will
print out (3.4, -7)
friend ostream& operator<<(ostream&, const
coord2d&);
//overload the > operator as a friend function so that if p1
and p2 are of type "coord2d"
//p1=(a,b), p2=(c,d)
//then p1>p2 will true if a>c and b>d. It will return
false otherwise
//p1, p2 should not change
friend bool operator>(const coord2d&, const
coord2d&);
//overload the < operator as a friend function so that if p1
and p2 are of type "coord2d"
//p1=(a,b), p2=(c,d)
//then p1<p2 will true if a<c and b<d. It will return
false otherwise
//p1, p2 should not change
friend bool operator<(const coord2d&, const
coord2d&);
//overload the * operator so that if p1 and p2 are of type
"coord2d" and k is a double
//then p1=k*p2 will result in the coordinates of p1 be set to those
of p2 multiplied by k
//p2 should not change
//e.g., p1=(3,5), p2=(4,-2), k=2 then p1=k*p2 will result in
p1=(8,-4) and p2=(4,-2)
friend coord2d operator*(double, const coord2d&);
//overload the + operator so that if p1 and p2 are of type
"coord2d" and x is a double
//then p1=x+p2 will result in the coordinates of p1 be set to
adding x to those of p2
//p2 should not change
//e.g., p1=(3,5), p2=(4,-2), x=3 then p1=x+p2 will result in
p1=(7,-1) and p2=(4,-2)
friend coord2d operator+(double, const coord2d&);
public:
coord2d(double a = 0, double b = 0) {
x_coord = a, y_coord = b; }
//default constructor: initializes coordinates x_coord and y_coord
to a and b respectively.
void set(double a, double b) {
x_coord = a, y_coord = b;}
//sets the coordinates to (a,b)
//overload the + operator as a member function so that if p1 and p2
and p3 are of type "coord2d"
//then p1=p2+p3 will result in the coordinates of p1 be set to
adding coordinates of p3 to those of p2
//p2, p3 should not change
//e.g., p1=(3,5), p2=(4,-2), p3=(10,20) then p1=p2+p3 will result
in p1=(14,18), p2=(4,-2), p3=(10,20)
coord2d operator+(const coord2d&) const;
//overload the * operator as a member function so that if p1 and
p2 are of type "coord2d"
//p1=(a,b), p2=(c,d)
//then p1*p2 will return a*c+b*d
//p1, p2 should not change
double operator*(const coord2d&) const;
//overload [] operator so if p is of type "coord2d", then
//p[k] will return p_xcoord if k=0, p[k] will return p.y_coord if
k=1
//and p[k] will display an error message and exit the program for
other k's
double& operator[](int);
private:
double x_coord; //x coordinate
double y_coord; //y coordinate
};
#endif
// end of coord2d.h
// coord2d.cpp
#include "coord2d.h"
#include <cstdlib>
ostream& operator<<(ostream& out, const
coord2d& p)
{
out<<"("<<p.x_coord<<",
"<<p.y_coord<<")";
return out;
}
bool operator>(const coord2d& p1, const coord2d&
p2)
{
return ((p1.x_coord > p2.x_coord) &&
(p1.y_coord > p2.y_coord));
}
bool operator<(const coord2d& p1, const coord2d&
p2)
{
return ((p1.x_coord < p2.x_coord) &&
(p1.y_coord < p2.y_coord));
}
coord2d operator*(double k, const coord2d& p)
{
return coord2d(k*p.x_coord,k*p.y_coord);
}
coord2d operator+(double k, const coord2d& p)
{
return coord2d(k+p.x_coord,k+p.y_coord);
}
coord2d coord2d:: operator+(const coord2d& p) const
{
return
coord2d(x_coord+p.x_coord,y_coord+p.y_coord);
}
double coord2d:: operator*(const coord2d& p) const
{
return((x_coord*p.x_coord)+(y_coord*p.y_coord));
}
double& coord2d:: operator[](int k)
{
if(k == 0)
return x_coord;
else if(k == 1)
return y_coord;
else
{
cout<<"Error: input can be 0
or 1"<<endl;
exit(1);
}
}
//end of coord2d.cpp
// main.cpp
#include <iostream>
#include "coord2d.h"
using namespace std;
int main()
{
coord2d p1(3.4, 7) , p2(4,-2), p3(3,5);
cout<<"P1: "<<p1<<endl;
cout<<"P2: "<<p2<<endl;
cout<<"P3: "<<p3<<endl;
if(p1 > p3)
cout<<p1<<" >
"<<p3<<endl;
else
cout<<p1<<" not >
"<<p3<<endl;
if(p1 < p2)
cout<<p1<<" <
"<<p2<<endl;
else
cout<<p1<<" not <
"<<p2<<endl;
double k = 2;
p1 = k*p2;
cout<<k<<"*"<<p2<<" =
"<<p1<<endl;
p1 = k+p2;
cout<<k<<"+"<<p2<<" =
"<<p1<<endl;
p1 = p2 + p3;
cout<<p2<<"+"<<p3<<" =
"<<p1<<endl;
double result = p2 * p3;
cout<<p2<<"*"<<p3<<" =
"<<result<<endl;
cout<<"P1[0] = "<<p1[0]<<" P1[1] = "<<p1[1]<<endl;
cout<<"p1[2] = "<<p1[2]<<endl;
return 0;
}
//end of main.cpp
Output:
USING C++: Referring to the header file below named coord2d.h, Implement each of the 8 operator...
please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...
/* 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);...
Study the c++ code below and answer the question on templates after // A polymorphic swap function for any type of object (an example) template<typename T> void MySwap( T& x, T& y) { T temp; temp = x; x = y; y = temp; } // A polymorphic class holding an [x,y] position of any type template<class T> class Position { public: Position(T x=0, T y=0) : x_(x), y_(y) {} friend std::ostream& operator<<(std::ostream& os, const Position& p) { return os...
I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
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...
Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...
Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...