14.1 instance and static members

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the first point in understanding static member functions?

Even though static member variables are declared in a class, they are actually defined outside the class declaration. The lifetime of a class's static member variable is the lifetime of the program. This means that a class's static member variables come into existence before any instances of the class are created.

By default what are static member variables set to? What is good practice when it comes to static member variables?

0. To set them to zero so it is clear for other users.

What is the second point in understanding static member functions?

A class's static member functions can be called before any instances of the class are created. This means that a class;s static member function can access the class;s static member variables before any instances of the class are defined in memory. This gives you the ability to create very specialized setup routines for class objects.

How can we think of static member variables and functions?

As belonging to the class instead of the instances of the class.

instance and and static members concept:

Each class object(an instance of a class) has its own copies of the class's instance variables. If a member variable is declared static, however, all instances fo that class have access to that variable. If a member function is declared static, it may be called without any instances of the class being defined.

What is a static member variable?

It does not belong to any instance of a class. A class instance does not have to exist in order for values to be stored in the class's static member variables.

Explain the budget example of using a static member variable.

There were four divisions of a company. We wanted to get each divisions budget. and add them all up into a corporate budget total. So we used a static member variable to hold the total corporate budget. We also used a private member variable to hold each divisions budget. Each budget entered was stored in the private budget variable for each instance of the class. Each of these variables were different. Each divisions budget was added to the static variable. This variable is the same for all instances of the class. So at the end of the program the static variable contained the totals of each division.

Explain what happens in the tree book example.

There were three instances of a class created. We used a static variable to count the instances of the class. The static variables was the same for all three classes when we called getcount.

What happens when a member variable is declared with the key word static?

There will be only one copy of the member variable in memory, regardless of the number of instances of the class that might exist. A single copy of the class's static member variable is shared by all instances of the class.

What is a static member function?

They do not operate on instance variables. They can only operate on static member variables.

What is an instance variable?

When an instance of a class is created, we can use member functions to get the private members values. This is an instance variable. They are called instance variables because each instance of the class has its own copies of the variables.

A function that is a static member of a class cannot?

access any nonstatic member data in its class.

What is the format for declaring a static member variable and where does this go?

static ReturnType FunctionName (parameter list); It goes in the prototype.


संबंधित स्टडी सेट्स

Ethical issues Concept: Professionalism Exemplar: ethical dilemmas

View Set

Hemodialysis & Peritoneal Dialysis

View Set

ICEV - Animal Science Certification

View Set