首页 > 解决方案 > 学生书籍的程序 - 需要在其中插入一个复制构造函数

问题描述

我需要创建一个程序来定义一个学生类 CStudent,它存储姓名信息、学生的教职员工编号和专业提供: 使用复制构造函数创建对象 使用显式构造函数创建对象(带参数)

问题是,我不知道在哪里输入构造函数。这是我得到的代码以及我是如何编写它的。

#include <iostream>



using namespace std;
#define max 10
 class CStudent
{
private:
    char name[30];
    int facnum;
    char specialty[20];

public:
    void getDetails(void);
    void putDetails(void);
};

void CStudent::getDetails(void) {
    cout << "enter name: ";
    cin >> name;
    cout << "enter facnum:";
    cin >> facnum;
    cout << "Enter student speacialty: ";
    cin >> specialty;

}

void CStudent::putDetails(void) {

    cout << " Student details: \n";
    cout << " Name:" << name << " ,facnum: " << facnum << ",Specialty: " << specialty;

}

int main()
{
    CStudent std[max];     
    int n, loop;

    cout << "Enter total number of students: ";
    cin >> n;

    for (loop = 0; loop < n; loop++) {
        cout << "Enter details of student " << loop + 1 << ":\n";
        std[loop].getDetails();
    }

    cout << endl;

    for (loop = 0; loop < n; loop++) {
        cout << "Details of student " << (loop + 1) << ":\n";
        std[loop].putDetails();
    }

    return 0;
}

标签: c++visual-c++

解决方案


首先,请使用 std::string 而不是原始 char 数组,这样更容易存储、修改和移动字符串数据:

    #include <string> // include string header
    
    std::string a = "Hello, world!" // explicit std namespace
    
    using namespace std;
    string b = "Hello, world!" // implicit std namespace

其次,您没有为该类提供任何构造函数。构造函数是一个特殊的方法,以类名命名,没有返回类型:

    class CStudent {
    private:
        string name, specialty;
        int facnum;
    public:
        CStudent() = default; // default constructor
                              // with no parameters
        CStudent(string name, int facnum, string specialty) {
            this->name = name;
            this->facnum = facnum;
            this->specialty = specialty;
        } // constructor with specified parameters
        
        // copy constructor is a special constructor,
        // that receives a const class reference as it's parameter:
        CStudent(const CStudent& ref_obj) {
            this->name = ref_obj.name;
            this->facnum = ref_obj.facnum;
            this->specialty = ref_obj.specialty;
        }
    };

现在您可以使用这些构造函数创建对象,如下所示:

    CStudent a; // default constructed
    
    int facnum;
    string name, specialty; // set the data from console or manually
    CStudent b(name, facnum, specialty);
    CStudent b2 = CStudent(name, facnum, specialty); // the same
    auto b3 = CStudent(name, facnum, specialty); // also the same
    
    CStudent c(b); // copy constructed
    CStudent c2 = b; // the same

通常为类字段设置 setter/getter 是很常见的,setter/getter 是一种从字段获取或设置数据的方法:

    // set of getters:
    string get_name() const {
        return name;
    }
    
    int get_facnum() const {
        return facnum;
    }
    
    string get_specialty() const {
        return specialty;
    }
    
    // set of setters:
    void set_name(string name) {
        this->name = name;
    }

    void set_facnum(int facnum) {
        this->facnum = facnum;
    }
    
    void set_specialty(string specialty) {
        this->specialty = specialty;
    }

现在我们可以创建一个函数,它将创建一个 CStudent 并从控制台设置它的数据:

    static CStudent GetFromConsole() {
        int facnum;
        string name, specialty;
        
        cout << "enter name: ";
        cin >> name;
        cout << "enter facnum:";
        cin >> facnum;
        cout << "Enter student speacialty: ";
        cin >> specialty;
        
        return CStudent(name, facnum, specialty);
    }

以及一种将数据打印到控制台的方法:


    void PrintStudent() const {
        cout << "Name: " << name << endl;
        cout << "Fac. number: " << facnum << endl;
        cout << "Specialty: " << specialty << endl;
    }


推荐阅读