bartleby

Concept explainers

bartleby

Videos

Textbook Question
Chapter 11, Problem 1RQE

If a member variable is declared _____, all objects of that class share that variable.

Expert Solution & Answer
Check Mark
Program Description Answer

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”.
Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
__________________class members are accessible anywhere an object of the class is in scope
If a member variable is declared __________ , all objects of that class share that variable.
True or FalseA base class reference variable can reference an object of any class that is derived from the base class.

Chapter 11 Solutions

Starting Out With C++: Early Objects, Student Value Edition & Myprogramminglab With Pearson Etext -- Standalone Access Card Package, 9/e

Ch. 11.5 - When is a copy constructor called?Ch. 11.5 - How does the compiler know that a member function...Ch. 11.5 - What action is performed by a classs default copy...Ch. 11.6 - Assume there is a class named Pet. Write the...Ch. 11.6 - Assume that dog and cat are instances of the Pet...Ch. 11.6 - What is the disadvantage of an overloaded ...Ch. 11.6 - Prob. 11.17CPCh. 11.6 - Prob. 11.18CPCh. 11.6 - Assume there is a class named Animal, which...Ch. 11.6 - Prob. 11.20CPCh. 11.6 - Describe the values that should he returned from...Ch. 11.6 - Prob. 11.22CPCh. 11.6 - What type of object should an overloaded operator...Ch. 11.6 - Prob. 11.24CPCh. 11.6 - If an overloaded or operator accesses a private...Ch. 11.6 - Prob. 11.26CPCh. 11.6 - When overloading a binary operator such as or as...Ch. 11.6 - Explain why overloaded prefix and postfix and ...Ch. 11.6 - Prob. 11.29CPCh. 11.6 - Overload the function call operator ( ) (int i,...Ch. 11.8 - Prob. 11.31CPCh. 11.8 - How is the type declaration of an r value...Ch. 11.8 - Prob. 11.33CPCh. 11.8 - Prob. 11.34CPCh. 11.8 - Which operator must be overloaded in a class...Ch. 11.8 - Prob. 11.36CPCh. 11.10 - What arc the benefits of having operator functions...Ch. 11.10 - Prob. 11.38CPCh. 11.10 - Assume that there is a class named BlackBox. Write...Ch. 11.10 - Assume there are two classes, Big and Smal1.Write...Ch. 11.13 - What type of relationship between classes is...Ch. 11.13 - Why does it make sense to think of a base class as...Ch. 11.13 - What is a base class access specification?Ch. 11.13 - Think of an example of two classes where one class...Ch. 11.13 - What is the difference between private members and...Ch. 11.13 - What is the difference between member access...Ch. 11.13 - Suppose a program has the following class...Ch. 11.14 - What is the reason that base class constructors...Ch. 11.14 - Why do you think the arguments to a base class...Ch. 11.14 - Passing arguments to base classes constructors...Ch. 11.14 - What will the following program display? #include...Ch. 11.14 - What will the following program display? #include...Ch. 11 - If a member variable is declared _____, all...Ch. 11 - Static member variables are defined _____ the...Ch. 11 - A(n) _____ member function cannot access any...Ch. 11 - A static member function may be called _____ any...Ch. 11 - A(n) _____ function is not a member of a class,...Ch. 11 - A(n) _____ tells the compiler that a specific...Ch. 11 - _____ is the default behavior when an object is...Ch. 11 - A(n) _____ is a special constructor, called...Ch. 11 - _____ is a special built-in pointer that is...Ch. 11 - An operator may be _____ to work with a specific...Ch. 11 - When the _____ operator is overloaded, its...Ch. 11 - Making an instance of one class a member of...Ch. 11 - Object composition is useful for creating a(n)...Ch. 11 - A constructor that takes a single parameter of a...Ch. 11 - The class Stuff has both a copy constructor and an...Ch. 11 - Explain the programming steps necessary to make a...Ch. 11 - Explain the programming steps necessary to make a...Ch. 11 - Consider the following class declaration: class...Ch. 11 - Describe the difference between making a class a...Ch. 11 - What is the purpose of a forward declaration of a...Ch. 11 - Explain why memberwise assignment can cause...Ch. 11 - Explain why a classs copy constructor is called...Ch. 11 - Explain why the parameter of a copy constructor...Ch. 11 - Assume a class named Bird exists. Write the header...Ch. 11 - Assume a class named Dollars exists. Write the...Ch. 11 - Assume a class named Yen exists. Write the header...Ch. 11 - Assume a class named Length exists. Write the...Ch. 11 - Assume a class named Collection exists. Write the...Ch. 11 - Explain why a programmer would want to overload...Ch. 11 - Each of the following class declarations has...Ch. 11 - A derived class inherits the _____ of its base...Ch. 11 - The base class named in the following line of code...Ch. 11 - The derived class named in the following line of...Ch. 11 - In the following line of code, the class access...Ch. 11 - In the following line of code, the class access...Ch. 11 - Protected members of a base class are like _____...Ch. 11 - Complete the following table by filling in...Ch. 11 - Complete the following table by filling in...Ch. 11 - Complete the following table by filling in...Ch. 11 - When both a base class and a derived class have...Ch. 11 - When both a base class and a derived class have...Ch. 11 - An overridden base class function may be called by...Ch. 11 - Each of the following class declarations and/or...Ch. 11 - Soft Skills 44. Your companys software is a market...Ch. 11 - Check Writing Design a class Numbers that can be...Ch. 11 - Day of the Year Assuming that a year has 365 days,...Ch. 11 - Day of the Year Modification Modify the DayOfYear...Ch. 11 - Number of Days Worked Design a class called...Ch. 11 - Palindrome Testing A palindrome is a string that...Ch. 11 - Prob. 6PCCh. 11 - Corporate Sales A corporation has six divisions,...Ch. 11 - Prob. 8PCCh. 11 - Rational Arithmetic II Modify the class Rational...Ch. 11 - HTML Table of Names and Scores Write a class whose...Ch. 11 - Prob. 11PC

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • True/False: a member function in a class can access all of its class's member variables, but not if the variables are private.   A) True  B) False
    Write a class named Coin . The Coin class should have the followingmember variable:A string named sideUp . The sideUp member variable will holdeither “heads” or “tails” indicating the side of the coin that isfacing up.The Coin class should have the following member functions:A default constructor that randomly determines the side of thecoin that is facing up (“heads” or “tails”) and initializes the sideUpmember variable accordingly.A void member function named toss that simulates the tossingof the coin. When the toss member function is called, itrandomly determines the side of the coin that is facing up(“heads” or “tails”) and sets the sideUp member variableaccordingly.A member function named getSideUp that returns the value ofthe sideUp member variable.Write a program that demonstrates the Coin class. The programshould create an instance of the class and display the side that isinitially facing up. Then, use a loop to toss the coin 20 times. Eachtime the coin is tossed, display the…
    Employee and ProductionWorker ClassesCreate an Employee class that has properties for the following data:Employee nameEmployee numberNext, create a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have properties to hold the following data:Shift number (an integer, such as 1, 2, or 3)Hourly pay rateThe workday is divided into two shifts: day and night. The Shift property will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2.Create an application that creates an object of the ProductionWorker class and lets the user enter data for each of the object’s properties. Retrieve the object’s properties and display their values.    this.ReportViewer1.Employee(); cannot reference?
  • Circle Class (Easy)   Write a Circle class that has the following member variables: radius : a  double    The class should have the following member functions:    Default Constructor:  default constructor that sets radius to 0.0.  Constructor: accepts the radius of the circle as an argument.  setRadius: an mutator function for the radius variable. getRadius: an accessor function for the radius variable. getArea: returns the area of the circle, which is calculated as area = pi * radius * radius    getCircumference: returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius         Step1:  Create a declaration of the class.   Step2: Write a program that demonstrates the Circle class by asking the user for the circle’s radius, creating              Circle objects, and then reporting the circle’s area, and circumference.                You should create at least two circle objects, one sets the radius to 0.0 and one accepts the radius as an…
    Write base class with the name Shape . The Shape class have Private Variable: area, a double used to hold the shape's area. Public Member Functions: getArea the Accessor. This function should return the value in the member variable area. Parameterized constructor—Initializes area member. Defualt constructor— Empty body calcArea. This function should be a  incomplete function.Next, define a class named Triangle . It should be child of Shape class. It should have the following members:Protected Member Variables: base, a  integer used to hold the base of triangle.Height, a integer used to hold the height of the triangle. Public Member Functions: constructor—Initializes values for base, height. The overridden calcArea function in Circle described below.calcArea—calculates the area of the Triangle (1/2* base * height) and stores the result in the inherited member area.After you have created these classes, create a driver program that defines a Triangle object. Demonstrate that each object…
    True/False: a member function in a class can access all of its class's member variables, but not if the variables are protected.
  • Design a class named Month. The class should have the following private members:    • name - A string object that holds the name of a month, such as "January", "February", etc.      • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.     In addition, provide the following member functions:   • A default constructor that sets monthNumber to 1 and name to "January."   • A constructor that accepts the name of the month as an argument. It should set name to the value passed as the argument and set monthNumber to the correct value.   • A constructor that accepts the number of the month as an argument. It should set monthNumber to the value passed as the argument and set name to the correct month name.   • Appropriate set and get functions for the name and monthNumber member variables.   • Prefix and postfix overloaded ++ operator functions that increment…
    # Pets## Breed ClassCreate a `Breed` class with the following: ### Member VariablesCreate the following private member variables, all of type `std::string`: 1. `species_`2. `breed_name_`3. `color_` ### Constructors1. Create a default constructor for `Breed` that sets its `species_` to `"Dog"`, `breed_name_` to `"Chihuahua"`, and `color_` to `"Fawn"`.2. Create a non-default constructor that receives a `std::string` for `species_`, `breed_name_`, and `color_`; in that order. The values from the constructor should appropriately assign the member variables. ### Accessors and MutatorsCreate accessors and mutators for all member variables, following the naming conventions covered in class. e.g. for species_, name the accessor `Species`, and the mutator `SetSpecies`. ## Pet ClassCreate a `Pet` class with the following: ### Member VariablesCreate the following private member variables:1. `std::string name_`2. `Breed breed_` 3. `double weight_` ### Constructors1. Create a default constructor…
    Movie Information   Write a class named MovieInfo that holds data about movie. The class should have the following member variables: title: hold movie’s name category: hold movie’s category year: hold released year mins: hold running time in mins. Write a constructor that accepts arguments for each member variable, appropriate mutator functions that store values in these member variables, and accessor functions that return the values in theses member variables. Once you have written the class, write a separate program that creates four MovieInfo objects and stores the following data in them. Here is the information of four MovieInfo objects: moive1, moive2, moive3, moive4   movie1 title The Book of Life category Animation |Adventure | Comedy year 2014 mins 95     moive2 title Roman Holiday category Comedy | Drama | Romance year 1953 mins 118     movie3 title Kung Fu Panda category Animation | Action…
  • Movie Information   Write a class named MovieInfo that holds data about movie. The class should have the following member variables: title: hold movie’s name category: hold movie’s category year: hold released year mins: hold running time in mins. Write a constructor that accepts arguments for each member variable, appropriate mutator functions that store values in these member variables, and accessor functions that return the values in theses member variables. Once you have written the class, write a separate program that creates four MovieInfo objects and stores the following data in them. Here is the information of four MovieInfo objects: moive1, moive2, moive3, moive4   movie1 title The Book of Life category Animation |Adventure | Comedy year 2014 mins 95     moive2 title Roman Holiday category Comedy | Drama | Romance year 1953 mins 118     movie3 title Kung Fu Panda category Animation | Action…
    Employee and ProductionWorker Classes Create an Employee class that has properties for the following data: Employee name Employee number Next, create a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have properties to hold the following data: Shift number (an integer, such as 1, 2, or 3) Hourly pay rate The workday is divided into two shifts: day and night. The Shift property will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Create an application that creates an object of the ProductionWorker class and lets the user enter data for each of the object’s properties. Retrieve the object’s properties and display their values.
    Write a class named Patient that has member variables for first name, middle name, last name, phone number, name and phone number of emergency contact. The Patient class should have a constructor that accepts and argument for each member variable. The Patient class should also have accessor and mutator functions for each member variable. Write a class named Procedure that has member variables for name of procedure, date of procedure, name of physician, and charges for the procedure. This Procedure class represents a medical procedure that is performed on a patient. The Procedure class should have a constructor that accepts an argument for each member variable. The Procedure class should also have accessor and mutator functions for each member variable. Then write a program that creates an instance of the Patient class initialized with sample data that is entered by the user. Then, create an instance of the Procedure class for each procedure that is being performed on the patient. There…
    • SEE MORE QUESTIONS
    Recommended textbooks for you
  • Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
    Programming Logic & Design Comprehensive
    Computer Science
    ISBN:9781337669405
    Author:FARRELL
    Publisher:Cengage
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
  • Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
    Programming Logic & Design Comprehensive
    Computer Science
    ISBN:9781337669405
    Author:FARRELL
    Publisher:Cengage
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY