Home > Back-end >  [for] C/C format
[for] C/C format

Time:10-01

title:
Define a Cat class with static data members numOfcats, record the number of individuals in the Cat; A static member function getNumOfCats (), access numofCats,
Design program to test the class, realize static data members and the use of the static member function
#include
using namespace std;

/* * * * * * * * * * * * * * * * * * * * * * * *


Cat class definition, constructors, copy constructors, destructors, the number of output function getNumOfCats ()


* * * * * * * * * * * * * * * * * * * * * * */

Int main ()
{
Cat: : getNumOfCats ();
The Cat mycat1 (4);
Cat: : getNumOfCats ();
The Cat mycat2 (mycat1);
Cat: : getNumOfCats ();
return 0;
}
Input
No input
The Output
Every time the output statistics of the number of cats
The Sample Output
There are 0 cats alive!
There are 1 cats alive!
There are two cats alive!

then I code like this:
#include
using namespace std;
The class the Cat
{
Public:
The Cat () {+ + NumOfCats; }
The Cat (const Cat& Cat) {+ + NumOfCats; }
Static int getNumOfCats ()
{
Return NumOfCats;
}
The static void mycat ()
{
Cout<& lt;" There are "& lt; }
Private:
Static int NumOfCats;
}; Int the Cat: : NumOfCats=0;
Int main ()
{
Cat: : getNumOfCats ();
The Cat mycat1 (4);
Cat: : getNumOfCats ();
The Cat mycat2 (mycat1);
Cat: : getNumOfCats ();
return 0;
}

a compiler error: unable to the parameter 1 from the 'const int' convert 'const class Cat &'


this is what problem, from the beginning to learn programming is always make the mistake, not been cured, sealing at home half a month and forgot to how to deal with, if there is who is willing to take the time to help solve, niche grateful!

CodePudding user response:

The Cat mycat1 (4);//not have parameters is int
The Cat (const Cat& Cat) {+ + NumOfCats; }//only the copy constructor,
The Cat (int a) {NumOfCats=a; }//it is ok,
  • Related