C++ home / study / engineering / computer science / computer science questions a
ID: 3887340 • Letter: C
Question
C++
home / study / engineering / computer science / computer science questions and answers / Part 2 Create A Class Called Fun. Add To The Class A Static Integer Variable Called Count. ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: Part 2 Create a class called fun. Add to the class a static integer variable called count. Add a ...
Part 2
Create a class called fun.
Add to the class a static integer variable called count.
Add a constructor that has count++;
Add a function getCount() { return count ; }
Instantiate 4 instance of the class. F1, F2, F3 and F4.
Print out the value of count for each instance.
What do you note about the static variable called count ?
Filename: week6YourNameProgStatic
Explanation / Answer
#include <iostream>
using namespace std;
class fun{
public:
static int count;
fun() {
count++;
}
int getCount() {
return count;
}
};
int fun::count = 0;
int main()
{
fun F1,F2,F3,F4;
cout<<"F1 object count: "<<F1.getCount()<<endl;
cout<<"F2 object count: "<<F2.getCount()<<endl;
cout<<"F3 object count: "<<F3.getCount()<<endl;
cout<<"F4 object count: "<<F4.getCount()<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.