C++实现组合与聚合逻辑后程序输出与预期不符问题排查
代码问题排查及修复方案
存在的问题
- Name类构造函数赋值逻辑倒置:将成员变量赋值给了形参,导致firstName、lastName始终为空,无法输出正确姓名
- Customer类成员未正确初始化:构造函数中创建的
Product** ptr是局部变量,未赋值给类成员product,且product未初始化,后续访问直接触发野指针异常 - buy函数逻辑完全错误:
- 循环计数器
i每次调用都初始化为0,永远无法触发购买上限提示 - 赋值逻辑倒置:将成员
numProduct的值赋值给形参num,没有正确设置购买数量 - 仅修改了局部参数
p的属性,未将产品存入Customer的产品数组,购买记录完全没有保存 - 没有对
numProduct做自增操作,购买计数永远为0
- 循环计数器
- print函数硬编码输出产品数为4,未使用实际计数
numProduct,且遍历逻辑未判断有效产品范围
修复后完整代码
#include <iostream> #include <string> #include <iomanip> using namespace std; class Name { private: string firstName, lastName; public: Name(string fN, string lN) { // 修复赋值顺序 firstName = fN; lastName = lN; } string getFullName() { return firstName + " " + lastName; } }; class Product { private: string name, category; double price; int quantity; public: Product() { name = " "; category = " "; price = 0; quantity = 0; } Product(string n, string c, double p) { name = n; category = c; price = p; } string getName() { return name; } string getCategory() { return category; } double getPrice() { return price; } int getQuantity() { return quantity; } void setQuantity(int q) { quantity = q; } }; class Customer { private: string address; int numProduct; // 产品数组,最多存4个 Product product[4]; public: Name name; Customer(string fn, string ln, string add) : name(fn, ln) { address = add; numProduct = 0; } void buy(Product p, int num) { if (numProduct < 4) { p.setQuantity(num); // 存入产品数组 product[numProduct] = p; numProduct++; } else { cout << "Sorry!! You already reached the maximum number of products purchased." << endl; } } void print() { double total=0; cout << left << setw(7) << "Name" << ": " << name.getFullName() << endl; cout << "Address: " << address << endl; cout << "Number of products purchased: " << numProduct << endl; cout << left; cout << setw(4) << "No" << setw(15) << "Product Name" << setw(10) << "Category" << setw(10) << "Quantity" << setw(20) << "Unit Price (RM)" << setw(15) << "Amount (RM)" << endl; for (int i=0; i<numProduct; i++) { cout << left; cout << fixed << setprecision(2); double amount = product[i].getPrice() * product[i].getQuantity(); cout << setw(4) << i+1 << setw(15) << product[i].getName() << setw(15) << product[i].getCategory() << setw(10) << product[i].getQuantity() << setw(20) << product[i].getPrice() << setw(15) << amount << endl; total += amount; } cout << fixed << setprecision(2) << "Total price = " << total << endl; } }; int main() { Customer cust("Amir", "Jalil", "Masai, Johor"); Product p1("Jacob", "Biscuit", 14.8); Product p2("Twister", "Drink", 7.5); Product p3("Ayamas", "Nugget", 18.4); Product p4("Oreo", "Biscuit", 3.8); cust.buy(p4, 5); cust.buy(p2, 4); cust.buy(p3, 2); cust.buy(p1, 3); cust.print(); return 0; }
运行输出
Name : Amir Jalil Address: Masai, Johor Number of products purchased: 4 No Product Name Category Quantity Unit Price (RM) Amount (RM) 1 Oreo Biscuit 5 3.80 19.00 2 Twister Drink 4 7.50 30.00 3 Ayamas Nugget 2 18.40 36.80 4 Jacob Biscuit 3 14.80 44.40 Total price = 130.20
内容的提问来源于stack exchange,提问作者flyhigh
相关产品推荐
相关产品推荐

