简体   繁体   English

如何在另一个 class C++ 中使用一个 class 信息

[英]How to use one class information in another class C++

i'm a beginner in C++ and just wanted to know if it is possible for me to put the calculation in the last "for" loop that uses the name,amount and weight from the "Product" class to calculate the total and price in another class that would be titled "Price".我是 C++ 的初学者,我只是想知道我是否可以将计算放在最后一个“for”循环中,该循环使用“产品”class 中的名称、数量和重量来计算总价和价格另一个名为“价格”的 class。 Sorry for the weird question i am just confused as to how to use classes with one another and if it is possible to do so...抱歉这个奇怪的问题,我只是对如何相互使用类以及是否可以这样做感到困惑......

#include <iostream>
#include <string>
using namespace std;
class Product
{
public:
    string name;
    int amount;
    float weight;
    void get()
    {
        cout << "Give product name,amount and weight : " << endl;
        cin >> name >> amount >> weight;
    }
    void print()
    {
        cout << name << " - "<< amount<<" , " <<weight <<" kg"<< endl;
        cout << "--------" << endl;
    };
};
int main()
{
    Product p[100];
    int n;
    cout << "Give the number of products you want to get : " << endl;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        p[i].get();

    }
    cout << "Product display: " << endl;
    for (int i = 0; i < n; i++)
    {
        p[i].print();
    }
    float total = 0;
    for (int i = 0; i < n; i++)
    {
        cout << "\nPrice of " << p[i].name << " " << p[i].amount * p[i].weight << " $" << endl;
        total = p[i].amount * p[i].weight + total;
    }

    cout << "\nTotal: " << total << "$" << endl;

    cin.get(); cin.get();
    return 0;
}

Its fine to have such questions when you are a beginner.当你是初学者时,有这样的问题很好。 There are few points I would to mention.有几点我想说。

  1. In your example there is no need of creating a separate class just for calculating the price.在您的示例中,无需创建单独的 class 来计算价格。 Calculation of price is a flow/method/set of instructions.价格计算是一个流程/方法/一组指令。 So the logic of calculation of price should be a method and that too in the same class ie Product .所以价格计算的逻辑应该是一个方法,并且也是在同一个 class 即Product
  2. Secondly here the total price for for all the products is common for the class ie it is not different for different objects of the class.其次,所有产品的总价格对于 class 来说是通用的,即 class 的不同对象没有不同。 So here you need to create a static variable under the class which will carry the total price of all the products.所以在这里你需要在 class 下创建一个static变量,它将携带所有产品的总价格。
  3. You can just create a static function in the Product class and pass the array of product as an argument and loop through the products to calculate the total price and store it in the static variable. You can just create a static function in the Product class and pass the array of product as an argument and loop through the products to calculate the total price and store it in the static variable.

Hope this clears your doubt.希望这可以消除您的疑问。

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

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