#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;
}

创建对象的三种方法_创建对象

欢迎指出代码的不足之处,我很高兴你能指出我的错误。