C++面向对象程序设计方法(visual studio 2022)

某商城开发一个商品销售系统,功能如下:
1)用户分三种,普通用户账号中有账号ID,密码和余额。VIP用户除了这三种属性还有折扣比例(0.8指的是所有商品打八折)。管理员只有账号ID和密码。管理员可以对商品进行各种操作,比如添加、删除、修改和查询。所有用户必须登录商城才能进行其它操作。
2)商品有普通商品,有商品名称、编码、价格、数量。
3)用户可以查询所有商品,也可以查询自己的余额。购买商品后,计算总价并在账号余额中进行扣除。
4)请使用面向对象程序设计方法完成以上任务。

首先定义一个基类 User 表示用户,包含用户的账号ID和密码:

class User {
public:
    User(const std::string& id, const std::string& password)
        : id_(id), password_(password) {}

    // 其他公共方法

protected:
    std::string id_;
    std::string password_;
};

然后定义一个派生类 NormalUser 表示普通用户,继承自 User 类,并增加余额属性:

class NormalUser : public User {
public:
    NormalUser(const std::string& id, const std::string& password, double balance)
        : User(id, password), balance_(balance) {}

    // 其他公共方法

private:
    double balance_;
};

再定义一个派生类 VIPUser 表示VIP用户,继承自 NormalUser 类,并增加折扣比例属性:

class VIPUser : public NormalUser {
public:
    VIPUser(const std::string& id, const std::string& password, double balance, double discount)
        : NormalUser(id, password, balance), discount_(discount) {}

    // 其他公共方法

private:
    double discount_;
};

然后定义一个派生类 AdminUser 表示管理员用户,继承自 User 类:

class AdminUser : public User {
public:
    AdminUser(const std::string& id, const std::string& password)
        : User(id, password) {}

    // 其他公共方法
};

接下来,定义一个类 Product 表示商品,包含商品名称、编号、价格和数量等属性:

class Product {
public:
    Product(const std::string& name, const std::string& code, double price, int quantity)
        : name_(name), code_(code), price_(price), quantity_(quantity) {}

    // 其他公共方法

private:
    std::string name_;
    std::string code_;
    double price_;
    int quantity_;
};

然后定义一个类 ShoppingMall 表示商城系统,包含所有用户和商品的信息,以及实现商城中各种功能的方法:

class ShoppingMall {
public:
    // 构造函数,初始化所有用户和商品信息
    ShoppingMall() {}

    // 登录商城
    bool login(const std::string& id, const std::string& password);

    // 注册新用户
    bool registerUser(const std::string& id, const std::string& password);

    // 查询所有商品
    std::vector<Product> queryAllProducts();

    // 查询用户余额
    double queryBalance(const std::string& userId);

    // 购买商品
    bool purchase(const std::string& userId, const std::string& productCode, int quantity);

    // 管理员操作:添加新商品
    bool addProduct(const Product& product);

    // 管理员操作:删除商品
    bool deleteProduct(const std::string& productCode);

    // 管理员操作:修改商品信息
    bool updateProduct(const Product& product);

private:
    // 存储所有用户的信息
    std::vector<User> users_;
    // 存储所有商品的信息
    std::vector<Product> products_;
};

https://blog.csdn.net/qq_36616692/article/details/80697565
基本能实现你的

使用以下代码来实现User类:

#include <string>

class User {
public:
    std::string user_type;
    std::string account_id;
    std::string password;
    double balance;
    double discount;
    
    User(std::string user_type, std::string account_id, std::string password, double balance=0, double discount=1.0)
        : user_type(user_type), account_id(account_id), password(password), balance(balance), discount(discount) {}
    
    bool login(std::string account_id, std::string password) {
        if (this->account_id == account_id && this->password == password) {
            return true;
        } else {
            return false;
        }
    }
};

Product类的实现方式:

#include <string>

class Product {
public:
    std::string name;
    std::string code;
    double price;
    int quantity;
    
    Product(std::string name, std::string code, double price, int quantity)
        : name(name), code(code), price(price), quantity(quantity) {}
};

ShoppingMall类:

#include <iostream>
#include <string>
#include <vector>

#include "Product.h"
#include "User.h"

class ShoppingMall {
public:
    std::vector<Product> products;
    
    void query() {
        for (const auto& product : products) {
            std::cout << "Product name: " << product.name << ", code: " << product.code
                      << ", price: " << product.price << ", quantity: " << product.quantity << std::endl;
        }
    }
    
    void addProduct(const Product& product) {
        products.push_back(product);
    }
    
    void deleteProduct(const std::string& product_code) {
        for (auto it = products.begin(); it != products.end(); ++it) {
            if (it->code == product_code) {
                products.erase(it);
                break;
            }
        }
    }
    
    void updateProduct(const std::string& product_code, const Product& updated_product) {
        for (auto& product : products) {
            if (product.code == product_code) {
                product.name = updated_product.name;
                product.price = updated_product.price;
                product.quantity = updated_product.quantity;
                break;
            }
        }
    }
    
    void queryBalance(const User& user) {
        std::cout << "Your balance is " << user

C++实现,望采纳:

#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

// 商品类
class Product {
 public:
  // 构造函数
  Product(string name, int code, double price, int quantity)
      : name_(name),
        code_(code),
        price_(price),
        quantity_(quantity) {}

  // 获取商品名称
  string GetName() { return name_; }
  // 获取商品编码
  int GetCode() { return code_; }
  // 获取商品价格
  double GetPrice() { return price_; }
  // 获取商品数量
  int GetQuantity() { return quantity_; }
  // 增加商品数量
  void IncreaseQuantity(int quantity) { quantity_ += quantity; }
  // 减少商品数量
  void DecreaseQuantity(int quantity) { quantity_ -= quantity; }

 private:
  // 商品名称
  string name_;
  // 商品编码
  int code_;
  // 商品价格
  double price_;
  // 商品数量
  int quantity_;
};

// 用户类
class User {
 public:
  // 构造函数
  User(int id, string password) : id_(id), password_(password) {}

  // 获取用户 ID
  int GetID() { return id_; }
  // 获取用户密码
  string GetPassword() { return password_; }
  // 获取用户余额
  double GetBalance() { return balance_; }
  // 设置用户余额
  void SetBalance(double balance) { balance_ = balance; }

  // 购买商品
  bool BuyProduct(Product& product, int quantity) {
    // 判断商品数量是否足够
    if (product.GetQuantity() < quantity) {
      cout << "商品数量不足,无法购买" << endl;
      return false;
    }
    // 判断余额是否足够
    if (balance_ < product.GetPrice() * quantity) {
      cout << "余额不足,无法购买" << endl;
return false;
}
// 扣除余额
balance_ -= product.GetPrice() * quantity;
// 减少商品数量
product.DecreaseQuantity(quantity);
cout << "购买成功!" << endl;
return true;
}

protected:
// 用户 ID
int id_;
// 用户密码
string password_;
// 用户余额
double balance_;
};

// 普通用户类
class NormalUser : public User {
public:
NormalUser(int id, string password, double balance)
: User(id, password), balance_(balance) {}
};

// VIP 用户类
class VIPUser : public NormalUser {
public:
VIPUser(int id, string password, double balance, double discount)
: NormalUser(id, password, balance), discount_(discount) {}

// 购买商品
bool BuyProduct(Product& product, int quantity) {
// 计算折后价格
double price = product.GetPrice() * quantity * discount_;
// 调用父类的购买商品函数
return NormalUser::BuyProduct(product, quantity, price);
}

private:
// 折扣比例
double discount_;
};

// 管理员类
class Admin : public User {
public:
Admin(int id, string password) : User(id, password) {}

// 添加商品
void AddProduct(Product& product) {
products_[product.GetCode()] = product;
}
// 删除商品
void RemoveProduct(int code) { products_.erase(code); }
// 修改商品
void UpdateProduct(Product& product) { products_[product.GetCode()] = product; }
// 查询商品
Product* FindProduct(int code) {
auto it = products_.find(code);
if (it != products_.end()) {
return &it->second;
}
return nullptr;
}
// 查询所有商品
void ListProducts() {
for (const auto& [code, product] : products_) {
cout << "商品名称:" << product.GetName() << " 商品编码:" << product.GetCode()
<< " 商品价格:" << product.GetPrice() << " 商品数量:" << product.GetQuantity() << endl;
}
}

private:
// 商品列表
unordered_map<int, Product> products_;
};

int main() {
// 创建商品
Product product1("商品 1", 1, 100.0, 10);
Product product2("商品 2", 2, 50.0, 20);
Product product3("商品 3", 3, 30.0, 30);

// 创建管理员
Admin admin(1, "admin");
// 创建普通用户
NormalUser normal_user(2, "normal", 1000.0);
// 创建 VIP 用户
VIPUser vip_user(3, "vip", 2000.0, 0.8);

// 管理员添加商品
admin.AddProduct(product1);
admin.AddProduct(product2);
admin.AddProduct(product3);

// 普通用户购买商品
normal_user.BuyProduct(product1, 1);
// VIP 用户购买商品
vip_user.BuyProduct(product1, 2);

// 管理员查询商品
Product* p = admin.FindProduct(1);
if (p) {
cout << "商品名称:" << p->GetName() << " 商品编码:" << p->GetCode()
<< " 商品价格:" << p->GetPrice() << " 商品数量:" << p->GetQuantity() << endl;
} else {
cout << "未找到商品" << endl;
}

// 管理员查询所有商品
admin.ListProducts();

return 0;
}

在这个代码中,创建了三个类来实现上述功能:Product 类表示商品,User 类表示用户,Admin 类表示管理员。NormalUser 和 VIPUser 类分别表示普通用户和 VIP 用户。使用面向对象程序设计方法,将用户和商品封装在不同的类中,并使用继承关系来扩展功能。


基本的进销存管理,网上例程多了去了,稍加改改就能用