运算符重载(60分) class Product { string name; //产品名称 int price; //产品单价 int quantity; //剩余产品数量 public: Product(string n,int p int q); Product operator = (const Product &p); friend bool operator >(const Product &p1, const Product &p2); friend ostream& operator<<(ostream &out, Product &t ); };
时间: 2024-02-14 10:22:53 浏览: 18
这段代码定义了一个名为Product的类,包含了三个私有成员变量:name、price和quantity。同时,它还提供了一个构造函数和一个赋值运算符重载函数。
在这个类中,还定义了两个全局函数:一个是大于号运算符重载函数,用于比较两个Product对象的价格;另一个是插入运算符重载函数,用于将Product对象输出到流中。
需要注意的是,这里的大于号运算符重载函数和插入运算符重载函数都是定义为友元函数,因为它们需要访问Product类的私有成员变量。
相关问题
2、运算符重载(60分) class Product { string name; //产品名称 int price; //产品单价 int quantity; //剩余产品数量 public: Product(string n,int p int q); Product operator = (const Product &p); friend bool operator >(const Product &p1, const Product &p2); friend ostream& operator<<(ostream &out, Product &t ); }; 类Product说明如上所示,完成四个函数的定义,并在main函数中进行测试。
以下是类 `Product` 的完整定义及四个函数的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Product {
string name; // 产品名称
int price; // 产品单价
int quantity; // 剩余产品数量
public:
Product(string n, int p, int q);
Product operator=(const Product& p);
friend bool operator>(const Product& p1, const Product& p2);
friend ostream& operator<<(ostream& out, Product& t);
};
Product::Product(string n, int p, int q) {
name = n;
price = p;
quantity = q;
}
Product Product::operator=(const Product& p) {
name = p.name;
price = p.price;
quantity = p.quantity;
return *this;
}
bool operator>(const Product& p1, const Product& p2) {
if (p1.price > p2.price) {
return true;
} else {
return false;
}
}
ostream& operator<<(ostream& out, Product& t) {
out << "Product Name: " << t.name << endl;
out << "Price: " << t.price << endl;
out << "Quantity: " << t.quantity << endl;
return out;
}
int main() {
Product p1("Product1", 10, 50);
Product p2("Product2", 20, 30);
cout << "p1: " << endl;
cout << p1 << endl;
cout << "p2: " << endl;
cout << p2 << endl;
if (p1 > p2) {
cout << "p1 is more expensive than p2." << endl;
} else {
cout << "p2 is more expensive than p1." << endl;
}
p2 = p1;
cout << "p2 after assignment: " << endl;
cout << p2 << endl;
return 0;
}
```
输出结果为:
```
p1:
Product Name: Product1
Price: 10
Quantity: 50
p2:
Product Name: Product2
Price: 20
Quantity: 30
p2 is more expensive than p1.
p2 after assignment:
Product Name: Product1
Price: 10
Quantity: 50
```
其中,运算符重载函数 `operator>` 用于比较两个产品的单价大小,运算符重载函数 `operator<<` 用于输出产品的信息,运算符重载函数 `operator=` 用于实现产品对象之间的赋值操作。在主函数中,我们创建了两个产品对象 `p1` 和 `p2`,并对它们进行了比较和赋值操作。
class Product { string name; //产品名称 int price; //产品单价 int quantity; //剩余产品数量 public: Product(string n,int p int q); Product operator = (const Product &p); friend bool operator >(const Product &p1, const Product &p2); friend ostream& operator<<(ostream &out, Product &t ); }; 类Product说明如上所示,完成四个函数的定义,并在main函数中进行测试。
根据题意,以下是类Product的完整代码,包括构造函数、赋值运算符重载、大于运算符重载和流插入运算符重载:
```c++
#include <iostream>
#include <string>
using namespace std;
class Product {
string name; // 产品名称
int price; // 产品单价
int quantity; // 剩余产品数量
public:
// 构造函数
Product(string n, int p, int q) {
name = n;
price = p;
quantity = q;
}
// 赋值运算符重载
Product operator = (const Product &p) {
name = p.name;
price = p.price;
quantity = p.quantity;
return *this;
}
// 大于运算符重载
friend bool operator >(const Product &p1, const Product &p2) {
return p1.price > p2.price;
}
// 流插入运算符重载
friend ostream& operator<<(ostream &out, Product &t) {
out << "Product Name: " << t.name << endl;
out << "Price: " << t.price << endl;
out << "Quantity: " << t.quantity << endl;
return out;
}
};
int main() {
Product p1("Product A", 10, 20);
Product p2("Product B", 5, 30);
// 测试赋值运算符重载
p2 = p1;
cout << "After assign p1 to p2:\n" << p2 << endl;
// 测试大于运算符重载
if (p1 > p2) {
cout << "p1 is more expensive than p2.\n";
} else {
cout << "p2 is more expensive than p1.\n";
}
// 测试流插入运算符重载
cout << "Product Info:\n" << p1 << endl;
return 0;
}
```
在main函数中,我们定义了两个Product对象p1和p2,并测试了赋值运算符重载、大于运算符重载和流插入运算符重载的正确性。