Matriculation Number:

Similar documents
Review Questions for Final Exam

Object oriented programming

Functions, Arrays & Structs

Linked List using a Sentinel

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Object Oriented Programming: Inheritance Polymorphism

pointers & references

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Comp151. Generic Programming: Container Classes

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSC1322 Object-Oriented Programming Concepts

Functions, Arrays & Structs

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Classes and Objects in C++

Comp151. Inheritance: Initialization & Substitution Principle

Templates and Vectors

TEMPLATE IN C++ Function Templates

Lecture 23: Pointer Arithmetic

Inheritance, and Polymorphism.

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Constructor - example

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Due Date: See Blackboard

CSCE 110 PROGRAMMING FUNDAMENTALS

University of Swaziland Department OfComputer Science Main Examination December 2016

Part I: Short Answer (6 questions, 18 points total)

การทดลองท 8_2 Editor Buffer Array Implementation

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Review Questions for Final Exam KEY

Review: C++ Basic Concepts. Dr. Yingwu Zhu

pointers + memory double x; string a; int x; main overhead int y; main overhead

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Module 9. Templates & STL

Using Parallel Arrays. Parallel Array Example

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CS 247: Software Engineering Principles. ADT Design

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Global & Local Identifiers

Object oriented programming

Classes in C++98 and C++11

CS 115 Midterm 2 Solutions

C:\Temp\Templates. Download This PDF From The Web Site

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Use the dot operator to access a member of a specific object.

CE221 Programming in C++ Part 1 Introduction

CS197c: Programming in C++

Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.

Object Oriented Design

Question 1 Consider the following structure used to keep employee records:

Name Section: M/W or T/TH. True or False (14 Points)

Name SECTION: 12:45 2:20. True or False (12 Points)

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

True or False (12 Points)

Lambda functions. Zoltán Porkoláb: C++11/14 1

True or False (14 Points)

Friends and Overloaded Operators

CS24 Week 3 Lecture 1

Composition I. composition is a way to combine or compose multiple classes together to create new class

Introduction Of Classes ( OOPS )

Friends and Overloaded Operators

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CS 225. Data Structures

Inheritance and Polymorphism

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

EL2310 Scientific Programming

Midterm Exam 5 April 20, 2015

Object oriented programming

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Midterm Exam. Sample Solutions

C++_ MARKS 40 MIN

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

W3101: Programming Languages C++ Ramana Isukapalli

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

Lab 2: ADT Design & Implementation

CSCI 1370 APRIL 26, 2017

C++ Programming Fundamentals

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

C++ For Science and Engineering Lecture 12

Fundamentals of Programming CS-110. Lecture 2

Faculty of Science, Engineering and Technology

Object oriented programming

Polymorphism Part 1 1

Implementing an ADT with a Class

Constructors.

EE 152 Advanced Programming LAB 7

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CS 141, Introduction to Computer Science Fall Midterm Exam

Lecture 7. Log into Linux New documents posted to course webpage

moretosearch = (location < length);

Transcription:

Exercise 1. (a) Implement a function maximum that takes a vector of floats as argument and returns the maximum of the elements in the vector. The vector shall be passed as a const reference, and for an empty vector 0 shall be returned and an error message shall be displayed. #include <iostream> #include <vector> using namespace std; float maximum(const vector<float> &v) { if(v.size() == 0) { cout << "Error: vector is empty!" << endl; return 0; vector<float>::const_iterator it = v.begin(); float m = *it++; for(; it!= v.end(); ++it) if(*it > m) m = *it; return m; 1

(b) Implement a function maxvector that takes two vectors of shorts as arguments and returns the vector of maximums, i.e., the i -th element in the output vector shall be the maximum of the i -th elements in the input vectors. If the two input vectors have different sizes, an error message shall be displayed and an empty vector shall be returned. Example: Given the two vectors (1,2,5,6) and (2,-1,4,7), the result shall be = (2,2,5,7). #include <iostream> #include <vector> using namespace std; vector<short> maxvector(const vector<short> &a, const vector<short> &b) { if(a.size()!= b.size()) { cout << "Error: vectors have different lengths!" << endl; return vector<short>(); vector<short> v(a.size()); for(vector<short>::size_type i = 0; i < a.size(); ++i) v[i] = max(a[i],b[i]); return v; 2

Exercise 2. Given a structure Point for representing points in 3D (see code below), extend the implementation as follows: (a) Add a default constructor that initializes all coordinates to 0. (b) Add a constructor for initializing the three coordinates. (c) Overload the - operator for subtracting one point from another. (d) Overload the * operator for multiplying a scalar value with a point: Given a float s and a point p with coordinates (x,y,z ), then s * p is the point with coordinates (s x,s y,s z ). struct Point { float x, y, z; ; Point() : x(0), y(0), z(0) { Point(float xc, float yc, float zc) : x(xc), y(yc), z(zc) { Point operator-(const Point &p, const Point &q) { return Point(p.x-q.x, p.y-q.y, p.z-q.z); Point operator*(float s, const Point &p) { return Point(s*p.x, s*p.y, s*p.z); 3

Exercise 3. Design a class hierarchy for persons (class Person), students (class Student), and master students (class MasterStudent), such that a person has a name (of type string), a student is a person with an additional matric_number (of type int), and a master student is a student with an additional subject (of type string). Provide suitable constructors for the classes and make sure that all data members are private. Write a virtual member function info that prints all information (i.e., the data members) about a person and override this function in the derived classes such that also the additional information about students and master students is printed. When overriding info, call the info function of the base class first and then print the additional information. #include <iostream> #include <string> using namespace std; class Person { string name; public: Person(const string &n) : name(n) { ; virtual void info() const { cout << "name = " << name << endl; class Student : public Person { int matric_number; public: Student(const string &n, int mn) : Person(n), matric_number(mn) { ; void info() const { Person::info(); cout << "matr.nr = " << matric_number << endl; 4

class MasterStudent : public Student { string subject; public: MasterStudent(const string &n, int mn, const string &s) : Student(n,mn), subject(s) { ; void info() const { Student::info(); cout << "subject = " << subject << endl; 5

Exercise 4. (a) Read and understand the following program: #include <iostream> using namespace std; int main() { int x = 0, y = 0, z = 0; int &a = y; int *p1 = &x, *p2 = &y, *p3 = &z; *p1 = 100; a = 20; cout << "x=" << x << ", y=" << y << ", z=" << z << endl; // CHECK1 p3 = p1; p1 = &z; ++a; *p1 = *p3 + 5; cout << "x=" << x << ", y=" << y << ", z=" << z << endl; // CHECK2 p1 = p3; a += *p2; *p1 -= *p3; cout << "x=" << x << ", y=" << y << ", z=" << z << endl; // CHECK3 return 0; Fill the table with the values of the variables x, y, and z at the three checkpoints. x y z CHECK1 100 20 0 CHECK2 100 21 105 CHECK3 0 42 105 6

(b) Read and understand the following program: #include <iostream> using namespace std; class C { static int counter; int c; public: C() { c = ++counter; int get_c() { return c; virtual void id() { cout << "class C" << endl; void mycount() { cout << "C: " << c << endl; ; int C::counter = 10; class D : public C { public: void id() { cout << "class D" << endl; void mycount() { cout << "D: " << get_c() << endl; ; int main() { C c; D x; C *px = &x; x.id(); x.mycount(); px->id(); px->mycount(); return 0; What is the output of the program? class D D: 12 class D C: 12 7

Room for Notes 8

Room for Notes 9

Room for Notes 10