Complete listing of Types01.cpp, Listing 1
/*File: Types01.cpp This C++ program illustrates various data types. Variables of several different types are declared, initialized, and displayed. The program displays the following output on the screen (note that the glyph for the first line of text is not correctly represented by this editor): numCharVar= = shortVar= 32767 intVar= 2147483647 longVar= 2147483647 overflow= -2147483648 charVar= F wideChVar= 66 floatVar= 6.66667 doubleVar= 66.6667 longDblVar= 666.667 boolVar= 1 boolVar*2= 2 stringVar= Hello World Press any key to continue ************************************************/ #include <iostream> #include <string>//Required for type string using namespace std; class Types01{ public: static void classMain(){ //Instantiate an object in dynamic memory // and save its reference in a pointer // variable. Types01* ptrToObject = new Types01(); //Invoke an instance function on the object. ptrToObject -> doSomething(); }//End classMain function //-------------------------------------------// //An instance function of the Types01 class void doSomething(){ //Declare and initialize the values of // several different types of variables. //Integer types char numCharVar = 242; short shortVar = 32767; int intVar = 2147483647; long longVar = 2147483647; //Purposely overflow a long variable. long overflow = 2147483648; //Character types char charVar = 'A';//also a numeric type charVar = charVar + 5;//convert to 'F' wchar_t wideChVar = 'B'; //Floating point types float floatVar = 20/3.0; double doubleVar = 200/3.0; long double longDblVar = 2000/3.0; //The string type string stringVar = "Hello World"; //Boolean types bool boolVar = true; //Displayed as a character, not as a numeric // value cout << "numCharVar= " << numCharVar << endl; //Displayed as numeric values cout << "shortVar= " << shortVar << endl; cout << "intVar= " << intVar << endl; cout << "longVar= " << longVar << endl; cout << "overflow= " << overflow << endl; //Displayed as a character, not as a numeric // value cout << "charVar= " << charVar << endl; //Displayed as a numeric value, not as a // character cout << "wideChVar= " << wideChVar << endl; //All floating types displayed with same // number of significant digits as float. cout << "floatVar= " << floatVar << endl; cout << "doubleVar= " << doubleVar << endl; cout << "longDblVar= " << longDblVar << endl; //Displayed as a numeric value, not as true // or false. cout << "boolVar= " << boolVar << endl; //Arithmetic can be performed on bool // variable. cout << "boolVar*2= " << boolVar*2 << endl; //Displayed as string of characters cout << "stringVar= " << stringVar << endl; }//end doSomething function };//End Types01 class //---------------------------------------------// int main(){ Types01::classMain(); return 0; }//end main Listing 1 |