COSC1315 Fundamentals of Programming

Instructor:  Prof. Richard G. Baldwin

Assignment Number:  08

Object-Based program that illustrates static (class) variables.

Making certain that you comply with the General Requirements, write a C++ program that meets the following specifications:

BEGIN WRITTEN SPECIFICATIONS

This program must use Object-Based Programming Syntax.

Replace the question marks (???) in the following program by the code necessary to produce a program that demonstrates how to use a static (class) variable named classVar belonging to the class named Helper to store your name and then to display your name on the screen.

//Begin program code
#include <iostream>
#include <string>
using namespace std;

class Helper{
public:
  ??? classVar;//See initialization below
};//End Helper class
//-------------------------------------------------------//

class Asg08{ 
  public:
  static void classMain(){
    Asg08* ptrToObject = new Asg08();
    ptrToObject -> doSomething();
  }//End classMain function
  //-----------------------------------------------------//

  void doSomething(){
    ???classVar = "Your name goes here";
    cout << ???classVar << endl;
  }//end doSomething function
};//End Asg08 class
//-------------------------------------------------------//

//Must initialize the static variable outside the class.  
// It is not in scope and cannot be accessed until it is 
// initialized.  You must specify the type of the variable 
// when you initialize it.  In this case, it is initialized
// to contain an empty string.
???classVar = "";

int main(){
  Asg08::classMain();
  return 0;
}//end main
//-------------------------------------------------------//
//End program code

Program Output:

Your name

END WRITTEN SPECIFICATIONS

As specified in the General Requirements, copy the above specifications, including the two lines of upper-case characters and paste the specifications into a comment block at the beginning of your source code file.

Note:  Sometimes when copying C++ source code into HTML documents (like this one) the code will become corrupted.  This is particularly true for code that involves left and right angle brackets such as in:

#include <iostream>
if(x <= y){

Although Prof. Baldwin has made an effort avoid such problems, it is possible that some corruption of source code may have occurred during the creation of these HTML documents.  If you see anything that you believe might be corrupted source code, please notify Prof. Baldwin so that he can look into the problem, correct the code if necessary, and make all of the students aware of the problem.

-end-