Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solution to Midterm Exam

Similar presentations


Presentation on theme: "Solution to Midterm Exam"— Presentation transcript:

1 Solution to Midterm Exam
1. What is the output? #include <stdio.h> main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k) { printf("%d\n", j); --j; ++k; } 5 4 3

2 Solution to Midterm Exam
2. What is printed? #define VAL 4 #include <stdio.h> main() { int a[3]; a[0] = 1; a[1] = -1; a[2] = VAL; printf("%d\n", a[0] < 1 || a[1] != 1); printf("%d\n", !a[1]); printf("%d\n", a[0] | a[1]); printf("%d\n", a[a[0]+1]); } 1 -1 4

3 Solution to Midterm Exam
3. Write a program that read standard input character by character, and print out the characters to the standard output, replacing spaces ' ' by underscores '\_'. You can use while loop, getchar(), and putchar() functions. #include <stdio.h> main() { int c; while( (c=getchar()) != EOF) { if(c == ' ') putchar('_'); else putchar(c); }

4 Quizzes What is the output? #include <stdio.h> main() {
char *s = "ABC xyz"; printf("%s\n", s+4); printf("%c\n", s[4]); printf("%c\n", *s); printf("%s\n", &s); } xyz x A P^U

5 Quizzes What is the output? 11, 2, 13, 13, 10 #include <stdio.h>
int i; void func(int j, int *p, int a[]); main() { int j, k, *p, a[1]; i = 1; j = 2; k = 3; p = &k; a[0] = 0; func(j, p, a); printf("%d, %d, %d, %d, %d\n", i, j, k, *p, a[0]); } void func(int j, int *p, int a[]) i += 10; j += 10; *p += 10; a[0] += 10; p = &i; 11, 2, 13, 13, 10

6 Quizzes Write a function (with a name of your choice) which take a string and returns an int. If the string looks the same when read forward and backward, return 1, else return 0. #include <string.h> int palindrome(char *s) { char *e; e = s + strlen(s) -1; while( s < e ) { if (*s != *e) return 0; ++s; --e; } return 1;

7 Looking Toward C++ Object-Oriented Design is characterized by
Objects (data together with operations on the data) Classes (abstract characterizations of objects) Inheritance (code sharing) Polymorphism (run-time binding of operations to objects)

8 Objects The object-oriented programming emphasizes objects, which consist of data and operations on the data. For example, we may have a figures like circle or line object with the operation of drawing the figures. If c is a circle object, we invoke its draw method (operation) by passing a message to c: c.draw() While the traditional programming typically call a function like draw(CIR) where CIR is a flag to indicate which figure to draw.

9 Classes and Abstract Data Type
A class is an abstract characterization of a set of objects. A class defines variable (data) and methods (operations) common to a set of objects. An abstract data type associates operations on the type but hides detail implementation.

10 Class example The following code declares a class string:
char data[80]; public: void store(char*); int length(); }; The char data[80] is the data, store() and length() are methods. Only the two methods are accessible to the outside world. The data are private.

11 Create Objects and Invoke Method
string s, t; defines two object s and t belonging to the class string. Data member is referenced by writing s.x and the method f is invoked by writing s.f(arguments) If ptr is a pointer to s, we use s -> x or s -> f(arguments)

12 Pass a Message to Object
class string { char data[80]; public: void store(char *); int length(); } string s, t; s.store("Hi Mom"); len = s.length(); The user of the string class need not to know the implementation details.

13 Inheritance A new class can be defined based on an old class. All the data and associated methods becomes part of the new class. class pen { int x, y; int status; public: void set_status(int); void set_location(int, int); }; class colored_pen: public pen { int color; void set_color(int);

14 Polymorphism Poly : Greek word meaning many.
Morphism : Greek word meaning form. Polymorphism mean many forms. In object-oriented programming, polymorphism refers to identically named methods that have different behavior depending on the type of object that they reference.


Download ppt "Solution to Midterm Exam"

Similar presentations


Ads by Google