简体   繁体   English

C++ 类对象默认构造函数错误

[英]C++ class object default constructor bug

I am trying to create a class object s2 with some customized attributes and some attributes from default constructor however my output is the wrong output for the get_year function.我正在尝试创建一个具有一些自定义属性和一些来自默认构造函数的属性的类对象 s2,但是我的输出是 get_year 函数的错误输出。 It should be outputing 0 which is the key for FRESHMAN but it is out putting 2 instead.它应该输出 0,这是 FRESHMAN 的关键,但它输出的是 2。 The rest of the code is outputting as expected:其余代码按预期输出:

#include <stdio.h>
#include <iostream>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
#include <ctime>
#include <vector>
#include <cctype>

using namespace std;


enum year {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
struct name
{
    string firstName;
    string lastName;
    friend std::ostream& operator <<(ostream& os, const name& input)
    {
        os << input.firstName << ' ' << input.lastName << '\n';
        return os;
    }
};
class Student: name{

    private:
        name Name;
        year Year;
        int idNumber;
        string Department;


    
    
    




    public:
    void setname(string fn="", string ln="")
    {
        Name.firstName =fn;
        Name.lastName =ln;
    }
    name get_name()
    {
        return Name;
    }
    void set_year(year yr=FRESHMAN)
    {
        Year=yr;
    }
    year get_year()
    {
        return Year;
    }
    void set_ID(int ID=0)
    {
        idNumber=ID;
    }
    int get_ID()
    {
        return idNumber;
    }
    void set_Department(string Dept="")
    {
        Department=Dept;
    }
    string get_Department()
    {
        return Department;
    }

};
int main()
{
    Student s2;
    s2.setname("Nikolai", "Khabeboolin");
    s2.set_ID(12436193);

    cout<<"ID is: "<< s2.get_ID()<<", name is "<< s2.get_name()<<", year in school is: "<<s2.get_year()<<", Department is "<<s2.get_Department()<<endl;
    return 0;
}

Student lacks a constructor, so all its members are default initialized, and the default initialization of year Year and int idNumber is "no initialization", so reading from them is undefined behavior. Student缺少构造函数,因此它的所有成员都默认初始化,并且year Yearint idNumber的默认初始化是“不初始化”,因此从它们读取是未定义的行为。 Reading them might find 0, 2, a random value each time, or crash.读取它们可能会发现 0、2,每次都是随机值,或者崩溃。

I see that your class contains a void set_year(year yr=FRESHMAN) member, but your code never called set_year , so no part of this executed.我看到您的课程包含一个void set_year(year yr=FRESHMAN)成员,但您的代码从未调用set_year ,因此没有执行任何部分。

You should make a default constructor for Student , or as Goswin von Brederlow stated, use year Year{FRESHMAN};您应该为Student创建一个默认构造函数,或者如Goswin von Brederlow所说,使用year Year{FRESHMAN}; and int idNumber{-1};int idNumber{-1}; when declaring the members, to give them default initializations.在声明成员时,给他们默认的初始化。

By not explicitly declaring and defining a constructor, in this case Student() , you open yourself up to undefined behavior.通过不显式声明和定义构造函数,在本例中是Student() ,您将自己开放给未定义的行为。 Your constructor should call set_year(year yr=FRESHMAN ) OR even better, just set the year itself.您的构造函数应该调用set_year(year yr=FRESHMAN ) 或者更好,只需设置年份本身。

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

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