Question

C++ prgramming - Templates and Static Create a class called fun. Add to the class a...

C++ prgramming - Templates and Static

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.

-Be sure to include a UML​

What do you note about the static variable called count? (Answer the question)

Homework Answers

Answer #1

#include <iostream>
using namespace std; // consider removing this line in serious projects


class fun{
   public:
static int count;
fun(); //creating constructor
int getcount(){
    return count;
}
};

int fun::count = 0;
fun::fun () {
cout<<count++<<endl;
}

int main() {
fun f1;
fun f2;
fun f3;
fun f4;
cout<<f1.getcount()<<endl;
cout<<f2.getcount()<<endl;
cout<<f3.getcount()<<endl;
cout<<f4.getcount()<<endl;
   return 0;
   }

----------------------------------------end of program--------------------------------------------

//output

0
1
2
3
4
4
4
4

-----------------------------end of output-----------------------------------------------------------------------

Explaination:

Whenever we create an instance of a class fun(f1,f2,f3,f4), first the constructor of the class is
called which prints the count value: 0,1,2,3 (for four instances of class).
Now, as we call f1.getcount() , it prints increment value of count i.e. 4.
As we call f2.getcount() , it prints value of count i.e. 4.
Same happens for f3 nd f4.

-----------------------------------------------------------end of explaination-----------------------------------------------------------

Uml: class diagram

FUN()
count:int

fun();

getcount():

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
how to create a class called Cheetah that: inherits from the Animal class. makes use of...
how to create a class called Cheetah that: inherits from the Animal class. makes use of at least one static field which needs to have a static setter and getter. Contains a toString ()method. Has an array as one of it's field. .Create an application class, and within it create a Cheetah object and print it out with the main method
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using the ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. PLEASE CODE THIS IN JAVA
Add a second data member (another int) to the class ObjX. Then in main assign a...
Add a second data member (another int) to the class ObjX. Then in main assign a value to the new variable and print it. class ObjX { // No "static" keyword for either member. int i; void print () { System.out.println ("i=" + i); } } public class DynamicExample { public static void main (String[] argv) { // First create an instance, which allocates space from the heap. ObjX x = new ObjX (); // Now access members via the...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide and clear. Test your calculator by writing a main program incorporating the test code below: Calculator mycalc; mycalc.clear(); mycalc.add(4.52); mycalc.add(3.789); mycalc.divide(2.6); mycalc.multiply(3.12); mycalc.subtract(2.678); cout << mycalc.display() << endl;       // prints out "7.2928" mycalc.clear(); mycalc.add(5.0); cout << mycalc.display() << endl;       // prints out "5" //advanced stuff #1: add a constructor Calculator calc1; cout << calc1.display() << endl;  //prints out 0 //advanced stuff #2: add a parameterized...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT