step for Learning 11 Eleventh C Programming Review



![[practice 1] P o i n t e r 4 [practice 1] P o i n t e r 4](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-4.jpg)
![[explain 1] P o i n t e r 5 [explain 1] P o i n t e r 5](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-5.jpg)

![[practice 2] S t r i n g 7 [practice 2] S t r i n g 7](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-7.jpg)
![[explain 2] S t r i n g 8 [explain 2] S t r i n g 8](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-8.jpg)


![[practice 3] C l a s s 11 [practice 3] C l a s s 11](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-11.jpg)
![[explain 3] C l a s s 12 [explain 3] C l a s s 12](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-12.jpg)

![[practice 4] Exception Handling 14 [practice 4] Exception Handling 14](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-14.jpg)
![[explain 4] Exception Handling 15 [explain 4] Exception Handling 15](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-15.jpg)
![Handout 4 1. Given the C++ variable declarations below. int m[3][3] = {{2, 3, Handout 4 1. Given the C++ variable declarations below. int m[3][3] = {{2, 3,](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-16.jpg)
![Handout 4 #include <iostream> using namespace std; void main() { int m[3][3] = {{2, Handout 4 #include <iostream> using namespace std; void main() { int m[3][3] = {{2,](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-17.jpg)

![Handout 4 • #include <iostream> using namespace std; void rotate(int a[], int len, int Handout 4 • #include <iostream> using namespace std; void rotate(int a[], int len, int](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-19.jpg)




















- Slides: 39

step for Learning 11 Eleventh. C++ Programming • Review • Home. Work Solution

Pointer A pointer is a variable that holds the address of something else. • int foo; • int *x; • foo • Address • 0 • 1 • 2 • 3 • 4 • 5 • x • 81345 • 81346 • 81347 • . . . • foo = 123; • x = &foo; • 123 • . . . • • MEMORY • 3 L 6. 2 2

Pointer A pointer must have a value before you can dereference it (follow the pointer). • int *x; • *x=3; !!! g n i ! ! h ! yt R n O a R o t R t n • E i o p ’t n s e o • x d • int foo; • int *x; • x = &foo; • *x=3; ne i f s i • this oo f o t s t n i o p 3 • x
![practice 1 P o i n t e r 4 [practice 1] P o i n t e r 4](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-4.jpg)
[practice 1] P o i n t e r 4
![explain 1 P o i n t e r 5 [explain 1] P o i n t e r 5](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-5.jpg)
[explain 1] P o i n t e r 5

String A string is a null terminated array of characters. null terminated means there is a character at the end of the array that has the value 0 (null). Pointers are often used with strings: char *msg = "RPI"; ll) u n o( r e • z • msg • 'R' • 'P' • 'I' • 0 6
![practice 2 S t r i n g 7 [practice 2] S t r i n g 7](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-7.jpg)
[practice 2] S t r i n g 7
![explain 2 S t r i n g 8 [explain 2] S t r i n g 8](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-8.jpg)
[explain 2] S t r i n g 8

OOP (Object Oriented Programming) • OOP? (Object Oriented Programming) -To recognize all of the data as an object, an object can be operated independently, while other objects, also available as a component to the program. Programming available as a component of other objects • Characters of OOP - Abstraction, Encapsulation, Informationhiding, Inheritance, Polymorphism ( 추상화, 캡슐화, 데이터 은닉, 상속, 다형성) 9

Class • The composition of the class -The class is blueprint of the object. -The class is composed member variables(or fields) and member functions(or methods) -Member variable represents the properties of object -Member function represents the behavior of object 10
![practice 3 C l a s s 11 [practice 3] C l a s s 11](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-11.jpg)
[practice 3] C l a s s 11
![explain 3 C l a s s 12 [explain 3] C l a s s 12](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-12.jpg)
[explain 3] C l a s s 12

Exception Handling • Basic Concepts • An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing • The special processing that may be required after detection of an exception is called exception handling • The exception handling code unit is called an exception handler int main() { int a, b; cin>> a >> b; try { if(b==0) throw = b; cout<< “a/b : “ << a/b << endl; } catch(int c) { cout<< c << “error” << endl; } return 0; } 실행 결과 0이 아닐경우 80 4 a/b : 20 0일 경우 40 0 입력오류 13
![practice 4 Exception Handling 14 [practice 4] Exception Handling 14](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-14.jpg)
[practice 4] Exception Handling 14
![explain 4 Exception Handling 15 [explain 4] Exception Handling 15](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-15.jpg)
[explain 4] Exception Handling 15
![Handout 4 1 Given the C variable declarations below int m33 2 3 Handout 4 1. Given the C++ variable declarations below. int m[3][3] = {{2, 3,](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-16.jpg)
Handout 4 1. Given the C++ variable declarations below. int m[3][3] = {{2, 3, 6}, {8, 4, 2}, {7, 4, 9}}; int a[] = {10, 20, 30, 40, 50, 60, 70 , 80}; int b[] = {-5, -6, -7, -5, -3, -5, -6}; int *x[2] = {a, b}; int *p = a; int *q = &a[3]; Evaluate the following C++ expressions and give the results of each expression a[1] *a *(a + 1) *a + 1 p[2] q[1] m[0][0] **m *(m[1] + 2) - 2 *(q + m[1][2]) *(q + **m) m[**m] *(m[*(*(m + 1) + 2)] + **m) **x x[0][0] *(&a[3] - 1) *(x[1] + 2) *x[1] + 2 *(*(x + 0) + 3) q - p *(a + (q - p)) 20 10 20 11 30 50 2 2 0 60 60 9 9 10 10 30 -7 -3 40 16
![Handout 4 include iostream using namespace std void main int m33 2 Handout 4 #include <iostream> using namespace std; void main() { int m[3][3] = {{2,](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-17.jpg)
Handout 4 #include <iostream> using namespace std; void main() { int m[3][3] = {{2, 3, 6}, {8, 4, 2}, {7, 4, 9}}; int a[] = {10, 20, 30, 40, 50, 60, 70, 80}; int b[] = {-5, -6, -7, -5, -3, -5, -6}; int *x[2] = {a, b}; int *p = a; int *q = &a[3]; cout << a[1] << endl; cout << *a << endl; cout << *(a + 1) << endl; cout << *a + 1 << endl; cout << p[2] << endl; cout << q[1] << endl; cout << m[0][0] << endl; cout << **m << endl; cout << *(m[1] + 2) - 2 << endl; cout << *(q + m[1][2]) << endl; cout << *(q + **m) << endl; cout << m[**m] << endl; cout << *(m[*(*(m + 1) + 2)] + **m) << endl; cout << **x << endl; cout << x[0][0] << endl; cout << *(&a[3] - 1) << endl; cout << *(x[1] + 2) << endl; cout << *x[1] + 2 << endl; cout << *(*(x + 0) +3) << endl; cout << q - p << endl; cout << *(a + (q - p)) << endl; } 17

Handout 4 2. A rotation of some array A is the cyclic shifting of all elements according to some given offset, Example: Array A: {1, 2, 3, 4, 5, 6, 7, 8} If we rotate the elements of array A by 4 positions, then we get: {5, 6, 7, 8, 1, 2, 3, 4} Create and implement a function void rotate (int a[], int len, int offset) in C++. Meaning of the arguments: a[] the input array, len : the size of array a[] (number of elements in array a, offset : the shifting offset 18
![Handout 4 include iostream using namespace std void rotateint a int len int Handout 4 • #include <iostream> using namespace std; void rotate(int a[], int len, int](https://slidetodoc.com/presentation_image_h2/8f8d58bd7850c2ccb12033c6618b437b/image-19.jpg)
Handout 4 • #include <iostream> using namespace std; void rotate(int a[], int len, int offset) { for(int i=0; i<offset; i++) a[i]+=offset; for(int i=offset; i<len; i++) a[i] -= offset; } void output(int a[], int len) { for(int i=0; i<len; i++) cout<<a[i]<<" "; } void main() { int a[]={1, 2, 3, 4, 5, 6, 7, 8}; int len=8; output(a, len); int offset; cout<<"How much do shifting? : "; cin>>offset; rotate(a, len, offset); output(a, len); } 19

Handout 5 • Exercise 1: Class and object definition a) Define a class Student with the following attributes: Name : char * Number : integer Courses: Course * class Student { char *Name; int Number; Course* Courses; }; 20

Handout 5 b) Make each of the above attributes private and define for each attribute a pair of public get-set-methods class Student { private: char Name; int Number; Course *Courses; public: void Set. Name(const char *p. Ch_Name) { strcpy(Name, p. Ch_Name); } const char* Get. Name() { return Name; } void Set. Number(int Int_Number) { Number = Int_Number; } void Set. Course(Course *p. Course) { for(int i=0; i<32; ++i) { if(Courses[i] == NULL) { Courses[i] = p. Course; } } } Course* Get. Courses(int Int_Index) { return Courses[Int_Index]; } }; 21

Handout 5 • c) Create a constructor-method for your Student-class for initializing objects of the Student-Class. class Student { private: char Name; int Number; Course *Courses; public: Student() { memset(Name, 0, 256); Number = 0; memset(Courses, 0, sizeof(Course*) * 32); } void Set. Name(const char *p. Ch_Name) { strcpy(Name, p. Ch_Name); } const char* Get. Name() { return Name; } void Set. Number(int Int_Number) { Number = Int_Number; } void Set. Course(Course *p. Course) { for(int i=0; i<32; ++i) { if(Courses[i] == NULL) { Courses[i] = p. Course; } } } Course* Get. Courses(int Int_Index) { return Courses[Int_Index]; } }; 22

Handout 5 • d) Create a class Course with following attributes: Id: integer Instructor: char * Room. Nr: integer class Course { public: int Id; char* Instructor; int Room. Nr; }; 23

Handout 5 • e) Using the above two classes create a simple application that allows the assignment of students to courses (using the Courses attribute of the class Student). Your application should internally store a sequence of Student objects either by using an array of by using a linked list. • void main() { Course Arr_Course[3]; for(int i=0; i<3; ++i) { Arr_Course[i]. Id = i + 1; sprintf(Arr_Course[i]. Instructor, "Inst%d", i + 1); Arr_Course[i]. Room. Nr = 101 + i; } Student Arr_Student[3]; for(int i=0; i<3; ++i) { char p. Ch_Buffer[256]; cout(p. Ch_Buffer, "Std%d", i + 1); Arr_Student[i]. Set. Number(i + 1); Arr_Student[i]. Set. Name(p. Ch_Buffer); } Arr_Student[0]. Set. Course(0, &Arr_Course[1]); Arr_Student[0]. Set. Course(1, &Arr_Course[2]); Arr_Student[1]. Set. Course(0, &Arr_Course[0]); Arr_Student[1]. Set. Course(1, &Arr_Course[1]); Arr_Student[1]. Set. Course(2, &Arr_Course[2]); Arr_Student[2]. Set. Course(0, &Arr_Course[2]); } 24

Handout 5 • f) Extend your application by a query function that prints for a given course. Id the name and number of all students who attend this course. • void Print. Course. Info(int Int_ID, Courses[], int Int_Course. Size, Student Arr_Student[], int Int_Student. Size) { Course *p. Course = NULL; for(int i=0; i<Int_Course. Size; ++i) { if(Courses[i]. Id == Int_ID) { p. Course = &Courses[i]; break; } } if(p. Course == NULL) { return; } int Int_Student. Count = 0; for(int i=0; i<Int_Student. Size; ++i) { for(int j=0; j<32; ++j) { if(p. Course == Arr_Student[i]. Get. Courses(j)) { ++Int_Student. Count; } } } cout << "ID : " << p. Course->Id << ", Instructor : " << p. Course->Instructor << ", Roon. No : " << p. Course>Room. Nr << ", Student. Count : " << Int_Student. Count << endl; } 25

Handout 6 • Exercise 1: Given the following C++ code: class A { public : int x; A *obj. ARef; private : int y; protected : int z; }; class B : public A { public : A obj. A; }; class C { public : A obj. A; A *obj. ARef; B obj. B; }; Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK). 26

Handout 6 in class A in class B in class C x OK ■ Wrong OK Wrong ■ y OK ■ Wrong OK Wrong ■ z OK ■ Wrong OK Wrong ■ obj. A. x OK Wrong ■ OK ■ Wrong obj. A. y OK Wrong ■ obj. A. z OK Wrong ■ obj. ARef->x OK ■ Wrong obj. ARef->y OK ■ Wrong OK Wrong ■ obj. ARef->z OK ■ Wrong OK Wrong ■ obj. B. x OK Wrong ■ OK ■ Wrong obj. B. y OK Wrong ■ obj. B. z OK Wrong ■ 27

Handout 6 • Exercise 2: • Given the following class hierarchy: 1. Create C++ code without attributes and methods for all 6 classes. • class Object { }; class Character : public Object{ }; class Digit : public Character{ }; class Letter : public Character{ }; class Vowel : public Letter{ }; class Consonant : public Letter{ }; 28

Handout 6 • 2. Extend the class character by a public attribute ch, so that it can store a single character. class Character : public Object{ public: char ch; }; • 3. Overload the operator + for the class Character, so that it can add two objects of type Character. • Character operator + (const Character &r. Character) { Character Char_Ret; Char_Ret. ch = ch + r. Character. ch; return Char_Ret; } 29

Handout 6 • 4. Override the operator + in the Digit class, so that it adds the numeric value of two digits and delivers the digit that we get if we finally apply “modulo 10”. (Example ‘ 5’ + ‘ 6’ = ‘ 1’ // 5 + 6 = 11 % 10 = 1) class Digit : public Character{ public: Digit operator + (const Digit &r. Digit) { Digit Dig_Ret; Dig_Ret. ch = (((ch - '0') + (r. Digit. ch - '0')) % 10) + '0'; return Dig_Ret; } • 5. Extend the Object class by an object counter that counts the number of created objects for all objects of the above class hierarchy. (Tip: Lecture 9 slide 5) The counter should be embedded into the Object-class default constructor. class Object { public: Object() { static int Int_Count = 0; ++Int_Count; cout << "Current Object created : " << Int_Count << endl; } }; 30

Handout 6 • 6. Change the visibility of the attribute ch, so that it is visible in all subclasses, but inaccessible from outside. Create a get-set method pair for the attribute ch. class Character : public Object{ public: Character operator + (const Character &r. Character) { Character Char_Ret; Char_Ret. ch = ch + r. Character. ch; return Char_Ret; } char Get. Ch() { return ch; } void Set. Ch(char a_Ch) { ch = a_Ch; } protected: char ch; }; • 7. Create a main-method, where you create 2 objects of each class in the above class hierarchy and that prints finally the value of your object counter (this should be 10). void main() { Character ch[2]; Digit dig[2]; Letter let[2]; Vowel vow[2]; Consonant con[2]; 31 }

Handout 7 • Exercise 1: Given the following C++ code: #include <iostream> void fun() { try { std: : cout << "FAn"; throw 3; // line 5 std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; } 32

Handout 7 • 1 a. What is the output of the above code? 33

Handout 7 • 1 b) What is the output of the above code, if the throw-statement in line 5 is replaced by: i) throw (double)5. 0; ii) throw 'c'; iii) throw true; First try to develop the answers by simply analysing the code. Afterwards take a compiler for verifying whether your answers were right or not. i) throw (double)5. 0; ii) throw 'c'; iii) throw true; 34

Handout 7 • 2. a) Extend the above code by adding an exception class for exception-object creation. Your exception class should comprise an error indicating attribute (e. g. by using an integer. ) and an appropriate constructor. class Exception { public: Exception(int Int_Code) { Int_Error. Code = Int_Code; } int Int_Error. Code; }; b) Change throw statement in line 5 so that it throws objects of your exception class. void fun() { try { std: : cout << "FAn"; throw Exception(3); // line 5 std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } 35

Handout 7 • c) Add a catch block in main for catching exception-objects of your exception class. void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (Exception ex) { std: : cout << "Exception " << ex. Int_Error. Code << "n"; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; 36 }

Handout 7 • d) Change your code so that it works with objects created on the heap by using the new operator. Discuss the positive and negative aspects of such an object-reference based approach for exception handling. #include <iostream> class Exception { public: Exception(int Int_Code) { Int_Error. Code = Int_Code; } int Int_Error. Code; }; void fun() { try { std: : cout << "FAn"; throw new Exception(3); std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } 37

Handout 7 • d) Change your code so that it works with objects created on the heap by using the new operator. Discuss the positive and negative aspects of such an object-reference based approach for exception handling. void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (Exception *ex) { std: : cout << "Exception " << ex->Int_Error. Code << "n"; delete ex; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; } 38

Thank you