overload the rest of the operators, add to the code arithmetic operators (multiplication, division but avoid division by zero, subtraction) as friend members. Comparison operators as friend members increment and decrement --p,p--,++p,p++ as member functions main.cpp  #include #include "point.hpp" using namespace std; int main() { point p1(10, 12); point p2(2, 3); point p3; p3 = p1 + p2; p3.print(); pair P(2, 3); point p4 = p1 + P; p4.print(); point p5 = P + p1; p5.print(); return 0; } point.hpp #include #include "point.hpp" using namespace std; int main() { point p1(10, 12); point p2(2, 3); point p3; p3 = p1 + p2; p3.print(); pair P(2, 3); point p4 = p1 + P; p4.print(); point p5 = P + p1; p5.print(); return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

overload the rest of the operators, add to the code

arithmetic operators (multiplication, division but avoid division by zero, subtraction) as friend members.

Comparison operators as friend members

increment and decrement --p,p--,++p,p++ as member functions

main.cpp 

#include <iostream>
#include "point.hpp"
using namespace std;

int main() {
point p1(10, 12);
point p2(2, 3);
point p3;
p3 = p1 + p2;
p3.print();

pair <double, double> P(2, 3);
point p4 = p1 + P;
p4.print();

point p5 = P + p1;
p5.print();

return 0;
}

point.hpp

#include <iostream>
#include "point.hpp"
using namespace std;

int main() {
point p1(10, 12);
point p2(2, 3);
point p3;
p3 = p1 + p2;
p3.print();

pair <double, double> P(2, 3);
point p4 = p1 + P;
p4.print();

point p5 = P + p1;
p5.print();

return 0;
}

 

 

Expert Solution
Step 1

#include <iostream>

#include "point.hpp"

using namespace std;

 

int main()

{

    point p1(10,12);

    point p2(2,3);

    cout<<"Points are:"<<endl;

    p1.print();

    p2.print();

    point p3 ;

    p3 = p1 + p2;

    cout<<"sum of two points:"<<endl;

    p3.print();

      

    //p1+(2,3)

    pair <double,double> P(2,3);

    point p4 = p1 + P;

    p4.print();

      

    //(2,3) + p1

    point p5 = P + p1;

    p5.print ();

    

    //

    cout<<"product of two points:"<<endl;

    point p6= p1 * p2;

    p6.print();

    

    

    cout<<"division of two points:"<<endl;

    point p7= p1 / p2;

    p7.print();

    

    

    cout<<"Difference of two points:"<<endl;

    point p8= p1 - p2;

    p8.print();

    

   

    

    return 0;

}

#include <iostream>

using namespace std;

#ifndef POINT_H

#define POINT_H

 

class point

{

 

 

    public:

        point(double X = 0, double Y = 0): x(X), y(Y) {}

  

        void setX(double x)

        {

            this ->x=x;

        }

        double getX() const

        {

            return this->x ;

        }

        void setY(double y)

        {

            this->y=y ;

        }

        double getY() const

        {

            return this->y ;

        }

        void print() const ;

        

    

    

        private:

        double x, y;// Private data members

        

        friend point operator+(point lhp, point rhp);

        friend point operator*(point lhp, point rhp);

        friend point operator/(point lhp, point rhp);

        friend point operator-(point lhp, point rhp);

        friend point operator+(point lhp, pair <double, double> rhp);

        friend point operator+(pair <double, double> lhp, point rhp) ;

        

    };

    void point::print() const{

    cout<<"x,y : ("<<this ->x<<","<<this->y<<")"<<endl;

    }

    point operator+(point lhp, point rhp){

    return point(lhp.x +rhp.x, +lhp.y+rhp.y);

    }

    point operator+(point lhp, pair<double,double> rhp){

    return point(lhp.x +rhp.first, lhp.y +rhp.second);

    }

    point operator+(pair<double,double> lhp, point rhp){

    return point(lhp.first +rhp.x, lhp.second +rhp.y);

    }

    point operator*(point lhp, point rhp){

    return point(lhp.x *rhp.x, lhp.y*rhp.y);

    }

    point operator/(point lhp, point rhp){

    return point(lhp.x /rhp.x, lhp.y/rhp.y);

    }

    point operator-(point lhp, point rhp){

    return point(lhp.x -rhp.x, lhp.y-rhp.y);

    }

    

    #endif

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education