Question

In C++, A point class has the following definition: class Point{ private: int x; int y;...

In C++, A point class has the following definition:

class Point{

private:

int x;

int y;

public:

Point(int, int);

A vector has two points as a start point (x,y) and end point (x,y). Write the header file and implementation file of the Vector class containing constructor, other functions, and also a function returning length of Vector determined from start point and end point coordinates. Write also the client code.

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

----------------------Point.h------------------

#ifndef MY_POINT

#define MY_POINT

#include<iostream>

#include<cmath>

using namespace std;

class Point{

    private:

   

        int x;

        int y;

   

    public:

   

        Point(int, int);

        int getX();

        int getY();

};

#endif

-------------------------point.cpp-------------------------

#include "Point.h"

Point::Point(int x, int y)

{

    this->x = x;

    this->y = y;

}

int Point::getX()

{

    return this->x;

}

int Point::getY()

{

    return this->y;

}


-----------------------Vector.h------------------------

#include "Point.cpp"

#include<cmath>

class Vector{

    // point class object to store the information of two points

    Point *p1, *p2;

   

    public:

       

        // constructor

        Vector(int x1, int y1, int x2, int y2);

       

        // getter methods

        Point* getP1();

        Point* getP2();

       

        double calculateLengthOfvector();

};


-------------------------Vector.cpp-----------------------------

#include "Vector.h"

// constructor

Vector::Vector(int x1, int y1, int x2, int y2)

{

    this->p1 = new Point( x1, y1 );

    this->p2 = new Point( x2, y2 );

}

// return the pointer to first point

Point* Vector::getP1()

{

    return this->p1;

}

// return the pointer to second point

Point* Vector::getP2()

{

    return this->p2;

}

double Vector::calculateLengthOfvector()

{

    // calculate distance between two points

    double dist = sqrt( pow( this->p1->getX() - this->p2->getX() , 2 ) + pow( this->p1->getX() - this->p2->getY(), 2 ) );

}


---------------------main.cpp------------------

#include "Vector.cpp"

int main()

{

    int x1, y1, x2, y2;

   

    cout<<"Enter coordinates for first point : ";

    cin>>x1>>y1;

   

    cout<<"Enter coordinates for second point : ";

    cin>>x2>>y2;

   

    Vector v1(x1, y1, x2, y2);

   

    cout<<"Distance of vector : "<<v1.calculateLengthOfvector();

   

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
In C++, A point class has the following definition: class Point{ private: int x; int y;...
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
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