自定义一个类student,里面有属性:name,sex,id。成员方法:show(self),stud(self)。然后在if_name=='_main_':中测试当前student里面的代码
时间: 2024-03-13 16:45:02 浏览: 74
好的,以下是代码实现:
```python
class Student:
def __init__(self, name, sex, id):
self.name = name
self.sex = sex
self.id = id
def show(self):
print("姓名:{},性别:{},学号:{}".format(self.name, self.sex, self.id))
def stud(self):
print("{}同学正在学习".format(self.name))
if __name__ == '__main__':
s = Student("张三", "男", "20210001")
s.show()
s.stud()
```
在if_name=='_main_':中,我们创建了一个学生对象s,并通过构造函数传递了姓名、性别、学号等属性值。接着,调用show()方法和stud()方法,分别展示了学生的基本信息和学习状态。运行结果如下:
```
姓名:张三,性别:男,学号:20210001
张三同学正在学习
```
相关问题
#include<iostream> #include<string> using namespace std; class Student { public: Student(int n,string nam) { num=n; name=nam; } void display() { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; } protected: int num; string name; }; class Student1:public Student { public: Student1(int n,string name,int a):Student(n,nam) { age=a; } void show() { display(); cout<<"age:"<<age<<endl; } private: int age; }; class Student2:public Student1 { public: Student2(int n,string name,int a,int s):Student1(n,nam,a) { sex=s; } void show_all() { show(); cout<<"score:"<<score<<endl; } private: int score; }; int main() { Student2 stud(10010,"zaiweitao",17,89); stud.show_all(); return 0; }哪里错了?
您在派生类 `Student1` 的构造函数中将 `nam` 作为参数传递,但 `nam` 并没有定义,应该改为 `name`,即:
```
Student1(int n, string name, int a) : Student(n, name) {
age = a;
}
```
同样,在派生类 `Student2` 的构造函数中,应该将参数 `name` 改为 `nam` 或 `name`,并且要在构造函数中初始化 `score`,否则会有编译错误。改正后的代码如下:
```
class Student1 : public Student {
public:
Student1(int n, string name, int a) : Student(n, name) {
age = a;
}
void show() {
display();
cout << "age:" << age << endl;
}
private:
int age;
};
class Student2 : public Student1 {
public:
Student2(int n, string name, int a, int s) : Student1(n, name, a) {
score = s;
}
void show_all() {
show();
cout << "score:" << score << endl;
}
private:
int score;
};
int main() {
Student2 stud(10010, "zaiweitao", 17, 89);
stud.show_all();
return 0;
}
```
注意:在使用继承时,每个派生类的构造函数必须调用直接基类的构造函数,以确保直接基类的成员得到初始化。
定义一个描述学生基本情况的类,数据成员包括:年龄、姓名、性别;成员函数包括:输入和输出各个数据成员的值。 分别给出如下的3个文件: ①含类定义的头文件student.h, //student.h class Student { public: void display( ); private: int num; char name[20]; char sex; }; ②包含成员函数定义的源文件student.cpp //student.cpp #include <iostream> using namespace std; #include "student.h" void Student::display( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } ③包含主函数的源文件main.cpp。 为了组成一个完整的源程序,应当有包括主函数的源文件: //main.cpp #include <iostream> using namespace std; #include "student.h" int main( ) {Student stud; stud.display(); return 0; } 请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。修改补充代码并上机运行
/student.h
```c++
class Student {
public:
void display();
void set_value(int n, char *nam, char s);
private:
int num;
char name[20];
char sex;
};
```
/student.cpp
```c++
#include <iostream>
#include "student.h"
using namespace std;
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
void Student::set_value(int n, char *nam, char s) {
num = n;
strcpy(name, nam);
sex = s;
}
```
/main.cpp
```c++
#include <iostream>
#include "student.h"
using namespace std;
int main() {
Student stud;
stud.set_value(1001, "Tom", 'M');
stud.display();
return 0;
}
```
阅读全文