c++ - C++如何从函数返回对象数组

标签 c++ arrays function oop object

最近,我开始学习c++,因此,我尝试使用自己的基础知识来制作一个简单的成绩计算器(我对Javascript已有很好的了解,所以我知道编程的基础知识)。
因此,在这种情况下,我必须从函数调用中返回一个对象数组,以便以后可以在程序中使用它,但我只是找不到正确的方法。
因此,基本上我想从subArr函数返回getInput,但是由于我的语言基础知识,我无法做到这一点。我尝试使用Google搜索,但没有找到任何简单的解决方案。
这是代码,希望它很简单:

//the Subject class:
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

//get Input function declaration
Subject getInput(int num){
    
    //array of objects of type "Subject"
    Subject subArr[num];
    
    //a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr[i]=Subject(name, grade, factor);
    };
    
    //trying to return the subArr at last
    return subArr;
};

//main function
int main(){
    //get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    //trying to receive the subArr from getInput call
    Subject subArr = getInput(numOfSubjects);
    
};

最佳答案

使用std::vector:

#include <iostream>
#include <vector>

using namespace std;

// the Subject class
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

// get Input function declaration
vector<Subject> getInput(int num){
    
    // array of objects of type "Subject"
    vector<Subject> subArr;
    
    // a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr.push_back(Subject(name, grade, factor));
    };
    
    // trying to return the subArr at last
    return subArr;
};

// main function
int main(){
    // get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    // trying to receive the subArr from getInput call
    vector<Subject> subArr = getInput(numOfSubjects);
};

关于c++ - C++如何从函数返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64373574/

相关文章:

c++ - 你如何声明一个 extern "C"函数指针

c++ - CMake 图形用户界面 : Specify library path for Windows

Java 数组创建和数组名称更改

c - 时间复杂度有何不同?

c++ - 如何识别 Windows 静态库中的导出函数?

c++ - 在源文件 (cpp) 中定义带有静态和内联的模板函数有哪些优势?

javascript - 分别带有结果的 promise 数组

php - 在 PHP 中,将数组与 MySQL 表中的字段进行比较

java - 在 Java 中复制包含对象的双 ArrayList (ArrayList<ArrayList<Object>>>)

python - 如何对其他列中的每个值求和组中的值?