Download presentation
Presentation is loading. Please wait.
Published byΑελλα Ειδοθεα Φωτόπουλος Modified over 6 years ago
1
Eleventh step for Learning C++ Programming
Review HomeWork Solution
2
A pointer is a variable that holds the address of something else.
... MEMORY 1 2 3 4 5 81345 81346 81347 Address int foo; int *x; foo = 123; x = &foo; foo 123 x 3 10/2013
3
P o i n t e r A pointer must have a value before you can dereference it (follow the pointer). int *x; *x=3; int foo; int *x; x = &foo; *x=3; x doesn’t point to anything!!! ERROR!!! this is fine x points to foo 10/2013
4
[practice 1] P o i n t e r
5
[explain 1] P o i n t e r
6
A string is a null terminated array of characters.
null terminated means there is a character at the end of the the array that has the value 0 (null). Pointers are often used with strings: char *msg = "RPI"; zero (null) msg 'R' 'P' 'I'
7
[practice 2] S t r i n g
8
[explain 2] S t r i n g
9
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 ( 추상화, 캡슐화, 데이터 은닉, 상속, 다형성)
10
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
11
[practice 3] C l a s s
12
[explain 3] C l a s s
13
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 catch(int c) { cout<< c << “error” << endl; } return 0; 실행 결과 0이 아닐경우 일 경우 a/b : 입력오류 int main() { int a, b; cin>> a >> b; try { if(b==0) throw = b; cout<< “a/b : “ << a/b << endl; }
14
[practice 4] Exception Handling
15
[explain 4] Exception Handling
16
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] 20 *a 10 *(a + 1) *a + 1 11 p[2] 30 q[1] 50 m[0][0] 2 **m *(m[1] + 2) - 2 *(q + m[1][2]) 60 *(q + **m) m[**m][**m] 9 *(m[*(*(m + 1) + 2)] + **m) **x x[0][0] *(&a[3] - 1) *(x[1] + 2) -7 *x[1] + 2 -3 *(*(x + 0) + 3) 40 q - p 3 *(a + (q - p)) 11/2014
17
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][**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; }
18
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 11/2014
19
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); 11/2014
20
class Student { char *Name; int Number; Course* Courses; }; 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; }; 11/2014
21
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 SetName(const char *pCh_Name) { strcpy(Name, pCh_Name); } const char* GetName() { return Name; void SetNumber(int Int_Number) { Number = Int_Number; void SetCourse(Course *pCourse) { for(int i=0; i<32; ++i) { if(Courses[i] == NULL) { Courses[i] = pCourse; Course* GetCourses(int Int_Index) { return Courses[Int_Index]; }; 11/2014
22
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 SetName(const char *pCh_Name) { strcpy(Name, pCh_Name); const char* GetName() { return Name; void SetNumber(int Int_Number) { Number = Int_Number; void SetCourse(Course *pCourse) { for(int i=0; i<32; ++i) { if(Courses[i] == NULL) { Courses[i] = pCourse; Course* GetCourses(int Int_Index) { return Courses[Int_Index]; }; 11/2014
23
class Course { public: int Id; char* Instructor; int RoomNr; };
Handout 5 d) Create a class Course with following attributes: Id: integer Instructor: char * RoomNr: integer class Course { public: int Id; char* Instructor; int RoomNr; }; 11/2014
24
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].RoomNr = i; } Student Arr_Student[3]; char pCh_Buffer[256]; cout(pCh_Buffer, "Std%d", i + 1); Arr_Student[i].SetNumber(i + 1); Arr_Student[i].SetName(pCh_Buffer); Arr_Student[0].SetCourse(0, &Arr_Course[1]); Arr_Student[0].SetCourse(1, &Arr_Course[2]); Arr_Student[1].SetCourse(0, &Arr_Course[0]); Arr_Student[1].SetCourse(1, &Arr_Course[1]); Arr_Student[1].SetCourse(2, &Arr_Course[2]); Arr_Student[2].SetCourse(0, &Arr_Course[2]); 11/2014
25
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 PrintCourseInfo(int Int_ID, Course Courses[], int Int_CourseSize, Student Arr_Student[], int Int_StudentSize) { Course *pCourse = NULL; for(int i=0; i<Int_CourseSize; ++i) { if(Courses[i].Id == Int_ID) { pCourse = &Courses[i]; break; } if(pCourse == NULL) { return; int Int_StudentCount = 0; for(int i=0; i<Int_StudentSize; ++i) { for(int j=0; j<32; ++j) { if(pCourse == Arr_Student[i].GetCourses(j)) { ++Int_StudentCount; cout << "ID : " << pCourse->Id << ", Instructor : " << pCourse->Instructor << ", RoonNo : " << pCourse->RoomNr << ", StudentCount : " << Int_StudentCount << endl; 11/2014
26
Handout 6 class A { public : int x; A *objARef; private : int y;
Exercise 1: Given the following C++ code: class A { public : int x; A *objARef; private : int y; protected : int z; }; class B : public A { public : A objA; class C { B objB; Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK). 11/2014
27
Handout 6 objA.x objA.y objA.z objARef->x objARef->y
in class A in class B in class C x OK ■ Wrong OK Wrong ■ y z objA.x objA.y objA.z objARef->x objARef->y objARef->z objB.x objB.y objB.z 11/2014
28
Handout 6 Exercise 2: Given the following class hierarchy:
1. Create C++ code without attributes and methods for all 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{ 11/2014
29
Handout 6 class Character : public Object{ public: char ch; };
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 &rCharacter) { Character Char_Ret; Char_Ret.ch = ch + rCharacter.ch; return Char_Ret; } 11/2014
30
Handout 6 class Digit : public Character{ public:
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’ // = 11 % 10 = 1) class Digit : public Character{ public: Digit operator + (const Digit &rDigit) { Digit Dig_Ret; Dig_Ret.ch = (((ch - '0') + (rDigit.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; } }; 11/2014
31
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 &rCharacter) { Character Char_Ret; Char_Ret.ch = ch + rCharacter.ch; return Char_Ret; } char GetCh() { return ch; void SetCh(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]; } 11/2014
32
Handout 7 Exercise 1: Given the following C++ code: 11/2014
#include <iostream> void fun() { try { std::cout << "FA\n"; throw 3; // line 5 std::cout << "BA\n"; } catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; void main() { std::cout << "A\n"; fun(); std::cout << "B\n"; std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014
33
1a. What is the output of the above code?
Handout 7 1a. What is the output of the above code? 11/2014
34
Handout 7 1b) 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; 11/2014
35
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_ErrorCode = Int_Code; } int Int_ErrorCode; }; b) Change the throw statement in line 5 so that it throws objects of your exception class. void fun() { try { std::cout << "FA\n"; throw Exception(3); // line 5 std::cout << "BA\n"; catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; 11/2014
36
Handout 7 c) Add a catch block in main for catching exception-objects of your exception class. void main() { try { std::cout << "A\n"; fun(); std::cout << "B\n"; } catch (int i) { std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (Exception ex) { std::cout << "Exception " << ex.Int_ErrorCode << "\n"; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014
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. #include <iostream> class Exception { public: Exception(int Int_Code) { Int_ErrorCode = Int_Code; } int Int_ErrorCode; }; void fun() { try { std::cout << "FA\n"; throw new Exception(3); std::cout << "BA\n"; catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; 11/2014
38
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 << "A\n"; fun(); std::cout << "B\n"; } catch (int i) { std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (Exception *ex) { std::cout << "Exception " << ex->Int_ErrorCode << "\n"; delete ex; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.