标签:oat alt public ace 图片 成员 cout mes inf
#include<iostream> using namespace std; class Student { public: Student(const char*name,int age,float score); void show(); private: static int m_total;//静态成员变量 const char* m_name; int m_age; float m_score; }; Student::Student(const char *name, int age, float score) { m_name = name; m_age = age; m_score = score; m_total++; } void Student::show() { cout << m_name << endl; cout << m_age << endl; cout << m_score << endl; cout << m_total << endl; } int Student::m_total = 0;//初始化静态成员变量 int main() { //在栈上创建对象1 Student stu("小明",19,66.4); stu.show(); //在堆上创建对象2 Student* pStu = new Student("小明",19,55.5); pStu->show(); delete pStu; //创建匿名对象3 (new Student("小明", 19, 66.6))->show(); return 0; }
标签:oat alt public ace 图片 成员 cout mes inf
原文地址:https://www.cnblogs.com/qq1480040000/p/12875419.html