C++ 指针数组(类)
所以我有这些课程: 大体上,我编写了一个指针数组:C++ 指针数组(类),c++,class,pointers,C++,Class,Pointers,所以我有这些课程: 大体上,我编写了一个指针数组: student*arry[10] 如何使每个单元格指向不同类的对象? 例如: 我要0号,2号,4号 指向medstudent类的对象 使用(新语句) 多谢各位 这是我们班的学生 #include<iostream> #include"student.cpp" using namespace std; class medStudent:public student { public :int clinicH; public: m
student*arry[10]代码>
如何使每个单元格指向不同类的对象?
例如:
我要0号,2号,4号
指向medstudent类的对象
使用(新语句)
多谢各位
这是我们班的学生
#include<iostream>
#include"student.cpp"
using namespace std;
class medStudent:public student {
public :int clinicH;
public:
medStudent(int ch, string n , int i ):student(n,i){
setClinicH(ch);
cout << "New Medecine Student" << endl;
}
~medStudent(){
cout << "Medecine Student Deleted" << endl;
}
medStudent(medStudent & ms):student(ms){
cout << "New Copy Medecined Student" << endl;
}
medstudent(){
}
void setClinicH(int ch){
clinicH = ch;
}
int getClinicH()const{
return clinicH;
}
void print()const{
student::print();
cout << "Clinical Hours: " << getClinicH() << endl;
}
};
#包括
#包括“student.cpp”
使用名称空间std;
班级学生:公立学生{
公众:int clinicH ;;
公众:
medStudent(int-ch,string n,int-i):学生(n,i){
赛斯(ch);
cout由于您显式声明了一个MEDStuent
构造函数,编译器将不会为您的类创建默认构造函数。当您执行new medstuent();
时,您(显式)尝试调用默认构造函数,而默认构造函数并不存在
这将给您一个构建错误,如果您阅读它并向我们展示它,应该很容易诊断这个错误(当询问关于构建错误的问题时,始终在问题主体中包括完整的和未编辑的错误输出,包括任何信息输出,以及导致错误的代码)
解决方案?调用现有的参数化构造函数。例如,new-medstuent(“foo”,123)
顺便说一下,如果希望继承正常工作,并且在删除子类的对象时调用基类析构函数,则需要使析构函数虚拟=新医学院学生
?即使是最基本的初学者教程或书籍,你也应该从中学到一些东西,这告诉我们你不值得我们的帮助。除非你表现出一些努力,否则我们为什么要花费我们的努力和时间?你认为我很笨,我没有尝试过这个吗?我尝试过这个,但它告诉我有[错误]学生文件中“班级学生”的重新定义如果你尝试过这个,为什么不告诉我们你做了什么?如何创建一个你的尝试的例子并展示给我们?然后告诉我们它是如何做的,或者没有工作的,你有什么问题,你得到了什么构建错误,或者它何时/何地崩溃,或者你期望的输出是什么,你实际得到了什么轻松。我编译了所有的类,没有错误!但是在主文件中,在我写了这一行之后,它显示了错误[0]=new medstudent();[error]在“medstudent”之前需要类型说明符这不是作业,这是我自己的知识。你的代码在哪里?不要粘贴代码图片,而是用代码编辑你的问题。
#include <iostream>
//#include"medstudent.cpp"
using namespace std;
class student//:public medstudent
{
public :
static int numberOfSaeeds;
const int id;
string name;
public:
~student(){
cout << "Delete Student: " << getName() << " " << endl ;
}
student(string n, int i):id(i){
setName(n);
cout << "Student with args" << endl ;
}
void setName(string n){
name = n;
}
string getName()const{
return name;
}
void print()const{
cout << "My name is: " << name << endl;
cout << "My ID is: " << id << endl;
}
void setNOS(int nos){
numberOfSaeeds = nos;
}
int getNOS(){
return numberOfSaeeds;
}
void printAddress()const{
cout << "My address is " << this << endl;
}
student * getAddress(){
return this;
}
student(student & sc):id(sc.id){
name = sc.name;
setName(sc.getName());
cout << "New Object using the copy constructor" << endl;
}
};
#include<iostream>
using namespace std;
#include"time.cpp"
#include "student.cpp"
//#include"medstudent.cpp"
int main(){
student a1("asa" , 2);
student * a[10];
a[3]= new student("jj", 22 );
a[0] = new medStudent();
}