Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 打印对象数组_C++_Arrays_Class_Object - Fatal编程技术网

C++ 打印对象数组

C++ 打印对象数组,c++,arrays,class,object,C++,Arrays,Class,Object,我想打印两个类的对象数组。我有一个类student作为Base类和CS_student,是_student派生类,我有一个类型student的数组,我想在class university的打印功能中打印它 我想要3个选项: 打印所有CS_学生 打印所有内容都是您的学生 打印所有CS_学生和IS_学生 但它只打印一个对象。这是我的密码 class student (){ int ID; string first_Name; string last_Name; public: virtual vo

我想打印两个类的对象数组。我有一个类student作为
Base
类和
CS_student
是_student
派生类,我有一个类型student的数组,我想在class university的打印功能中打印它

我想要3个选项:

  • 打印所有CS_学生
  • 打印所有内容都是您的学生
  • 打印所有CS_学生和IS_学生
  • 但它只打印一个对象。这是我的密码

    class student (){
    int ID;
    string first_Name;
    string last_Name;
    
    public:
    virtual void print ()=0; };
    
    class CS_student ():public student {
    
    public:
        void print(){
            cout<<" CS student "<<endl;}
    };
    class IS_student():public student {
    
    public:
        void print(){
            cout<<"IS student"<<endl;}
    };
    class university {
        student **S;
        int size;
    public :
        university (){
            S= new student *[size];
            size = 10;}
            ........
            ........
    
            void Print(){
                int y;
                cout<<"enter 1 to print CS student , enter 2 to print IS 
        student , enter 3 to print all CS and IS students";
                cin>>y;
                switch(y){
                    case 1:{
                        for ( int i =0 ; i<size ; i++){
                          student * obj = S[i];
                          CS_student *obj2 = dynamic_cast<CS_student*>(obj);
                          if(obj2){
                         obj2->print();}
                        }
                           }
                           break;
                    case 2:{
                        for ( int i =0 ; i<size ; i++){
                          student * obj = S[i];
                          IS_student *obj2 = dynamic_cast<IS_student*>(obj);
                          if(obj2){
                         obj2->print();}
                        }
                           }
                           break;
                    case 3:{
                        for ( int i =0 ; i<size ; i++){
                           S[i]->print();}
                        break;
                    default:{
                        cout<<"Error";
                            }
                           }
                }
            }
    };
    
    班级学生(){
    int-ID;
    字符串名;
    字符串last_Name;
    公众:
    虚拟空打印()=0;};
    CS_班学生():公立学生{
    公众:
    作废打印(){
    库特
    
    因此,您使用
    size
    作为动态分配数组的长度,然后将size设置为
    10
    。那么数组的大小是多少?当您使用它时,它是未初始化的,并且使用它时有未定义的行为。您需要交换这两行,以便在创建数组时
    size
    10

    在设置
    size=10
    之前,您正在初始化
    S=new student*[size];
    变量。在使用它之前,您应该初始化
    size
    变量。

    您似乎不熟悉“序列”的概念。但凭直觉,您认为
    S=new student*[size]是什么;size=10;
    会吗?如果忽略
    if(obj2){…}
    检查会怎么样?它会崩溃吗?那么我假设数组中有空指针。@UliKöhler是的,它会崩溃这意味着初始化有问题。崩溃(通常)意味着空ptr(假设它与SIGSEGV崩溃)。这意味着您让一些学生处于未初始化状态-->学生*是一个没有指向有效内存位置的指针。问题似乎在于您的初始化代码中没有显示在您的问题中。size=10;是打字错误,我在创建对象时从main发送了大小,我没有编写所有的方法,但它运行良好..onl如果(obj2){…}
    检查导致程序崩溃,打印时会忽略
    ?引入它们有什么具体原因吗?@rai这解决了您的问题吗?
    
    S= new student *[size];
    size = 10;