Qt中的UML图解释
UML Diagram Interpretation in Qt
问题...
我正在尝试使用 Qt 解释此 UML 图:
我关心的是如何定义 QList class 以及如何实现聚合和组合关系。我知道继承是通过.... class derived : public base directive.
实现的
我已经尝试在下面发布。
愿任何人协助。谢谢
#ifndef PRODUCT_H
#define PRODUCT_H
#include <Transaction.h>
#include <QString>
#include <QList>
#include <QDate>
#include <QDebug>
//define product class
class Product {
public:
Product(QString name, int num, double seprice, double suprice, QString sc );
virtual void sell(int n);
void restock(int n);
QString getSupplierCode();
void setProductCode(QString c );
QString getProductCode();
QList<Transaction*>getTransactions();
QString toString();
void removeAll();
bool isExpired();
private:
QString m_Name;
int m_NumOfItems;
QString m_ProductCode;
double m_SellingPrice;
double m_SupplierPrice;
QString m_SupplierCode;
QList<Transaction*>m_Transactions;
};
//end of product class
#endif //PRODUCT_H
#ifndef FOODPRODUCT_H
#define FOODPRODUCT_H
//begining of FoodProduct class
class FoodProduct : public Product {
public:
FoodProduct(QString name, int num, double seprice, double suprice, QString sc, QDate sbd);
void sell(int n);
QString toString();
bool isExpired();
private:
QDate m_SellByDate;
};
//end of FoodProduct Class
#endif // FOODPRODUCT_H
#ifndef PRODUCTLIST_H
#define PRODUCTLIST_H
//begining of ProductList Class
class ProductList : public QList<Product*> {
public:
QString add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd );
void sell(QString pc, int n );
void restock(QString pc, int n );
QString toString();
void removeStock();
QString getTransactions(QString pc) const;
private:
int m_NextCode;
};
//end of ProductList Class
#endif //PRODUCTLIST_H
#include <Product.h>
#ifndef TRANSACTION_H
#define TRANSACTION_H
//begining of Transaction Class
class Transaction
{
public:
Transaction(QString type, QDate date, int num, double price );
QString toString();
private:
QString m_Type;
QDate m_Date;
int m_NoOfItems;
double m_PricePerItem;
};
//end of Transaction class
#endif // TRANSACTION_H
implementations
#include <Product.h>
Product::Product(QString name, int num, double seprice, double suprice, QString sc)
{
m_Name = name;
m_NumOfItems = num;
m_SellingPrice = seprice;
m_SupplierPrice = suprice;
m_SupplierCode = sc;
}
void Product::sell(int n)
{
if(m_NumOfItems == 0)
{
qDebug() << "Out of stock";
}
else
{
m_NumOfItems -= n;
m_Transaction.append(Transaction("Sale", QDate::currentDate(),n, m_SellingPrice));
}
}
void Product::restock(int n)
{
m_NumOfItems += n;
m_Transaction.append(Transaction("Purchase", QDate::currentDate(),n, m_SupplierPrice));
}
QString Product::getSupplierCode()
{
return m_SupplierCode;
}
void Product::setProductCode(QString c)
{
m_ProductCode = c;
}
QString Product::getProductCode()
{
return m_ProductCode;
}
QString Product::toString()
{
return QString("Product Name: %1\nProduct Code: %2\nSupplier Price: %3\nSelling Price: %4\nSupplier Code: %5")
.arg(m_Name).arg(m_ProductCode).arg(m_SupplierPrice).arg(m_SellingPrice).arg(m_SupplierCode);
}
void Product::removeAll()
{
m_NumOfItems = 0;
}
-------------------------
#include <Transaction.h>
Transaction::Transaction(QString type, QDate date, int num, double price)
{
m_Type = type;
m_Date = date;
m_NoOfItems = num;
m_PricePerItem = price;
}
QString Transaction::toString()
{
QString date1;
date1 = m_Date.toString();
return QString("Transaction Type: %1\nDate: %2\nNumber of Items: %3\nPrice: R%4\n")
.arg(m_Type).arg(date1).arg(m_NoOfItems).arg(m_PricePerItem*m_NoOfItems);
}
--------------------------
FoodProduct::FoodProduct(QString name,int num, double seprice, double suprice, QString sc, Qdate sbd)
{
FoodProduct = Product();
m_SellByDate = sbd;
}
void FoodProduct::sell(int n)
{
if (isExpired())
qDebug << "Product has expired!";
else Product::sell(n);
}
bool FoodProduct::isExpired()
{
return(m_SellByDate < QDate::currentDate());
}
QString FoodProduct::toString()
{
Product::toString();
}
--------------------
#include ProductList.h
QString ProductList::add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd)
:int m_NextCode(1001);
{
ProductList = Product();
m_SellByDate = sbd;
m_NextCode++;
}
ProductList::~ProductList()
{
}
void ProductList:sell(Qstring pc, int n)
{
foreach(Product* pc, ProductList)
{
ProductList::m_NumberOfItems -= n;
}
}
void ProductList:restock(Qstring pc, int n)
{
foreach(Product* pc, ProductList)
{
ProductList::m_NumberOfItems += n;
}
}
void ProductList::removeStock()
{
foreach(Product* pc, ProductList)
{
if (ProductList::isExpired())
ProductList::m_NumberOfItems = 0;
}
}
QString ProductList::toString()
{
return QString("Product Name: %1\nProduct Code: %2\nNumber of Items: %3")
.arg(ProductList::m_Name).arg(ProductList::m_ProductCode).arg(ProductList::m_NumberofItems);
}
QString ProductList::getTransactions(QString pc)
{
}
QList
不得作为自身实现。它是一个通用的 class 并且 ProductList
确实实现了它。那里显示 subclass ProductList
returns 列表 Product
classes.
关于aggregation/composition,您对如何实现它比较自由。最简单的是使用数组。您还可以使用链表或任何合适的方式。当您在聚合 class 中释放对象时,聚合将使对象继续存在。相比之下,组合将在释放时杀死对象。实际上没有必要这样做。这是建模者给程序员的提示。因此,如果您自己有一些垃圾收集,您可能会以不同的方式处理它。
看的时候ProductList
感觉用的是构图不对。我想 Product
可以独立生存。所以聚合似乎更合适。不过 YMMV。
问题...
我正在尝试使用 Qt 解释此 UML 图: 我关心的是如何定义 QList class 以及如何实现聚合和组合关系。我知道继承是通过.... class derived : public base directive.
实现的#ifndef PRODUCT_H
#define PRODUCT_H
#include <Transaction.h>
#include <QString>
#include <QList>
#include <QDate>
#include <QDebug>
//define product class
class Product {
public:
Product(QString name, int num, double seprice, double suprice, QString sc );
virtual void sell(int n);
void restock(int n);
QString getSupplierCode();
void setProductCode(QString c );
QString getProductCode();
QList<Transaction*>getTransactions();
QString toString();
void removeAll();
bool isExpired();
private:
QString m_Name;
int m_NumOfItems;
QString m_ProductCode;
double m_SellingPrice;
double m_SupplierPrice;
QString m_SupplierCode;
QList<Transaction*>m_Transactions;
};
//end of product class
#endif //PRODUCT_H
#ifndef FOODPRODUCT_H
#define FOODPRODUCT_H
//begining of FoodProduct class
class FoodProduct : public Product {
public:
FoodProduct(QString name, int num, double seprice, double suprice, QString sc, QDate sbd);
void sell(int n);
QString toString();
bool isExpired();
private:
QDate m_SellByDate;
};
//end of FoodProduct Class
#endif // FOODPRODUCT_H
#ifndef PRODUCTLIST_H
#define PRODUCTLIST_H
//begining of ProductList Class
class ProductList : public QList<Product*> {
public:
QString add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd );
void sell(QString pc, int n );
void restock(QString pc, int n );
QString toString();
void removeStock();
QString getTransactions(QString pc) const;
private:
int m_NextCode;
};
//end of ProductList Class
#endif //PRODUCTLIST_H
#include <Product.h>
#ifndef TRANSACTION_H
#define TRANSACTION_H
//begining of Transaction Class
class Transaction
{
public:
Transaction(QString type, QDate date, int num, double price );
QString toString();
private:
QString m_Type;
QDate m_Date;
int m_NoOfItems;
double m_PricePerItem;
};
//end of Transaction class
#endif // TRANSACTION_H
implementations
#include <Product.h>
Product::Product(QString name, int num, double seprice, double suprice, QString sc)
{
m_Name = name;
m_NumOfItems = num;
m_SellingPrice = seprice;
m_SupplierPrice = suprice;
m_SupplierCode = sc;
}
void Product::sell(int n)
{
if(m_NumOfItems == 0)
{
qDebug() << "Out of stock";
}
else
{
m_NumOfItems -= n;
m_Transaction.append(Transaction("Sale", QDate::currentDate(),n, m_SellingPrice));
}
}
void Product::restock(int n)
{
m_NumOfItems += n;
m_Transaction.append(Transaction("Purchase", QDate::currentDate(),n, m_SupplierPrice));
}
QString Product::getSupplierCode()
{
return m_SupplierCode;
}
void Product::setProductCode(QString c)
{
m_ProductCode = c;
}
QString Product::getProductCode()
{
return m_ProductCode;
}
QString Product::toString()
{
return QString("Product Name: %1\nProduct Code: %2\nSupplier Price: %3\nSelling Price: %4\nSupplier Code: %5")
.arg(m_Name).arg(m_ProductCode).arg(m_SupplierPrice).arg(m_SellingPrice).arg(m_SupplierCode);
}
void Product::removeAll()
{
m_NumOfItems = 0;
}
-------------------------
#include <Transaction.h>
Transaction::Transaction(QString type, QDate date, int num, double price)
{
m_Type = type;
m_Date = date;
m_NoOfItems = num;
m_PricePerItem = price;
}
QString Transaction::toString()
{
QString date1;
date1 = m_Date.toString();
return QString("Transaction Type: %1\nDate: %2\nNumber of Items: %3\nPrice: R%4\n")
.arg(m_Type).arg(date1).arg(m_NoOfItems).arg(m_PricePerItem*m_NoOfItems);
}
--------------------------
FoodProduct::FoodProduct(QString name,int num, double seprice, double suprice, QString sc, Qdate sbd)
{
FoodProduct = Product();
m_SellByDate = sbd;
}
void FoodProduct::sell(int n)
{
if (isExpired())
qDebug << "Product has expired!";
else Product::sell(n);
}
bool FoodProduct::isExpired()
{
return(m_SellByDate < QDate::currentDate());
}
QString FoodProduct::toString()
{
Product::toString();
}
--------------------
#include ProductList.h
QString ProductList::add(QString name, int num, double seprice, double suprice, QString sc, QDate sbd)
:int m_NextCode(1001);
{
ProductList = Product();
m_SellByDate = sbd;
m_NextCode++;
}
ProductList::~ProductList()
{
}
void ProductList:sell(Qstring pc, int n)
{
foreach(Product* pc, ProductList)
{
ProductList::m_NumberOfItems -= n;
}
}
void ProductList:restock(Qstring pc, int n)
{
foreach(Product* pc, ProductList)
{
ProductList::m_NumberOfItems += n;
}
}
void ProductList::removeStock()
{
foreach(Product* pc, ProductList)
{
if (ProductList::isExpired())
ProductList::m_NumberOfItems = 0;
}
}
QString ProductList::toString()
{
return QString("Product Name: %1\nProduct Code: %2\nNumber of Items: %3")
.arg(ProductList::m_Name).arg(ProductList::m_ProductCode).arg(ProductList::m_NumberofItems);
}
QString ProductList::getTransactions(QString pc)
{
}
QList
不得作为自身实现。它是一个通用的 class 并且 ProductList
确实实现了它。那里显示 subclass ProductList
returns 列表 Product
classes.
关于aggregation/composition,您对如何实现它比较自由。最简单的是使用数组。您还可以使用链表或任何合适的方式。当您在聚合 class 中释放对象时,聚合将使对象继续存在。相比之下,组合将在释放时杀死对象。实际上没有必要这样做。这是建模者给程序员的提示。因此,如果您自己有一些垃圾收集,您可能会以不同的方式处理它。
看的时候ProductList
感觉用的是构图不对。我想 Product
可以独立生存。所以聚合似乎更合适。不过 YMMV。