answersLogoWhite

0


Best Answer

Global data are used only as a last resort. It's highly recommended not use global data in your programs because sometimes it's really hard to avoid name clashing. And as result to track such problem down.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why use static data when you can use global data in the class in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What may be different for all objects in a class and what remains the same in C plus plus programming?

The only things that may be different for all objects of a class are their member variables. They represent the object's data. The only things that remain the same are the static members -- they are akin to global variables, but are local to all objects of the class type.


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C. C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class. Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What is storage classes in c plus plus?

AUTO EXTERN STATIC are the storage classes in c++

Related questions

What may be different for all objects in a class and what remains the same in C plus plus programming?

The only things that may be different for all objects of a class are their member variables. They represent the object's data. The only things that remain the same are the static members -- they are akin to global variables, but are local to all objects of the class type.


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C. C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class. Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What is storage classes in c plus plus?

AUTO EXTERN STATIC are the storage classes in c++


How can you hide the data in a class in C plus plus?

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.To hide the data in a class in C++, simply declare the data private, and then manipulate the data using the public interface of the class.


What is static in c plus plus programming?

The keyword "static" in C has a slightly complicated meaning, depending on where it is used. It generally denotes static storage, meaning that it is allocated in the static data area of the program, and not on the stack or heap. Also, it generally limits the visibility of the name to a certain area of the code. If used on a variable or function of global scope, then it generally means that the function or variable can only be used by other functions within the same file, as opposed to "extern" which means that the variable and function can be used from other files. If used on a local variable within a function, it means that the variable maintains its value between function calls (since it is allocated statically and not on the stack).


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


What is data storage in c plus plus?

A storage class is not a class as such, it is a modifier. C++ provides several built-in storage classes: auto, register, static, extern and mutable. The auto storage class is implied for all function local variables and it can only be used in functions. Since it is implied it is rarely used. However, with C++11, the auto keyword has an alternative use, allowing unambiguous types to be deduced without the need to explicitly declare their type. This greatly simplifies code where the type is of little importance to the reader, and is particularly useful when iterating through an STL container as it greatly simplifies and shortens the declaration of the for statement that controls the loop. The register storage class can be used for any variable that will fit in a CPU register. Since register variables do not exist in RAM, they have no address. Declaring a register variable is not a guarantee that the variable will be stored in a register, only that it might be. Simple variables such as counters are good candidates for register variables, however your compiler should be able to automatically determine when to use a register rather than RAM for a variable. The static storage class ensures that a variable remains in memory at all times. This does not mean the variable is accessible at all times, however, since visibility is ultimately determined by the variable's scope. However, when you modify a static variable that is in scope, it will maintain that value even when it falls from scope (static variables are never destroyed). All static variables must be initialised with a value and each time the program is run, they will default to the initial value. Class member functions as well as variables can be declared static. Static members of a class are local to the class, rather than to an instance of the class. They are generally used to provide internal class functionality or information that is common to all instances of the class (much like global functions and variables, but scoped to the class). Public static members are also accessible outside of the class, even when no instances of the class exist. Extern provides external linkage to a global variable that is defined in another file. This is typically used when two or more source files share the same global variable. One file defines the global while all others declare it external. The mutable storage class is only used in classes, and allows the member to override the constness of the class. That is, when calling constant member functions, the class' mutable members can be modified while the immutable (normal) members remain constant. Mutable members are typically used internally be the class, such that the external "state" of the class can remain constant.


What are destructors in c plus plus?

A destructor in C++ is a method of a class that runs when the class is deleted. It performs cleanup of the members of the class that need cleanup, such as deallocation of subobjects referred to by pointers in the class, subobjects which were earlier allocated by a constructor or other method of the class.


What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


What is the difference between class data type and basic type in c plus plus .How can someone know which one is class type and which one is basic type.?

Basic types (primitive data types) have no methods associated with them.