c++类,学生信息

#include<iostream>
#include<string>
using namespace std;
class Student
{
		private:
			string name;//string要加std::string 
			long ID;
			int age;
		public:
				void setName(string name)//void要加返回值类型可以不用returnvoid(string name) 
			{
					this->name=name;//函数的参数的值传入类成员的属性 
				
			}
			void setID(long ID)
			{
					this->ID=ID;
			}
			
			void setAge(int age)
			{
					this->age=age;
					
			}

	string getName()//输出函数 
	{
		return name;
	}
	long getID()
	{
		return ID;
	}
	int getAge()
	{
		return age;
	}
	
	
 } ;
 
 int main()
 {
 	Student c;//类的成员c 
 	c.setName("王");//输入字串要用string 
	c.setID(23411346);
	c.setAge(21); 
	cout<<c.getAge()<<"   "<<c.getID()<<"   "<<c.getName()<<endl;//"  "是空格,分开数据 
	
 	return 0;
}
 	

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的学生信息管理系统的C++类的示例: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; class Student { public: Student(string id, string name, string gender, int age, string cls) { m_id = id; m_name = name; m_gender = gender; m_age = age; m_cls = cls; } string getId() { return m_id; } string getName() { return m_name; } string getGender() { return m_gender; } int getAge() { return m_age; } string getCls() { return m_cls; } private: string m_id; string m_name; string m_gender; int m_age; string m_cls; }; class PrimaryStudent : public Student { public: PrimaryStudent(string id, string name, string gender, int age, string cls, int englishScore, int mathScore, int chineseScore) : Student(id, name, gender, age, cls) { m_englishScore = englishScore; m_mathScore = mathScore; m_chineseScore = chineseScore; } int getEnglishScore() { return m_englishScore; } int getMathScore() { return m_mathScore; } int getChineseScore() { return m_chineseScore; } private: int m_englishScore; int m_mathScore; int m_chineseScore; }; class MiddleStudent : public PrimaryStudent { public: MiddleStudent(string id, string name, string gender, int age, string cls, int englishScore, int mathScore, int chineseScore, int geographyScore, int historyScore, string address) : PrimaryStudent(id, name, gender, age, cls, englishScore, mathScore, chineseScore) { m_geographyScore = geographyScore; m_historyScore = historyScore; m_address = address; } int getGeographyScore() { return m_geographyScore; } int getHistoryScore() { return m_historyScore; } string getAddress() { return m_address; } private: int m_geographyScore; int m_historyScore; string m_address; }; int main() { // 创建一个小学生对象 PrimaryStudent ps("001", "小明", "男", 10, "一年级", 80, 90, 85); cout << "小学生信息:" << endl; cout << "学号:" << ps.getId() << endl; cout << "姓名:" << ps.getName() << endl; cout << "性别:" << ps.getGender() << endl; cout << "年龄:" << ps.getAge() << endl; cout << "班级:" << ps.getCls() << endl; cout << "英语成绩:" << ps.getEnglishScore() << endl; cout << "数学成绩:" << ps.getMathScore() << endl; cout << "语文成绩:" << ps.getChineseScore() << endl; // 创建一个中学生对象 MiddleStudent ms("002", "小红", "女", 15, "高一", 90, 95, 92, 85, 88, "北京市朝阳区"); cout << "中学生信息:" << endl; cout << "学号:" << ms.getId() << endl; cout << "姓名:" << ms.getName() << endl; cout << "性别:" << ms.getGender() << endl; cout << "年龄:" << ms.getAge() << endl; cout << "班级:" << ms.getCls() << endl; cout << "英语成绩:" << ms.getEnglishScore() << endl; cout << "数学成绩:" << ms.getMathScore() << endl; cout << "语文成绩:" << ms.getChineseScore() << endl; cout << "地理成绩:" << ms.getGeographyScore() << endl; cout << "历史成绩:" << ms.getHistoryScore() << endl; cout << "家庭住址:" << ms.getAddress() << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宝耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值