Home Count number of objects created in C++
Reply: 1

Count number of objects created in C++

Srijit B
1#
Srijit B Published in 2013-01-10 08:55:12Z

Need to count no. of objects created in setval. Please help. Source Code : 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(); 
}

We have 3 member functions of the class student : 1. setval : To take input 2. showval : To display output 3. calfeespaid : To calculate total amount of fees paid

Now, my aim is to create a static int variable count which will count the number of objects created in the setval function.

qPCR4vir
2#
qPCR4vir Reply to 2013-01-10 09:08:22Z

0, no object created ..........

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342483 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO