繁体   English   中英

基于UML图的C ++编码

[英]coding base on UML diagram c++

我有一个Line类UML图,其中显示了以下详细信息

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

根据我对UML图的解释,这就是我所做的

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

在主cpp中,我试图调用函数getPOne()

main.cpp

#include "Point"
#include "Line"

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

在上述情况下,如何从Line类中调用getPOne()?


操作符重载

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

您的实现存在两个问题:

  1. 您不应该从Point派生您的Line类-语义上是错误的( Line不是Point的一种),并且根据UML描述也是错误的。

  2. 您需要为Point类定义一个专有运算符<< ,以便将其与cout结合使用。 如果您不这样做,则编译器将不知道如何打印数据(毕竟,这是您专有的定义数据)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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