Solution to Midterm Exam

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Chapter 41 Defining Classes and Methods Chapter 4.
Character Input and Output C and Data Structures Baojian Hua
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Functions / Procedures
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Character Input and Output
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
C++ fundamentals.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
CSC Programming for Science Lecture 8: Character Functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Functions Functions, locals, parameters, and separate compilation.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Characters must also be encoded in binary. ASCII maps characters to numbers.
Object-Oriented Concepts
Chapter 7 User-Defined Methods.
CSE 220 – C Programming Pointers.
Strings (Continued) Chapter 13
Lecture 8 String 1. Concept of strings String and pointers
Functions, locals, parameters, and separate compilation
Structured Programming (Top Down Step Refinement)
Corresponds with Chapter 7
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Your questions from last session
CIS 199 Final Review.
Exercise Arrays.
C++ Programming CLASS This pointer Static Class Friend Class
Java Programming: From the Ground Up
Computer Science II for Majors
Presentation transcript:

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

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

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); }

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

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

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;

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)

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.

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.

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.

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)

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.

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);

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.