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 16:36:25 浏览: 58
在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;
}
```
void String::setString(const char *s)
这是一个字符串类(String)中的成员函数(setString),用于将一个 C 风格的字符串(const char *s)赋值给该类对象的字符串成员变量。具体实现代码可能类似于以下代码:
```
class String {
private:
char* str;
public:
void setString(const char* s) {
// 先释放原有的字符串内存
if (str != nullptr) {
delete[] str;
}
// 计算新字符串的长度
int len = strlen(s);
// 为新字符串分配内存
str = new char[len+1];
// 复制新字符串到成员变量
strcpy(str, s);
}
};
```
阅读全文