Question

In: Computer Science

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

Solutions

Expert Solution

Static Variables in Functions:-

#include <iostream>

#include <string>

using namespace std;

void sample()

{

    // static variable

    static int count = 0;

    cout << count << " ";

     

    // value is updated and

    // will be carried to next

    // function calls

    count++;

}

int main()

{

    for (int j=0; j<5; j++)    

sample();

    return 0;

}

OUTPUT:-

0 1 2 3 4 

Static Variables in Class:-

#include<iostream>

using namespace std;

class Sample

{

public:

    static int i;

     

    Sample()

    {

        // Do nothing

    };

};

int Sample::i = 1;

int main()

{

    Sample obj;

    // prints value of i

    cout << obj.i;

}

OUTPUT:-

1

Class objects as static:

#include<iostream>

using namespace std;

class Sample

{

    int r;

    public:

        Sample()

        {

r = 0;

            cout << "Inside Constructor\n";

        }

        ~Sample()

        {

            cout << "Inside Destructor\n";

        }

};

int main()

{

    int y = 0;

    if (y==0)

    {

        Sample obj;

    }

    cout << "End of main\n";

}

OUTPUT:-

Inside Constructor
Inside Destructor
End of main

Static functions in a class

#include<iostream>

using namespace std;

class Sample

{

   public:

     

    // static member function

    static void printMsg()

    {

        cout<<"Welcome to Sample!";

    }

};

// main function

int main()

{

    // invoking a static member function

    Sample::printMsg();

}

OUTPUT:-

Welcome to Sample!

Related Solutions

write a simple program to explain vector type in c++... use comments to explain plz
write a simple program to explain vector type in c++... use comments to explain plz
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
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows: There is a MAZE. There is a mouse inside the maze. The mouse must find the exit from the maze. When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over! User can add as many cats...
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...
Visual C# 2017 Write a program named Eggs that declares four variables of type int to...
Visual C# 2017 Write a program named Eggs that declares four variables of type int to hold the number of eggs produced in a month by each of four chickens, and assign a value to each variable. Sum the eggs, then display the total in dozens and eggs. For example, if the total number of eggs was 95, the output would be 95 eggs is 7 dozen and 11 eggs. Note: For final submission ensure that the total number eggs...
Write a program to demonstrate the use of InetAddress. The program takes a list of names...
Write a program to demonstrate the use of InetAddress. The program takes a list of names or IP addresses as command line parameters and prints the name and an IP address of the local host, followed by names and IP addresses of the hosts specified on the command line. use java program
1 Demonstrate the use of local variables in a Java program that has 1 function 2...
1 Demonstrate the use of local variables in a Java program that has 1 function 2 Demonstrate the use of overloaded methods each has three primitive parameters two methods computes and returns the product to the calling method for printing one method computes and prints the product; does not return a result 3 Demonstrate the use of overloaded constructors Three constructors each initialize two variables Three methods, 1 computes the radius of a circle, 1 computes the area of a...
Using C++, Write a program that will use pointer syntax to access variables, dynamically allocate memories,...
Using C++, Write a program that will use pointer syntax to access variables, dynamically allocate memories, and pass pointers to functions. 1.            The program should ask the user to enter a size to the array. 2.            The program should dynamically allocate an array with the size. 3.            The program should then ask user to input values to the array 4.            The program should then find the maximum, display all elements forward and reversed using two different ways of pointer access...
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT