Exam Example Questions

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Introduction to Programming Lecture 39. Copy Constructor.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Operator Overloading Fundamentals
1 תוכנה 1 תרגול 14 – סיכום. 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public.
LECTURE LECTURE 17 More on Inheritance, Virtual Inheritance, & Virtual Destructors, 17.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב'
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
. Copying, casting, and more. Example: MyString u Lets put our knowledge of C++ classes to use u Define a class to represent a string u Replace all the.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Definition Class In C++, the class is the construct primarily used to create objects.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Review of C++ Programming Part II Sheng-Fang Huang.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Java and C++, The Difference An introduction Unit - 00.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 CSC241: Object Oriented Programming Lecture No 22.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
LECTURE LECTURE 14 Exception Handling Textbook p
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Overview of C++ Polymorphism
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Overloading Operator MySting Example
Inheritance II CMSC 202.
Lesson 14 Copy and Assignment
C Basics.
LinkedList Class.
Chapter 2 Objects and Classes
group work #hifiTeam
Chapter 16-2 Linked Structures
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
CS212: Object Oriented Analysis and Design
Pointers, Dynamic Data, and Reference Types
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Dynamic Memory A whole heap of fun….
Introduction to Programming
Overview of C++ Polymorphism
CS410 – Software Engineering Lecture #5: C++ Basics III
The Stack.
CS410 – Software Engineering Lecture #6: Advanced C++ I
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Exam Example Questions

Makefile data.o: data.c data.h gcc -c data.c driver.o: driver.c data.h gcc -c driver.c io.o: io.c gcc -c io.c driver: io.o data.o driver.o gcc -o driver io.o driver.o data.o

Which files will be updated ? 1. driver 2. driver.o 3. data.h -rw-r--r-- 1 plab courses 673 Feb 4 18:10 data.c -rw-r--r-- 1 plab courses 673 Feb 4 16:59 data.h -rw-r--r-- 1 plab courses 673 Feb 4 17:00 data.o -rwxr-xr-x 1 plab courses 673 Feb 4 18:01 driver -rw-r--r-- 1 plab courses 673 Feb 4 17:30 driver.c -rw-r--r-- 1 plab courses 673 Feb 4 17:31 driver.o -rw-r--r-- 1 plab courses 673 Feb 4 17:40 io.c -rw-r--r-- 1 plab courses 673 Feb 4 17:41 io.o Which files will be updated ? 1. driver 2. driver.o 3. data.h 4. data.o 5. io.o 1, 4

Macro What will be the output ? #define MAX(a,b) ( ( (a) > (b) ) ? (a) : (b) ) int i=2; int j=3; int x = MAX(i++,j); printf("%d %d\n",i,x); What will be the output ? 3 3

C string char s[] = {'p','l','a','b'}; What will be returned by calling strcmp(s,"plab")? a negative number 2. 0 3. a positive number 4. We cannot know for sure 4

struct struct Flight { char source[20]; char * destination; }; Flight ticket1; Flight ticket2; ticket1.destination =(char*)malloc( 20*sizeof(char)); strcpy(ticket1.source, "florence"); strcpy(ticket1.destination,"london"); ticket2=ticket1; *(ticket2.destination + 2) = 'k'; ticket2.source[1] = 'm'; printf("%s %s",ticket1.source,ticket2.destination); The output of the code will be: florence lokdon

Friend Function (1) class Movie { (2) public: (3) friend void print(Movie const& m); (4) private: (5) int m_length; (6) }; (7) void Movie::print( Movie const & m ) { (8) cout << m.m_length; (9) }

Friend Function בחר את התשובה הנכונה (תשובה אחת בלבד): ניסיון הגישה למשתנה הפרטי (private member) m_length בשורה 8 יגרום לשגיאת קומפילציה. הקוד יתקמפל ללא שגיאות ולא יגרום לשגיאות זמן-ריצה (run-time errors). בשורה 7, "print" חייבת להיות מוגדרת כפונקציה ולא כמתודה (method) של המחלקה Movie. הרצת הקוד של "print" תגרום לשגיאות זמן-ריצה. תשובה - 3

Operator Overloading מהי הצורה הנכונה להכריז על אופרטור ההשמה (assignment operator), להשמת אובייקטים מהמחלקה (class) Y למחלקה X. כלומר, מתודה המאפשרת את שורות הקוד הבאות: X a; Y b; a = b;

Operator Overloading void Y::operator=(Y const &from); void X::operator=(X& to, Y const &from); Y& X::operator=( Y const &from); X& X::operator=( Y const &from); Y& Y::operator=( X const &to); תשובה 4

Copy Constructor class Person { public: Person ( int id, int friendId = 0) : m_id( id ) { init( friendId ); } ˜Person( ) { if (m_bestFriend) delete m_bestFriend; } void init( int friendId ) { if( friendId > 0 ) m_bestFriend = new Person( friendId ); else m_bestFriend = NULL; } void show () {cout<<"id "<< m_id << endl; cout<<“best friend “<< m_bestFriend->m_id<<endl; } protected: int m_id; Person * m_bestFriend; };

Copy Constructor תשובה 1 int func4() { Person p( 1234,789); foo(p); p.show(); } void foo( Person p ) { p.show();} הקוד של הפונקציה "func4” עלול לגרום לשגיאות זמן-ריצה (run-time errors). הקוד של הפונקציה "func4” יגרום לשגיאות בקומפילציה. הקוד של הפונקציה "func4” ירוץ ויתקמפל ללא שגיאות. אם נגדיר את המתודה (method) show כמתודה סטטית, הקוד יתקמפל וירוץ ללא שגיאות. תשובה 1

Student class Student : public Person { public: Student( int id, int friendId, int grade) : Person( id, friendId) , m_grade(grade) { } void show() { cout <<"id "<<m_id <<“ grade "<<m_grade<< endl; } protected: int m_grade; };

Exceptions id 1234 //assuming Student : public Person int bar( int id) { if (id < 0) throw Student(1234, 567, 89); else return 0; } int func5() { try { bar(-3); } catch(Person p) { p.show(); } catch(Student s) { s.show(); id 1234

Animal class Animal { public: Animal(string name = string(“something”) ) { m_name = name; cout << "animal ctor" << endl; } virtual ˜Animal() { cout << "animal dtor" << endl; } virtual void print() { cout << m_name << endl;} private: string m_name; };

Dog class Dog : public Animal { public: Dog ( string name ) : Animal(name) { cout << "dog ctor" << endl; } virtual ˜Dog() { cout << "dog dtor" << endl; } virtual void print() { Animal::print(); cout << “how how” << endl; } };

Construction and Destruction void foo( Dog& d ) { cout << "how how" << endl; } int main() { Dog d("rambo"); foo(d); return 0; What will be the output ? animal ctor dog ctor how how dog dtor animal dtor

Frog class Frog : public Animal{ public: Frog ( string name ) : Animal(name) { cout << "frog ctor" << endl; } virtual ˜Frog () { cout << "frog dtor" << endl; } virtual void print() { Animal::print(); cout << “quack” << endl; } private: Frog ( Frog const& f ) : Animal( f ) {} };

Dynamic Casting (1) Frog * f = new Frog("foo"); (2) Animal * a = new Dog("humi"); (3) Dog * d1 = dynamic_cast<Dog*>( f ); (4) Dog * d2 = dynamic_cast<Dog*>( a ); לאחר ריצת שורות הקוד הנ"ל, d1=NULL (כלומר, הפוינטר d1 יקבל את הערך NULL). לאחר ריצת שורות הקוד הנ"ל, d2=NULL באחת השורות עלולה להיות שגיאת זמן ריצה (run-time error). תשובה 1

Type Checking! Dog d1("humi"); Dog d2("muki"); (1) Dog& d3 = d2; (4) Dog& d5 = new Dog("rambo"); סמנו את כל השורות שיגרמו לשגיאת קומפילציה (4) Trying to convert Dog* to Dog&

Hidden Constructor האם השורות הבאות מתקמפלות ללא שגיאה? Frog f(“kermit”); vector<Frog> fv; fv.push_back(f); fv[0].print(); instantiated from `vector<_Tp, _Alloc>::push_back (const _Tp &) [with _Tp = Frog, _Alloc = allocator<Frog>]' `Frog::Frog (const Frog &)' is private Will not compile – Frog’s copy c-tor is private

Conversion void foo(string s) { cout << s << endl; } void bar(char* s) { cout << s << endl; } void apple(int i) { cout << i << endl; } int main() { char c=’z’; char *str = "plab"; string s(“easy”); foo(str); //1 bar(s); //2 apple(c); //3 return 0; } 2 – trying to convert string to char* , no implicit cast available

Const (2) converting foo const& to foo&. struct foo { int* p; }; class bar { foo m_foo; public: bar(int *q) { m_foo.p = q; } foo const& getFoo() const { return m_foo; }//1 foo & getApple() const { return m_foo; }//2 }; int main() { int i, j; bar mybar(&i); *(mybar.getFoo().p) = 7; //3 mybar.getFoo().p = &j; //4 } Which lines will not compile ? (2) converting foo const& to foo&. (4) assigning to a const pointer

Memory allocation void func() { double a; int* b = new int; double* c = &a; set<int> d; double* e = new double[10]; string words[10]; // ... } Write code to release memory for this section: delete b; delete [] e;