【问题标题】:Count number of objects created in C++ [closed]计算在 C++ 中创建的对象的数量 [关闭]
【发布时间】:2013-01-10 08:55:12
【问题描述】:

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

【问题讨论】:

  • 等等,你想让我帮你修键盘吗?
  • 这是我给你的礼物:“”。随意复制粘贴这个空间你想要的任何地方。我建议使用它进行缩进。
  • 想要我们的东西吗?需要更多的努力。请做。如何:catb.org/esr/faqs/smart-questions.html
  • 尝试粘贴源代码,但出现很多格式错误
  • @SrijitB:缺少源代码不是这里的主要问题。您需要解释您尝试过的什么什么出了问题以及为什么您期望得到不同的结果。

标签: c++


【解决方案1】:

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

【讨论】:

  • +1,完全准确(我将代码粘贴到问题中)。 setval()中没有创建对象
  • 大声笑.......................
猜你喜欢
  • 2019-12-30
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多