Question

In: Computer Science

Write a C++ program to show the use of static data member and static member_function. Give...

Write a C++ program to show the use of static data member and static member_function. Give an explanation of the scenario you used to code (e.g. why it is required to use the static members). Upload only a word document file that contains the exact code and its explanation as asked.

also write comments against each line of code

Solutions

Expert Solution

SOLUTION:-

C++ static data member

Sometime when we need a common data member that should be same for all objects then we need static data members. So, any changes in the static data member through one member function will reflect in all other object’s member functions.

Let's take a program to understand it clearly as -

//necessary header file 
#include <iostream.h>
#include <conio.h>
using namespace std;
// create a class Stat
class Stat  
{
        private:
        //static data member declaration        
                static int X;
    public:
        //static member function
                static void fun()
                {
                        cout <<"Value of X: " << X << endl;
                }
};

//definition of static data member, it should be always outside the class
int Stat :: X = 10;
//main function to execute the code 
int main()
{
    //object of class X
        Stat X;
    //accessing the static data member using static member function 
        X.fun();
        
        return 0;
} 

OUTPUT : Value of X: 10

Case2: Static data member can also be accessed through the class name without using the static member function

#include <iostream>
#include <conio.h>
using namespace std;
class Stat
{
        public: 
                static int X;
};
//definition of static data member, it should be always outside the class
int Stat :: X =20;

int main()
{
    //here we need an Scope Resolution Operator (SRO) :: to access the static data member 
    //without static member function.       
    cout<<"\nValue of X: "<<Stat::X;
        return 0;
}

OUTPUT : Value of X: 20


Related Solutions

write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
write a program in java that contain a class for botique . data member include code...
write a program in java that contain a class for botique . data member include code , color , size , quantity . your class should contains all accessor and mutator methods , non paraqmetric constructor , parametric constructor , input andvidsplay method
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT