计算在C ++中创建的对象数

时间:2013-01-10 08:55:12

标签: c++

需要数不。在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函数中创建的对象数。

1 个答案:

答案 0 :(得分:3)

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