C++ 类与对象:student 类示例
这是一个 C++ 程序,定义了一个名为 'student' 的类,其中包含了构造函数、析构函数和一些成员函数。构造函数用于初始化对象,析构函数用于释放对象占用的资源和内存。成员函数用于设置和获取成员变量的值。在 main 函数中没有具体的操作,只是返回 0,因此程序并没有实际的输出。
#include <bits/stdc++.h>
using namespace std;
/*
类,面向对象
*/
class student
{
private:
string name;
int ID;
protected:
public:
student();//构造函数
~student();//析构函数
void setName(string newName);
void setID(int newID);
string getName();
int getID();
};
student::student()
{
cout<'student::student(),构造函数,分配资源,分配内存,自动分配'<endl;
}
student::~student()
{
cout<'student::~student(),析构函数,回收资源,回收内存,自动回收'<endl;
}
void student::setName(string newName)
{
this->name=newName;
}
void student::setID(int newID)
{
this->ID=newID;
}
string student::getName()
{
return this->name;
}
int student::getID()
{
return this->ID;
}
int main()
{
return 0;
}
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nFnk 著作权归作者所有。请勿转载和采集!