Concept explainers
If a member variable is declared _____, all objects of that class share that variable.
If a member variable is declared as a “static”, then all objects of that class share have access to that member variable.
Explanation of Solution
Static member variable:
In object-oriented programming, the member variable is declared with the keyword of “static” is referred as static member variable.
- In memory, it allocates only one copy of the static member variable for that class.
- If changes are made to static variable, then it reflects to all other instances of that class.
- The static member variables can be re-declared and reuse outside the class using the scope resolution operator (::).
- The static member variables are used anywhere in the program. Even though it is declared outside or inside of the class.
Example:
Consider the example of static member variable declaration is as follows:
//Header file
#include<iostream>
using namespace std;
//class definition
class sample
{
//declaration of instance variable
int c;
//declaration of static variable
static int ct;
//access specifier
public:
//constructor
sample()
{
//increment the variable
c = ++ct;
}
//function definition
void show_code()
{
//display the output
cout << "Object number is: " << c << endl;
}
//static member function definition
static void show_count()
{
//display the output
cout << "The number of objects in the program: " << ct<< endl;
}
};
//definition of static member variable "ct"
int sample::ct=0;
//definition of main method
int main()
{
//Create two Objects for "sample" class
sample o1, o2;
//call the functions
o1.show_count();
o1.show_code();
o2.show_count();
o2.show_code();
//return statement
return 0;
}
Explanation:
- Here, the variable “ct” is declared as a static member variable in the class “sample”.
- In main() function,
- Create two objects for “sample”class.
- Call the show_count() function by using two objects such as “o1” and “o2”.
- Call the show_code() function by using two objects such as “o1” and “o2”.
- Therefore, all the objects of the class have rights to access the static member variable “ct”.
Output:
The number of objects in the program: 2
Object number is: 1
The number of objects in the program: 2
Object number is: 2
Want to see more full solutions like this?
Chapter 11 Solutions
Starting Out with C++: Early Objects (9th Edition)
Additional Engineering Textbook Solutions
SURVEY OF OPERATING SYSTEMS
Electric Circuits. (11th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Concepts Of Programming Languages
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
- Find the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forward
- What are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forwardShould software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forward
- I need help to resolve the following activityarrow_forwardModern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning