Варіант - 8
1.Language Basics
1.6.5.
| Using the scope resolution operator: ':: '
|
#include < iostream> using std:: cout; using std:: endl;
int count1 = 100;
int main() { int count1 = 10; int count3 = 50; cout < < endl < < " Value of outer count1 = " < < count1;
// Виведення результату значення outer count1 cout < < endl < < " Value of global count1 = " < <:: count1; // Виведення результату значення global count1 { int count1 = 20; int count2 = 30; cout < < endl < < " Value of inner count1 = " < < count1;
// Виведення результату значення inner count1 cout < < endl < < " Value of global count1 = " < <:: count;
// Виведення результату значення global count1 count3 += count2; }
cout < < endl < < " Value of outer count1 = " < < count1 // Виведення результату значення outer count1 < < endl < < " Value of outer count3 = " < < count3; // Виведення результату значення outer count3 cout < < endl; return 0; }
1.6.6.
| global and block scope
|
#include < iostream.h>
int n=0; //Global
main() { int n = 1; { int n = 2; { int n = 3; cout < < " In inner n=" < < n< < endl;
// Виведення результату значення inner cout < < " Global n=" < <:: n < < endl;
// Виведення результату значення Global } cout < < " In outter n=" < < n< < endl;
// Виведення результату значення outter cout < < " Global n=" < <:: n< < endl; } cout < < " In main() n=" < < n< < endl; return 0; }
#include < iostream>
using namespace std;
void func();
int main() { int var = 5; cout < < " In main() var is: " < < var < < " \n\n";
func();
cout < < " Back in main() var is: " < < var < < " \n\n"; { cout < < " In main() in a new scope var is: " < < var < < " \n\n"; cout < < " Creating new var in new scope.\n"; int var = 10; cout < < " In main() in a new scope var is: " < < var < < " \n\n"; }
cout < < " At end of main() var is: " < < var < < " \n";
return 0; }
void func() { int var = -5; // локальна змінна в func() cout < < " In func() var is: " < < var < < " \n\n"; }
2.Data Types
2.4.3.
| Find the minimum and maximum of a set of values
|
#include < iostream> using namespace std; int main() { int i, avg, min_val, max_val; int nums[10]; nums[0] = 13; nums[1] = 18; nums[2] = 75; nums[3] = 120; nums[4] = 1321; nums[5] = 56; nums[6] = 13124; nums[7] = 12123; nums[8] = -19312; nums[9] = 88123; // знайти мінімальне і максимальне значення min_val = max_val = nums[0]; for (i=1; i< 10; i++) { if (nums[i] < min_val) min_val = nums[i]; if (nums[i] > max_val) max_val = nums[i]; } cout < < " Minimum value: " < < min_val < < '\n'; cout < < " Maximum value: " < < max_val < < '\n'; return 0; }
| 2.5.1.
| Displaying Leading Zeros
|
#include < iomanip> #include < iostream>
using namespace std;
int main() { const int num_members = 6; const int id[num_members] = { 6, 5, 1, 8, 3, 2 }; const int month[num_members] = { 9, 1, 1, 12, 10, 4 }; const int day[num_members] = { 2, 1, 13, 30, 31, 4 }; const int year[num_members] = { 2000, 2003, 2004, 1998, 2001, 2003 };
cout < < setfill('0'); for (int i = 0; i < num_members; ++i) cout < < ": " < < setw(8) < < id[i] < < ": " < < setw(2) < < month[i] < < " /" < < setw(2) < < day[i] < < " /" < < setw(2) < < year[i] % 100 < < endl; }
2.5.2.
| Change width as you output
|
#include < iostream> #include < iomanip> using namespace std; int main(void){ cout < < setw(5) < < 1 < < '\n' < < setw(6) < < 2; cout < < '\n' < < setw(7) < < 3; }
3.Operators statements
3.6.1.
| Demonstrate sizeof.
|
#include < iostream> using namespace std; int main() { char ch; int i; cout < < sizeof ch < < ' '; // розмір змінної char cout < < sizeof i < < ' '; // розмір змінної int cout < < sizeof (float) < < ' '; // розмір змінної float cout < < sizeof (double) < < ' '; // розмір змінної double return 0; }
#include< iostream.h>
union u_tag{ int i; double d; }u={88}; struct s_tag{ int i; double d; }s={66, 1.234};
int main() { int size; size=sizeof(union u_tag); cout< < " sizeof(union u_tag)=" < < size< < endl; u.i=100; cout< < " u.i=" < < u.i< < endl; u.d=1.2345; cout< < " u.d=" < < u.d< < endl; size=sizeof(u.d); cout< < " sizeof(u.d)=" < < size< < endl; cout< < " s.i=" < < s.i< < endl; cout< < " s.d=" < < s.d< < endl; size=sizeof(struct s_tag); cout< < " sizeof(struct s_tag)=" < < size< < endl; }
#include < iostream>
class EmptyClass { };
int main() { std:: cout < < " sizeof(EmptyClass): " < < sizeof(EmptyClass) < < '\n'; }
4 Array
#include < iostream> using namespace std; int main() { int i, j; int sqrs[10][2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; cout < < " Enter a number between 1 and 10: "; cin > > i; // шукати i for (j=0; j< 10; j++) if (sqrs[j][0]==i) break; cout < < " The square of " < < i < < " is "; cout < < sqrs[j][1]; return 0; }
4.4.3.
| Creating A Multidimensional Array
|
#include < iostream> int main() { int a[5][2] = { {0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}; for (int i = 0; i< 5; i++) for (int j=0; j< 2; j++) { std:: cout < < " a[" < < i < < " ][" < < j < < " ]: "; std:: cout < < a[i][j]< < std:: endl; } return 0; }
4.4.4.
| Initializing multidimensional arrays
|
#include < iostream> using std:: cout; using std:: endl;
void display(const int [][ 3 ]);
int main() { int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
cout < < " Values in array1 by row are: " < < endl; display(array1);
cout < < " \nValues in array2 by row are: " < < endl; display(array2);
cout < < " \nValues in array3 by row are: " < < endl; display(array3); return 0; }
void display(const int a[][ 3 ]) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++){ cout < < a[ i ][ j ] < < ' '; } cout < < endl; } }
5.Development
5.1.22.
| Character input with member function getline.
|
#include < iostream> using namespace std; int main() { const int SIZE = 80; char buffer[ SIZE ];
cout < < " Enter a sentence: \n"; cin.getline(buffer, SIZE);
cout < < " \nThe sentence entered is: \n" < < buffer < < endl; return 0; }
5.1.23.
| Is it a bad input
|
#include < iostream> #include < string> #include < cmath>
using namespace std;
int main() { double area; cout < < " Please enter the area of a square: "; cin > > area;
if (cin.fail()) { cout < < " Error: Bad input\n"; return 1; }
if (area < 0) { cout < < " Error: Negative area.\n"; return 1; }
cout < < " The side length is " < < sqrt(area) < < " \n";
return 0; }
5.2.1.
| parameterless manipulators that operate on output streams
|
#include < iostream> using namespace std; void showflags(void);
int main(void) { showflags(); cout.setf(ios:: right | ios:: showpoint | ios:: fixed); showflags(); }
void showflags(void){ long flag_set, i; int j; char flags[15][12] = { " skipws", " left", " right", " internal", " dec", " oct", " hex", " showbase", " showpoint", " uppercase", " showpos", " scientific", " fixed", " unitbuf", };
flag_set = cout.flags(); for (i=1, j=0; i< 0x2000; i = i< < 1, j++) if (i & flag_set) cout < < flags[j] < < " is on." < < endl; else cout < < flags[j] < < " is off." < < endl; cout < < endl; }
6.Exceptions
| 6.5.1.
| Throw your own exception class based on runtime_error
|
#include < iostream> #include < stdexcept> using std:: cin; using std:: cout; using std:: endl; using std:: runtime_error;
class DivideByZeroException: public runtime_error { public: DivideByZeroException:: DivideByZeroException(): runtime_error(" attempted to divide by zero") {} };
double quotient(int numerator, int denominator) { throw DivideByZeroException(); // припинити функцію
return 0; }
int main() { try { double result = quotient(1, 1); cout < < " The quotient is: " < < result < < endl; } catch (DivideByZeroException & divideByZeroException) { cout < < " Exception occurred: " < < divideByZeroException.what() < < endl;
}
return 0; }
6.5.2.
| Custom exception class
|
#include < iostream> #include < string>
using namespace std;
class Exception {
public: Exception(const string& msg): msg_(msg) {} ~Exception() {}
string getMessage() const { return (msg_); } private: string msg_; };
void f() { throw (Exception(" Mr. Sulu")); }
int main() {
try { f(); } catch (Exception& e) { cout < < " You threw an exception: " < < e.getMessage() < < endl; } }
6.5.3.
| Throw a custom exception object
|
#include < iostream>
using std:: cout; using std:: endl;
class Trouble { public: Trouble(const char * pStr = " There's a problem"): pMessage(pStr) {} const char * what() const { return pMessage; }
private: const char * pMessage; };
int main() { for (int i = 0; i < 2; i++) { try { if (i == 0) throw Trouble(); else throw Trouble(" Nobody knows the trouble I've seen..."); } catch (const Trouble& t) { cout < < endl < < " Exception: " < < t.what(); } } return 0; }
7.Function
7.3.8.
| Use array as function's parameter
|
#include < iostream.h>
float Total(float a[], int num);
const int SIZE = 10;
main() { float * f = new float [SIZE];
for (int i=0; i< SIZE; i++) f[i]=i+i; //*(f+i) cout < < " Sum = " < < Total(f, SIZE) < < endl;
cout < < " Average = " < < Total(f, SIZE)/SIZE; delete []f; return 0; }
float Total(float a[], int num) { int i; float sum = 0; for (i=0; i< num; i++) sum += a[i]; return sum; }
| 7.3.9.
| Change the contents of an array using a function
|
#include < iostream> using namespace std; void f(int *n, int num); int main() { int i, nums[10]; for (i=0; i < 10; i++) nums[i] = i+1; cout < < " Original contents: "; for (i=0; i < 10; i++) cout < < nums[i] < < ' '; cout < < '\n'; f(nums, 10); // обчислити куби cout < < " Altered contents: "; for (i=0; i< 10; i++) cout < < nums[i] < < ' '; return 0; }
void f(int *n, int num) { while (num) { *n = *n * *n; num--; n++; } }
7.3.10.
| Pass a string to a function: Invert the case of the letters within a string
|
#include < iostream> #include < cstring> #include < cctype> using namespace std; void f(char *str); int main() { char str[80]; strcpy(str, " ABCD"); f(str); cout < < str; return 0; } void f(char *str) { while (*str) { if (isupper(*str)) *str = tolower(*str); else if (islower(*str)) *str = toupper(*str); str++; } }
8.Structure
9.34.5.
| Pointers to Class Members
|
#include < iostream> using namespace std; class MyClass { public: MyClass(int i) { val=i; } int val; int double_val() { return val+val; } }; int main() { int MyClass:: *data; // Покажчик даних int (MyClass:: *func)(); // покажчик функції MyClass ob1(1), ob2(2); // створювати об'єкти data = & MyClass:: val; // отримати зсув func = & MyClass:: double_val; // get offset of double_val() cout < < " Here are values: "; cout < < ob1.*data < < " " < < ob2.*data < < " \n"; cout < < " Here they are doubled: "; cout < < (ob1.*func)() < < " "; cout < < (ob2.*func)() < < " \n"; return 0; }
| 9.34.6.
| To use a pointer to the object, you need to use the -> * operator
|
#include < iostream> using namespace std; class MyClass { public: MyClass(int i) { val=i; } int val; int double_val() { return val+val; } }; int main() { int MyClass:: *data; // Покажчик даних int (MyClass:: *func)(); // покажчик функції MyClass ob1(1), ob2(2); // створювати об'єкти MyClass *p1, *p2; p1 = & ob1; // доступ до об'єктів через покажчик p2 = & ob2; data = & MyClass:: val; // отримати зсув func = & MyClass:: double_val;
() cout < < " Here are values: "; cout < < p1-> *data < < " " < < p2-> *data < < " \n"; cout < < " Here they are doubled: "; cout < < (p1-> *func)() < < " "; cout < < (p2-> *func)() < < " \n"; return 0; }
| 9.34.7.
| Use new to allocate memory for a class pointer
|
#include< iostream.h> #include< string.h>
class phone { char name[50]; char tell[15]; public: void store(char *n, char *num); void print(); };
void phone:: store(char *n, char *num) { strcpy(name, n); strcpy(tell, num); }
void phone:: print() { cout< < name< < ": " < < tell; cout< < " \n"; }
main() { phone *p; p= new phone;
if (! p) { cout< < " Alloction error."; return 1; } p-> store(" AA", " 9999999999"); p-> print(); delete p; return 0; }
|