#include <iostream> using namespace std; class Student { int id; public: string name; void display() { cout<<id<<" "<<name<<endl; } }; int main() { Student s1; s1.id=201; s1.name= "Sogol"; s1.display(); return 0; }
时间: 2024-03-18 08:45:28 浏览: 22
这是一个使用 C++ 语言编写的简单程序,程序中定义了一个名为 Student 的类,包含 id 和 name 两个成员变量,以及一个 display() 函数用于输出 id 和 name。在 main() 函数中,创建了一个名为 s1 的 Student 对象,并对其成员变量进行了赋值,最后调用 display() 函数输出结果。需要注意的是,由于 id 成员变量被定义为私有(private),外部无法直接访问,所以程序会出现编译错误。
相关问题
#include <iostream> #include <string> using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;} private : int num; string name; char sex; }; /*在这里添加派生类的定义*/ int main() {Student1 stud1; stud1.get_value(); stud1.get_value_1(); stud1.display(); stud1.display_1(); return 0; }
这段代码定义了一个基类 `Student` 和一个派生类 `Student1`,其中 `Student1` 继承自 `Student`。
派生类 `Student1` 增加了两个成员函数 `get_value_1()` 和 `display_1()`,用于获取和显示学生的成绩信息。
完整的代码如下:
```
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
void get_value() {cin>>num>>name>>sex;}
void display() {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;}
private:
int num;
string name;
char sex;
};
class Student1 : public Student
{
public:
void get_value_1() {cin>>English>>math>>physics;}
void display_1() {cout<<"English: "<<English<<endl; cout<<"math: "<<math<<endl; cout<<"physics: "<<physics<<endl;}
private:
int English;
int math;
int physics;
};
int main()
{
Student1 stud1;
stud1.get_value();
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}
```
注意,在派生类中,需要使用 `public` 继承方式,否则派生类无法访问基类的公有成员。同时,派生类中增加的成员函数和成员变量可以在派生类对象中调用和使用。
#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: 空缺 {age=a; } void show( ) {display(); cout<<"age: "<<age<<endl; } private: int age; }; class Student2:public Student1 {public: 空缺 {score=s;} void show_all() {show(); cout<<"score:"<<score<<endl; } private: int score; }; int main( ) {Student2 stud(10010,"Li",17,89); stud.show_all( ); 补全 return 0; }
在主函数中,需要补全一个输出语句,以便输出 stud 对象的信息。
可以在 stud.show_all() 函数调用之后,加上一行代码:
```
cout << "Student2 object information:" << endl;
```
这样就可以在输出学生的信息之前,先输出一条描述信息,以便更好地区分输出内容。完整的代码如下:
```
#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 nam, 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 nam, int a, int s) : Student1(n, nam, a) {
score = s;
}
void show_all() {
show();
cout << "score: " << score << endl;
}
private:
int score;
};
int main() {
Student2 stud(10010, "Li", 17, 89);
cout << "Student2 object information:" << endl;
stud.show_all();
return 0;
}
```
执行程序后,输出结果如下:
```
Student2 object information:
num:10010
name:Li
age: 17
score: 89
```