class student { public: void set(const char *n, const char *s) { strcpy_s(name, n); strcpy_s(sex, s); } void s(const int *age) { cout << name << sex << age << endl; } private: char name[32]; char sex[32]; int age[10]; };int main() { student c1; student arr[4]; char ch[10]; char ch1[10]; int ch2[20]; for (int i = 0; i < 4; i++) { cin >> ch >> ch1 >> ch2; arr[i].set(ch, ch1); arr[i].s(ch2); } }为什么输出不了年龄
时间: 2024-03-23 07:36:25 浏览: 53
在student类中,age是一个整型数组,而在s函数中,使用了一个整型指针age来输出年龄,这是不正确的。应该将age声明为int类型而不是int数组类型,同时在set函数中为age赋值。修改后的student类和main函数如下所示:
```
class student {
public:
void set(const char *n, const char *s, int a) {
strcpy_s(name, n);
strcpy_s(sex, s);
age = a;
}
void s() {
cout << name << sex << age << endl;
}
private:
char name[32];
char sex[32];
int age;
};
int main() {
student c1;
student arr[4];
char ch[10];
char ch1[10];
int ch2;
for (int i = 0; i < 4; i++) {
cin >> ch >> ch1 >> ch2;
arr[i].set(ch, ch1, ch2);
arr[i].s();
}
}
```
这样就可以正确地输出年龄了。
相关问题
class Country { private: char name[40],capital[40]; int population; public: Country( const char* cou=NULL,const char* cap="NONE",int pop=0 ); const char* GetName const ( ); const char* GetCapital const ( ); int GetPopulation const ( ); void Set( const char* cou="Russia",const char* cap="Moscow",int pop=143000000 ); void Show(); }; Country::Country( const char* cou,const char* cap,int pop ) { strcpy(name,cou); strcpy(capital,cap); population=pop; } const char* Country::GetName const( ) { return name; } const char* Country::GetCapital const( ) { return capital; } int Country::GetPopulation const( ) { return population; } void Country::Set( const char* cou,const char* cap,int pop ) { strcpy(name,cou); strcpy(capital,cap); population=pop; } void Country::Show( ) { cout<<name<<"-"<<capital<<"-"<<population<<endl; } 有何问题
这段代码仍然有一个问题:
在定义成员函数时,函数名后的const关键字应该放在函数参数列表的后面,而不是放在函数名后面。
修正后的代码如下:
```
class Country {
private:
char name[40], capital[40];
int population;
public:
Country(const char* cou = NULL, const char* cap = "NONE", int pop = 0);
const char* GetName() const;
const char* GetCapital() const;
int GetPopulation() const;
void Set(const char* cou = "Russia", const char* cap = "Moscow", int pop = 143000000);
void Show();
};
Country::Country(const char* cou, const char* cap, int pop) {
strcpy(name, cou);
strcpy(capital, cap);
population = pop;
}
const char* Country::GetName() const {
return name;
}
const char* Country::GetCapital() const {
return capital;
}
int Country::GetPopulation() const {
return population;
}
void Country::Set(const char* cou, const char* cap, int pop) {
strcpy(name, cou);
strcpy(capital, cap);
population = pop;
}
void Country::Show() {
cout << name << "-" << capital << "-" << population << endl;
}
```
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<string.h> using namespace std; class student { private: long num; char* name; string major; char Class[4]; public: student(const char* nam); void show_stu(); }; student::student(const char* nam) { name = new char[strlen(nam) + 1]; strcpy(name, nam); cout << "分别输入学号和姓名: " << endl; cin >> num; cin >> name; cout << "输入专业:" << endl; cin >> major; cout << "输入班级:" << endl; cin >> Class[4]; } void student::show_stu() { cout << "学生信息: " << num << " " << name << endl; cout << " " << major << " " << Class[4] << endl; } class leader:public student { private: string position; public: leader(const char* na) :student(na); }; leader::leader(const char* na) { cout << "输入职位:" << endl; cin >> positon; } int main() { student a("fakeeeeee"); a.show_stu(); leader b("fakeeeeee"); return 0; }帮我找出上面c++代码的bug
这段代码有一些错误:
1. 在 `student` 类中,`cin >> name` 会将已经通过 `new` 分配的内存丢失,应该改为 `cin >> name + 1`。
2. 在 `student` 类中,`cin >> Class[4]` 会导致越界访问,应该改为 `cin >> Class`。
3. 在 `leader` 类中,构造函数的声明应该为 `leader(const char* na) :student(na) {}`,而不是 `leader(const char* na) :student(na);`。
4. 在 `leader` 类中,`cin >> positon` 应该改为 `cin >> position`。
修改后的代码如下:
```
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class student {
private:
long num;
char* name;
string major;
char Class[4];
public:
student(const char* nam);
void show_stu();
};
student::student(const char* nam) {
name = new char[strlen(nam) + 1];
strcpy(name, nam);
cout << "分别输入学号和姓名: " << endl;
cin >> num;
cin >> name + 1;
cout << "输入专业:" << endl;
cin >> major;
cout << "输入班级:" << endl;
cin >> Class;
}
void student::show_stu() {
cout << "学生信息: " << num << " " << name << endl;
cout << " " << major << " " << Class << endl;
}
class leader : public student {
private:
string position;
public:
leader(const char* na) : student(na) {};
};
int main() {
student a("fakeeeeee");
a.show_stu();
leader b("fakeeeeee");
return 0;
}
```
阅读全文