180 likes | 306 Views
159.234 Lecture 4. C++ programs, enum, const, formatting Bibliography: Textbook p.30-34,37(const), 38, 383, browse p. 414-417 Programs: .. programsp03. Declaring Variables. C++ allows declarations anywhere.
E N D
159.234 Lecture 4 C++ programs, enum, const, formatting Bibliography: • Textbook p.30-34,37(const), 38, 383, • browse p. 414-417 Programs: ..\programs\p03
Declaring Variables • C++ allows declarations anywhere. • The variable exists from that point to the end of the enclosing block. • for (int j=0; j<10; ++j) { • cout << j; • int k = j; • cout << k; • } • j exists while the loop is executing • (and maybe longer depending on compiler settings). • k exists from its declaration to the end of the loop.
Declaring Variables for (int j=0;j<10;++j) { cout << j; int k = j * 2; cout << k; } cout << k;// error since k is no longer in scope
#include <iostream> #include <fstream> using namespace std; int main(){ double d = 1.0; { // this block has its own variable scope double d = 0.12345678901234567890123456789; cout << d << endl; } // i is declared for the loop block scope only for( int i=0;i<4;i++){ cout << i << endl; } // cout << i << endl; // error, i no longer in scope return 0; } Example output: 0.123457 0 1 2 3
Programstructure //comments here #include <file1.h> #include <file2.h> //------variables here--------------- #define VAR5 const global variable //------function prototypes--------------- void func1(int, int); int func2(); //---------------------------------- int main(){ //code here. . .//. . . return 0; } //--------function definitions here---------- void func1(int n1, int n2){ //function body here. . .} //---------------------------------- int funct2(){ // code here return 5*20; }
C++ Types All types from C and some new types Two new simple types:bool and wchar_t Boolean variables can represent eithertrue or false. The type wchar_tis a 'wide' character type designed to allow alphabets with many characters.
C++ Types Boolean variables:. bool test; //declaration test = true; //assigning values test= false; test = (x < 5); bool, true and false are keywords-not allowed as identifiers.
Enumeration enum day {sun,mon,tue,wed,thu,fri,sat}; defines a new type day. Use this to create variables that can store days: day d1,d2; d1 = tue; d2 = d1; if (d2 == wed) { ... }
Enumeration Enumerated types allow the compiler to check the values associated with them. d1 = 1; // will produce a warning enum is implemented using integers. In enum day {sun,mon,tue,wed,thu,fri,sat}; sun is represented by 0mon by 1 etc.
Enumeration There is no easy way to print out enumerated types. switch (d1) { case sun: cout << "Sunday"; break; case mon: ... }
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main(){ enum daysOfWeek{ mon, tue, wed, thu, fri, sat, sun }; const char *daynames[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; for(int day = 0; day < 7; day++ ){ // spot the deliberate mistake cout << "Day " << day << " is " << daynames << endl; } // daynames with no brackets is a pointer cout << endl; for(int day = mon; day<=sun; day++){ cout << "Day " << day << " is " << daynames[day] << endl; } return 0; } Example Output: Day 0 is 0xbffffc40 Day 1 is 0xbffffc40 Day 2 is 0xbffffc40 Day 3 is 0xbffffc40 Day 4 is 0xbffffc40 Day 5 is 0xbffffc40 Day 6 is 0xbffffc40 Day 0 is Monday Day 1 is Tuesday Day 2 is Wednesday Day 3 is Thursday Day 4 is Friday Day 5 is Saturday Day 6 is Sunday
Using const C way: #define MAX 400 C++ way of avoiding the pre-processor is by using const: const int MAX = 4000; MAX is a non-changeable integer variable. You can initialise constants but not assign to them.
V a l u e i s 3 . 1 4 Formatting To format the output we use manipulators and functions. cout << setprecision(6) << sqrt(2.0) <<endl; The output is 1.41421 cout <<“Value is”<< setw(7) << 3.14 <<endl; The output is
Formatting In <iostream.h> cout.setf(ios::left); cout.unsetf(ios::left);
Formatting In <iomanip.h> cout<<setfill(‘&’); cout<<setfill(‘ ’); //restore the default fill char
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main(){ const double d = 0.12345678901234567890123456789; cout << d << endl; // default precision is only about 6 digits cout << setprecision(10) << d << endl; cout << setprecision(30) << d << endl; cout << '|' << setprecision(16) << setw(22) << d << '|' << endl; cout.setf(ios::left); cout << '|' << setprecision(16) << setw(22) << d << '|' << endl; cout << setfill('#') << '|' << setprecision(16) << setw(22) << d << '|' << endl; const int two12 = 4096; const int two24 = two12 * two12; cout << setw(30) << two24 << endl; cout << setfill(' '); cout << setbase(16) << two24 << endl; cout << setbase(8) << two24 << endl; char ch; cout << "Enter a character (followed by return):"; cout << "Character was: \"" << ch << "\"" << endl; cin >> ch; cout << "Enter a character (followed by return):"; cin >> skipws >> ch; cout << "Character was: \"" << ch << "\"" << endl; } Example Output: 0.123457 0.123456789 0.12345678901234568 | 0.1234567890123457| |0.1234567890123457 | |0.1234567890123457####| 16777216###################### 1000000 100000000 Enter a character (followed by return):Character was: "" x Enter a character (followed by return): x Character was: "x"
Summary Variables can be declared anywhere in a C++ program, close to the places they are needed. Using enumerated types and const variables are prefered to #define as the compiler can check the variables type matchings for us (safety).
Next Expressions and operators C++ functions-default arguments -overloading -inline Bibliography Textbook, pg 65-74