简体   繁体   中英

coding base on UML diagram c++

I have a Line class UML diagram which shows the following details

 - pOne: Point
 - pTwo: Point
 + Line (ptOne: Point, ptTwo: Point)
 + getPOne () : Point
 + getPTwo () : Point
 + setPOne (pOne: Point) 
 + setPTwo (pTwo: Point)

)

this is what I have done according to my interpretation of the UML diagram

Line.h

#ifndef __testing__Line__
#define __testing__Line__
#include <iostream>
#include "Point"

class Line  {

private:
   //pOne and pTwo are objects of Point 
   Point pOne;
   Point pTwo;

public:

    Line() {

    };//default Constructor

    // constructor of class Line to store objects of Point(pOne,pTwo)
    Line (Point pOne,Point pTwo);

    // get method for objects of Point(pOne,pTwo)
    Point getPOne();
    Point getPTwo();

    // set method for objects of a(one,two)
    void setPOne (Point pOne);
    void setPTwo (Point pTwo);

};
#endif /* defined(__testing__Line__) */

Line.cpp

#include "Line.h"

Line::Line (Point pOne, Point pTwo)  {
    setPOne(pOne);
    setPTwo(pTwo);
}

Point Line::getPOne()    {
    return pOne;
}

Point Line::getPTwo()    {
    return pTwo;
}

Line::setPOne (Point pOne)   {
     this-> pOne = pOne;
}

Line::setATwo (Point pTwo)   {
     this->pTwo = pTwo;
}

and in main cpp I tried to call the function getPOne()

main.cpp

#include "Point"
#include "Line"

int main() {
    Line outputMethod;
    //Invalid operands to binary expression
    std::cout << outputMethod.getPOne() << std::endl;
}

How should I call getPOne() from Line class given in the above situation?


operator overloading

//overload << operator
friend std::ostream& operator<<(std::ostream& os, Point&)
{
  return os;
}

There are 2 issues with your implementation:

  1. You shouldn't derive your Line class from Point - it's wrong semantically ( Line isn't a kind of Point ) and according to the UML description as well.

  2. You need to define a proprietary operator << for your Point class in order to use it with cout . If you don't do that, the compiler doesn't know how to print your data (it's your proprietary defined data, after all).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM