求解,这个要怎么搞

fyk55555 2020-06-04 05:13:35
...全文
157 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fyk55555 2020-06-10
  • 打赏
  • 举报
回复
okok,谢谢
晚晶 2020-06-08
  • 打赏
  • 举报
回复
抱歉哈,你这个题目是不用string的,因为指定的存储类型就是char,是我多写了那个注释,这道题已经ok了。
fyk55555 2020-06-08
  • 打赏
  • 举报
回复
这个用string的话,有那个地方不一样
fyk55555 2020-06-08
  • 打赏
  • 举报
回复
引用 2 楼 白浅星 的回复:
提供一种思路附加代码如下:

#include <iostream>
//不知道你有没有用string,就不写string的了
using namespace std;
class Person {
private:
char name[10];
int age;
protected:
char *getName() {
return name;
}
int getAge() {
return age;
}
void setName(const char*Name) {
for (int i = 0; (name[i] = Name[i]) && (i < 10); ++i);
name[9] = 0;
}
void setAge(const int Age) {
age = Age;
}
};
class Stduent :private Person {
private:
int id;
int clas;
public:
int getId() {
return id;
}
int getClas() {
return clas;
}
void setId(int Id) {
id = Id;
}
void setClas(int Clas) {
clas = Clas;
}
char *getName() {
return Person::getName();
}
int getAge() {
return Person::getAge();
}
void setName(const char*Name) {
Person::setName(Name);
}
void setAge(const int Age) {
Person::setAge(Age);
}
};
int main() {
Stduent stu;
stu.setName("ZhangSan");
stu.setAge(20);
stu.setId(1);
stu.setClas(1);
cout << "id:" << stu.getId() << endl;
cout << "name:" << stu.getName() << endl;
cout << "age:" << stu.getAge() << endl;
cout << "class:" << stu.getClas() << endl;
return 0;
}
谢谢,那这个用string的话要在那个地方修改一下
晚晶 2020-06-07
  • 打赏
  • 举报
回复
提供一种思路附加代码如下:

#include <iostream>
//不知道你有没有用string,就不写string的了
using namespace std;
class Person {
private:
	char name[10];
	int age;
protected:
	char *getName() {
		return name;
	}
	int getAge() {
		return age;
	}
	void setName(const char*Name) {
		for (int i = 0; (name[i] = Name[i]) && (i < 10); ++i);
		name[9] = 0;
	}
	void setAge(const int Age) {
		age = Age;
	}
};
class Stduent :private Person {
private:
	int id;
	int clas;
public:
	int getId() {
		return id;
	}
	int getClas() {
		return clas;
	}
	void setId(int Id) {
		id = Id;
	}
	void setClas(int Clas) {
		clas = Clas;
	}
	char *getName() {
		return Person::getName();
	}
	int getAge() {
		return Person::getAge();
	}
	void setName(const char*Name) {
		Person::setName(Name);
	}
	void setAge(const int Age) {
		Person::setAge(Age);
	}
};
int main() {
	Stduent stu;
	stu.setName("ZhangSan");
	stu.setAge(20);
	stu.setId(1);
	stu.setClas(1);
	cout << "id:" << stu.getId() << endl;
	cout << "name:" << stu.getName() << endl;
	cout << "age:" << stu.getAge() << endl;
	cout << "class:" << stu.getClas() << endl;
	return 0;
}
Tryagain2006 2020-06-04
  • 打赏
  • 举报
回复
将下面程序中用到的方法 set... 和 get... 等补充到这里即可。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧