cout<<"school:" << Student::school
时间: 2024-05-02 08:14:09 浏览: 20
cout是C++中的输出流对象,用于向控制台数据。"<<"是流插入运算符,用于将右侧的数据插入到左侧的流对象中。
Student::school是一个静态成员变量,表示学生的学校。静态成员变量属于类,而不属于类的实例对象。可以通过类名和作用域解析运算符::来访问。
所以,cout<<"school:" << Student::school的意思是将字符串"school:"和Student类的静态成员变量school的值一起输出到控制台。
相关问题
#include<iostream> #include<string.h> using namespace std; class stu{ private: string name; int num; int cla; public: stu(){} stu(string a,int b,int c){ name=a; num=b; cla=c; } void show1(){ cout<<num<<"->"<<name<<"->"<<cla<<endl; } }; class student:public stu{ private: string school; string teacher; string major; public: student(){} student(string d,string e,string f);stu(name,num,cla){ school=d; teacher=e; major=f; } void show2(){ cout<<school<<"->"<<teacher<<"->"<<major<<endl; } }; int main(){ string name,school,teacher,major; int num,cla; cin>>name>>num>>cla>>school>>teacher>>major; stu x1(name,num,cla); x1.show1(); student x2(school,teacher,major); x2.show2(); return 0; }
这段代码存在一些错误:
1. `student(string d,string e,string f);stu(name,num,cla)` 这一行应该改为 `student(string d,string e,string f,string a,int b,int c):stu(a,b,c)`,这里需要在构造函数中调用基类(即`stu`类)的构造函数来初始化基类的成员变量。
2. 在 `main` 函数中创建 `student` 对象时只传入了三个参数,而 `student` 类需要传入 6 个参数,应该改为 `student x2(school,teacher,major,name,num,cla);`。
修改后的代码如下:
```
#include<iostream>
#include<string.h>
using namespace std;
class stu{
private:
string name;
int num;
int cla;
public:
stu(){}
stu(string a,int b,int c){
name=a;
num=b;
cla=c;
}
void show1(){
cout<<num<<"->"<<name<<"->"<<cla<<endl;
}
};
class student:public stu{
private:
string school;
string teacher;
string major;
public:
student(){}
student(string d,string e,string f,string a,int b,int c):stu(a,b,c){
school=d;
teacher=e;
major=f;
}
void show2(){
cout<<school<<"->"<<teacher<<"->"<<major<<endl;
}
};
int main(){
string name,school,teacher,major;
int num,cla;
cin>>name>>num>>cla>>school>>teacher>>major;
stu x1(name,num,cla);
x1.show1();
student x2(school,teacher,major,name,num,cla);
x2.show2();
return 0;
}
```
如本章开篇所述,当小学里的上课铃响之后,学生(Student)、教师(Teacher)和校长(Principal)会对同一个消息表现出不同的行为。请设计Person、Student、Teacher以及Principal类,合理安排他们之间的继承关系并将所有类的bellRing()及析构函数设计为虚函数,使得下述代码可以正常执行并产生期望的执行结果。 裁判测试程序样例: #include <iostream> using namespace std; //定义Person, Student, Teacher, Principal类 int main() { cout << "School bell rings..." << endl; Person* persons[3] = {new Student(),new Teacher(),new Principal()}; persons[0]->bellRing(); persons[1]->bellRing(); persons[2]->bellRing(); for (auto i=0;i<3;i++) delete persons[i]; return 0; } 输入样例: 输出样例: School bell rings... I am a student learning in classroom. I am a teacher teaching in classroom. I am the principal inspecting in campus. A student object destroyed. A teacher object destroyed. A principal object destroyed.
这道题要求我们设计一个人员类(Person)以及其子类学生类(Student)、教师类(Teacher)和校长类(Principal),并实现其虚析构函数和虚函数bellRing(),从而让根据铃声表现出不同行为的场景得以实现。
具体实现可能如下:
```
#include <iostream>
using namespace std;
class Person{
public:
virtual void bellRing() {
cout<<"Person: 咚咚咚"<<endl;
}
virtual ~Person(){};
};
class Student: public Person{
public:
void bellRing() override {
cout<<"Student: 走进教室"<<endl;
}
~Student(){};
};
class Teacher: public Person{
public:
void bellRing() override {
cout<<"Teacher: 准备上课"<<endl;
}
~Teacher(){};
};
class Principal: public Person{
public:
void bellRing() override {
cout<<"Principal: 视察校园"<<endl;
}
~Principal(){};
};
int main(){
Person* p;
Student s;
Teacher t;
Principal pr;
p = &s; p->bellRing();
p = &t; p->bellRing();
p = ≺ p->bellRing();
return 0;
}
```
这段代码定义了4个类: Person、Student、Teacher、Principal,其中Person作为基类,其他三个类分别为Person的子类。这些类中都有一个虚函数bellRing(),分别用来表示学生、教师和校长对上课铃响应的行为。Person类和其子类的析构函数都设为了虚函数,以避免内存泄漏。
在主函数中,我们定义了一个Person指针p,并使用&符号将其分别指向了Student、Teacher和Principal类的对象,然后通过调用它们的bellRing()函数,可以实现各自对上课铃的不同响应行为的输出。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![hpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)