1.掌握类的构造函数和析构函数的概念和使用方法 2.学习简单的面向对象程序的编写
1、硬件要求:计算机一台。 2、软件要求:Windows操作系统,Dev-C++或VC++ 6.0编译环境
设计一个Student类
(1)基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业;
其中:基本信息为private属性,成员函数为public属性;
(2)Student类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数
(3)基本功能:
A)可以从键盘输入学生的基本信息;
B)定义一个函数SetInfo(形参表),可以修改学生的基本信息函数,例如:班级、专业等;
C)定义一个Show()函数显示学生信息;
#include<iostream> #include<cmath> #include<string> using namespace std; class Student { private: double num; string name, sex, grade, clas, department, major,birthdate; public: Student() { } Student(double num,string birthdate,string name,string sex,string grade,string clas,string department,string major){ this->num=num; this->name=name; this->sex=sex; this->grade=grade; this->clas=clas; this->department=department; this->birthdate=birthdate; this->major=major; } ~Student() { } void SetIn() { cout << "学号:" << endl; cin >> num; cout << "姓名:" << endl; cin >> name; cout << "性别:" << endl; cin >> sex; cout << "出生日期:" << endl; cin >> birthdate; cout << "年级:" << endl; cin >> grade; cout << "班级:" << endl; cin >> clas; cout << "院级:" << endl; cin >> department; cout << "专业:" << endl; cin >> major; } void Show() { cout << "学号:" << num << endl; cout << "姓名:" << name << endl; cout << "性别:" << sex << endl; cout << "出生日期:" << birthdate << endl; cout << "年级:" << grade << endl; cout << "班级:" << clas << endl; cout << "院级:" << department << endl; cout << "专业:" << major << endl; } void SetInfo() { char a; cout << "是否修改(是=y,否=n)" << endl; cin >> a; if (a == 'y') { SetIn(); SetInfo(); } else { Show(); } } }; int main() { Student Stu; Stu.SetIn(); Stu.SetInfo(); system("pause"); return 0; }