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...
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables...
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables x and y, initialize them with different values. b. Use static memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px 2) Dynamic allocation...
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements,...
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements, write a loop, use arrays, obtain input from the user, display output, test and create a JavaDoc. Create project PaystubTest2 that is a Paystub calculator with program Paystub##.java (where ## is your initials) based on user input. (10 points) You must use the ReadStringFromUser and ReadFloatFromUser methods in the program to obtain user input. (20 points) This program will calculate the Gross Earnings, FICA...
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)...
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 the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT