#include <bits/stdc++.h> using namespace std;

// 包含头文件和命名空间

class student { private: int ID; string name; // 定义私有属性:学号和姓名 public: student();// 构造函数的默认值 student(int newID);// 构造函数的重载 student(string newName); ~student();// 析构函数 // 定义构造函数和析构函数 void setID(int newID);// 设置学号 void setName(string newName);// 设置姓名 int getID();// 获取学号 string getName();// 获取姓名 // 定义成员函数 };

student::student(string newName) { cout<<"student::student(string newName),newName="<<newName<<endl; // 输出构造函数被调用的信息 this->name=newName; // 初始化姓名 }

student::student(int newID) { cout<<"student::student(int newID),newID="<<newID<<endl; // 输出构造函数被调用的信息 this->ID=newID; // 初始化学号 }

void student::setID(int newID) { this->ID=newID; // 设置学号 }

void student::setName(string newName) { this->name=newName; // 设置姓名 }

int student::getID() { return this->ID; // 获取学号 }

string student::getName() { return this->name; // 获取姓名 }

student::student() { this->ID=0; this->name=""; // 初始化学号和姓名 cout<<"student::student()"<<endl; // 输出构造函数被调用的信息 }

student::~student() { cout<<"student::~student()"<<endl; // 输出析构函数被调用的信息 }

int main() { student stu4("赵同学"); // 创建一个对象,调用带有一个字符串参数的构造函数 cout<<"stu4.getName()="<<stu4.getName()<<endl; // 输出赵同学 return 0; }

// 主函数,创建对象并调用成员函

#include bitsstdc++husing namespace std;class studentprivate int ID; string name; public student; studentint newID;构造函数的重载 studentstring newName; ~student; void setIDint newID; void setNamestring new

原文地址: https://www.cveoy.top/t/topic/drzA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录