#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; } // 主函数

C++ 类和对象:学生类实现示例