Midterm Exam 5 April 20, 2015

Similar documents
Programming in C++: Assignment Week 5

18. Polymorphism. Object Oriented Programming: Pointers to base class // pointers to base class #include <iostream> using namespace std;

Data Structures (INE2011)

Inheritance, and Polymorphism.

CS250 Final Review Questions

Polymorphism Part 1 1

Friend Functions, Inheritance

Chapter 1: Object-Oriented Programming Using C++

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

CS250 Final Review Questions

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Introduction Of Classes ( OOPS )

Programming in C++: Assignment Week 6

B16 Object Oriented Programming

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

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Object-Oriented Programming (OOP) Fundamental Principles of OOP

CS250 Final Review Questions

More C++ : Vectors, Classes, Inheritance, Templates

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

4. C++ functions. 1. Library Function 2. User-defined Function

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Extending Classes (contd.) (Chapter 15) Questions:

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Object-Oriented Concept

CS111: PROGRAMMING LANGUAGE II

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS

CS313D: ADVANCED PROGRAMMING LANGUAGE

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

CS 162, Lecture 25: Exam II Review. 30 May 2018

SSE2034: System Software Experiment 3 Spring 2016

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++_ MARKS 40 MIN

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

IS 0020 Program Design and Software Tools

CSCE Practice Midterm. Data Types

Constructor - example

CS111: PROGRAMMING LANGUAGE II

CS105 C++ Lecture 7. More on Classes, Inheritance

EL2310 Scientific Programming

Programming in C++: Assignment Week 6

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

EL2310 Scientific Programming

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Polymorphism CSCI 201 Principles of Software Development

15: Polymorphism & Virtual Functions

Lab 2 - Introduction to C++

VIRTUAL FUNCTIONS Chapter 10

Inheritance and aggregation

IUE Faculty of Engineering and Computer Sciences Spring Semester

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Lecture 5: Inheritance

CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later

Programming in C++: Programming Test-1

What does it mean by information hiding? What are the advantages of it? {5 Marks}

The Address-of Operator &: The & operator can find address occupied by a variable. If var is a variable then, &vargives the address of that variable.

System Design and Programming II

EL2310 Scientific Programming

Function templates. An abstraction on a function space. A generic type.

COEN244: Polymorphism

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

Extending Classes (contd.) (Chapter 15) Questions:

C++ Inheritance and Encapsulation

Polymorphism. Zimmer CSCI 330

CS24 Week 3 Lecture 1

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

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.

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

C++ Polymorphism. Systems Programming

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS111: PROGRAMMING LANGUAGE II

CS 376b Computer Vision

Example Final Questions Instructions

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Design

Computer Programming Inheritance 10 th Lecture

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Final Exam. Name: Student ID: Section: Signature:

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

Fast Introduction to Object Oriented Programming and C++

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

C++ Important Questions with Answers

Object Oriented Design

Transcription:

Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d. protected. ANS c. static. Q2: Which of the following is most likely a base class of the other three? a. automobile. b. convertible. c. minivan. d. sedan. ANS a. automobile. Q3: To declare class subclass a privately derived class of superclass one would write: a. class subclass : private superclass b. class subclass :: private superclass c. class subclass < private superclass > d. class subclass inherits private superclass ANS a. class subclass : private superclass Q4: Assuming the following is the beginning of the constructor definition for class BasePlus- CommissionEmployee which inherits from class Point, BasePlusCommissionEmployee::BasePlusCommissionEmployee( string first, string last, string ssn, double sales, double rate, double salary ) : CommissionEmployee( first, last, ssn, sales, rate ) The second line: a. Invokes the CommissionEmployee constructor with arguments. b. Causes a compiler error. c. Is unnecessary because the CommissionEmployee constructor is called automatically. d. Indicates inheritance. ANS a. Invokes the CommissionEmployee constructor with arguments. Q5: Base class constructors and assignment operators: a. Are not inherited by derived classes. b. Should not be called by derived class constructors and assignment operators. c. Can be inherited by derived classes, but generally are not. d. Can call derived-class constructors and assignment operators. ANS: a. Are not inherited by derived classes. Q6: Which of the following statements about polymorphism is false? a. With polymorphism, you can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types. b. With polymorphism, new types of objects that can respond to existing messages can easily be incorporated into a system without modifying the base system.

c. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities. d. To get polymorphic behavior among existing objects, those objects must be instantiated from classes in the same inheritance hierarchy. ANS: c. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities. Actually, the reverse is true. Q7: If objects of all the classes derived from the same base class all need to draw themselves, the draw function would most likely be declared: a. private b. virtual c. protected d. friend ANS: b. virtual Q8: The main difference between a pure virtual function and a virtual function is: a. The return type. b. The member access specifier. c. That a pure virtual function cannot have an implementation. d. The location in the class. ANS: c. That a pure virtual function cannot have an implementation. Section 2 Question 1 (26 pts) Write code for two classes: Vehicle and Car. The classes need to provide constructors, destructor, and Describe functions so that the following main function can print out the output shown below. #include <iostream> #include <string> using namespace std; class Vehicle //Constructor for Vehicle //Solution: Vehicle(string num) //2 pt : regnum(num) cout << "Vehicle Constructor" << endl; // 2 pt //Destructor for Vehicle virtual ~Vehicle(void) // need to have virtual 1 pt cout << "Vehicle Destructor" << endl; // 1 pt for number 2

//Function Describe virtual void Describe(void) // Need to have virtual 2 pts cout << "Unknown vehicle, registration " << regnum << endl; string regnum; ; class Car : public Vehicle //Constructor for Car Car(string m, string num) : Vehicle(num), make( m ) // need to call Vehicle as initializer 2 pts cout << "Car Constructor" << endl; //Destructor for Car ~Car(void) cout << "Car Delete" << endl; // 1 pt for number 1 //Function Describe virtual void Describe(void) //1 pt for the whole function cout << "Car (" << make << "), registration " << regnum << endl; string make; ; int main() Car v1 ("BMW", "ABC 123");

Vehicle* vp4 = new Car ("Jaguar","XYZ 123"); vp4->describe(); delete vp4; return 0; Output: Vehicle Constructor Car Constructor Vehicle Constructor Car Constructor Car (Jaguar), registration XYZ 123 Car Delete Vehicle Destructor Car Delete Vehicle Destructor Question 2 (25 pts): Write the output of the following code, and explain why. #include <iostream> using namespace std; class A A(int n = 2) : x(n) cout << "A constructor" << endl; virtual void Add(int n) x+=n; virtual ~A() cout << x << endl; cout << "A destructor" << endl; int x; ; class B : public A B(int n) : A(n)

y=0; z=0; cout << "B constructor" << endl; void Add(int n) x = 0; y+=n; z = 1; ~B() cout << "B destructor"<< endl; std::cout << y << z; private: int y; int z; ; int main() int c=1; B *b = new B(5); A *a; a=b; a->add(c); delete a; std::cout << std::endl; return 0; Solution: A constructor B constructor B destructor 110 A destructor -- 5 pts each 3. (20 pts) Design and write code for three classes: Polygon, Rectangle, and Triangle. The classes need to provide constructors and area functions so that the following main function can print out the output shown below. The area of a rectangle can be computed with width*height, while the area of a triangle can be computed with width*height/2. One of the classes should be an abstract class. #include <iostream>

using namespace std; int main () Polygon * ppoly1 = new Rectangle; Polygon * ppoly2 = new Triangle; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; delete ppoly1; delete ppoly2; return 0; Output: 20 10 Solution: class Polygon int width, height; void set_values (int a, int b) width=a; height=b; virtual int area (void) =0; void printarea (void) cout << this->area() << endl; ; class Rectangle: public Polygon int area (void) return (width * height); ; class Triangle: public Polygon int area (void) return (width * height / 2); ;