计数在c++中创建的对象的数量

Count number of objects created in C++

本文关键字:对象 创建 c++      更新时间:2023-10-16

需要计数。在setval中创建的对象。请帮助。源代码:https://www.dropbox.com/s/z6igpioidhov9oo/static.cpp

#include<iostream>
#include<conio.h>
using namespace std;
class student
{
    static int count;
protected:
    char name[20];
    char course[20];
    int roll;
    float fees;
public:
    student()
    {
    }
    void setval()
    {
        count++;
        cout<<"nEnter the name : ";
        cin>>name;
        /*cout<<"nEnter the course : ";
         cin>>course;
         cout<<"nEnter the roll : ";
         cin>>roll;
         */cout<<"nEnter the fees : ";
        cin>>fees;
    }
    friend float calfeespaid(student);
    void showval()
    {
        cout<<"nName = "<<name;
        //cout<<"nCourse = "<<course;
        //cout<<"nRoll = "<<roll;
        cout<<"nfees = "<<fees;
        //cout<<"nNo. of objects created : "<<count;
    }
};
float calfeespaid(student s)
{
    static float total;
    total=total+s.fees;
    return total;
}
main()
{
    student s[5],a;
    for(int i=0;i<3;i++) 
    { 
        s[i].setval(); 
        calfeespaid(s[i]); 
    }
    for( int i=0;i<3;i++) 
    { 
        //cout<<count; 
        s[i].showval(); 
    } 
    cout<<"nTotal Fees Paid : "<<calfeespaid(a); 
    getch(); 
}

我们有班级学生的3个成员函数:1. setval:获取输入2. showval:显示输出3.calfeespaid:计算已支付的总费用

现在,我的目标是创建一个静态int变量count,它将计算setval函数中创建的对象的数量。

0,没有对象创建 ..........