类的构造函数
6、小作业:
有这么个需求,所有的在校生都必须有学号,所以能不能实现实例化一个Student对象之后,就要求输入学号,不存在一个没有学号的Student对象被实例化出来,大家明白了吗?也就是类似于 stud1 这样的对象是不允许存在的,因为其没有学号。大家课后试着做一下!
#pragma once #include <iostream> #include <string> using namespace std; class Student { string name; string sex; unsigned int studentid; public: Student(string x,string y, unsigned int z); };
#include "Student.h" Student::Student(string x, string y, unsigned int z) { name = x; sex = y; studentid = z; cout <<"学生名字 = "<< name << " 学生性别 = "<<sex << "学生学好 = " <<studentid << endl; }
#include "Student.h" int main() { Student aaaa("阿道夫","男",26); return 0; }
浙公网安备 33010602011771号