Programming in C++: Assignment Week 6

Similar documents
Programming in C++: Programming Test-1

Programming in C++: Assignment Week 5

Programming in C++: Programming Test-2

Programming in C++: Assignment Week 6

Programming in C++: Assignment Week 5

Inheritance, and Polymorphism.

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Midterm Exam 5 April 20, 2015

Inheritance

Friend Functions, Inheritance

Programming in C++: Assignment Week 8

CS OBJECT ORIENTED PROGRAMMING

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++: Assignment Week 1

C++ Quick Guide. Advertisements

Object Oriented Software Design II

VIRTUAL FUNCTIONS Chapter 10

Object Oriented Software Design II

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:

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 Structures (INE2011)

Polymorphism Part 1 1

15: Polymorphism & Virtual Functions

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Programming in C++: Assignment Week 4

COEN244: Polymorphism

Programming in C++: Assignment Week 4

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

Chapter 1: Object-Oriented Programming Using C++

SSE2034: System Software Experiment 3 Spring 2016

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

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

Programming in C++: Assignment Week 3

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

Polymorphism CSCI 201 Principles of Software Development

Inheritance and aggregation

C++ Programming: Polymorphism

Comp151. Inheritance: Initialization & Substitution Principle

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

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

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

Increases Program Structure which results in greater reliability. Polymorphism

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.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

Content. Inheritance 6. Object Oriented Programming

Upcasting. Taking an object reference and treating it as a reference to its base type is called upcasting.

CS

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

First Chocolate Code-Off for CISC220 Fall 17

Consider the following example where a base class has been derived by other two classes:

COP 3530 Discussion Session #2 Ferhat Ay

Class Sale. Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill

Programming in C++: Assignment Week 3


C++ Structures Programming Workshop 2 (CSCI 1061U)

Inheritance Examples

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

C++_ MARKS 40 MIN

Inheritance 6. Content. Inheritance. Reusability in Object-Oriented Programming. Inheritance

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

Object oriented programming

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

COMP322 - Introduction to C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Review Questions for Final Exam

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

Assumptions. History

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CS 11 C++ track: lecture 1

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Polymorphism. Zimmer CSCI 330

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

Lecture 14: more class, C++ streams

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

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

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Programming Languages: OO Paradigm, Polymorhism and Class Members

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

ECE 461 Fall 2011, Final Exam

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Simula. Where Would We Be Without It? By Joe Casey

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

WARM UP LESSONS BARE BASICS

Object Oriented Programming: Inheritance Polymorphism

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

CMPS 221 Sample Final

Programming in C++: Assignment Week 2

Engineering Tools III: OOP in C++

Introduction to Object-Oriented Programming with C++

Inheritance and Overloading. Week 11

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.

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

Transcription:

Programming in C++: Assignment Week 6 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 6, 2017 Question 1 Look at the code snippet below and find out What will be the correct syntax for up-casting? Mark 1 class Parent { void sleep() { class Child: public Parent { void gotoschool(){ int main(){ Parent parent; Child child; a) Child *pchild = (Child *) &parent; b) Child *pchild = &parent; c) Parent *pparent = &child; d) Parent *pparent = (Parent *) &child; Answer: c) Explanation: By definition of upcasting (Base Class to Derive class) Question 2 What will be the correct syntax for down-casting in above code (QS No-1)? a) Child *pchild = (Child *) &parent; 1

b) Child *pchild = &parent; c) Parent *pparent = &child; d) Parent *pparent = (Parent *) &child; Answer: a) Explanation: By definition of downcasting (Derive class to Base Class) Question 3 Look at the following code snippet and Find out the static binding from the option below Mark 1 class Base { void first() { virtual void second() { class Derived : public Base { void f() { virtual void g() { int main() { Base b; Derived d; Base *pb = &b; Base *pd = &d; Base &rb = b; Base &rd = d; pb->first(); // 1 pd->first(); // 2 rb.first(); // 3 rd.first(); // 4 pb->second(); // 5 pd->second(); // 6 rb.second(); // 7 rd.second(); // 8 a) 1 2 5 6 b) 3 4 7 8 c) 1 2 3 4 d) 5 6 7 8 Answer: c) Explanation:Direct function calls can be resolved using a process known as early binding or static binding. 2

Question 4 Consider the code of question 3 and find out the dynamic binding from the option below a) 1 2 5 6 b) 3 4 7 8 c) 1 2 3 4 d) 5 6 7 8 Answer: d) Explanation: Virtual functions get resolve at run time. Question 5 Consider the following code snippet. Find out the correct sequence of function calling. Marks: 1 class X { virtual void f() { //1 void g() { //2 class Y : public X { void f() { //3 virtual void g() { //4 int main() { X x; Y y; X& rx = y; x.f(); x.g(); rx.f(); rx.g(); a) 1 2 3 2 b) 1 2 3 4 c) 3 2 1 2 d) 3 2 1 4 Answer: a) Explanation: rx.f() call Y::f as f is virtual in the base class. 3

Question 6 What will be the output/error of the bellow code snippet? class Base { virtual void show() = 0; class Derived : public Base{ void show(){ cout << "Virtual Function"; int main() { Base obj; Base *b; Derived d; b = &d; b->show(); a) Compile Time Error: cannot create object b) Compile Time Error: cannot inherit Base class c) Compile Time Error: cannot overload show d) Virtual Function Answer: a) Explanation: Can t create of object of a abstract base class Question 7 What will be the output of the following program? Marks: 1 #include<iostream> using namespace std; class base { base() { cout << "c"; virtual ~base() { cout << "~c"; class derived : public base { derived() { cout << "d"; ~derived() { cout << "~d"; int main(void) 4

{ { derived *d = new derived(); base *b = d; delete b; a) d c c d b) d c d c c) c d c d d) c d d c Answer: d) Explanation: Deleting a derived class object using a pointer to a base class that has a nonvirtual destructor results in undefined Question 8 Find the abstract classes from the bellow code? class Instrument { virtual void play() = 0 { cout << "Instrument: Init Brush" << endl; class Wind : public Instrument { void play() { cout << "Polygon: play" << endl; class Percussion : public Instrument { class Woodwind : public Wind { void play() { cout << "Woodwind: play" << endl; class Brass : public Wind { void play() { cout << "Brass: play" << endl; class Drum : public Percussion { void play() { cout << "Drum: play" << endl; class Tambourine : public Percussion { void play() { cout << "Tambourine: play" << endl; a) Woodwind, Brass 5

b) Instrument, Percussion c) Instrument, Tambourine d) Percussion, Drum Answer: b) Explanation: pure virtual function play() is there in Instrument and Percussion class Question 9 What will be the output of the following Code Snippet? Marks: 1 class Instrument { virtual void play() { cout << "1 "; class Wind : public Instrument { void draw() { Instrument::play(); cout << "2 "; class Percussion : public Instrument { class Woodwind : public Wind { void play() { Instrument::play(); cout << "3 "; class Brass : public Wind { void play() { Instrument::play(); cout << "4 "; class Drum : public Percussion { void play() { Instrument::play(); cout << "5 "; class Tambourine : public Percussion { void play() { Instrument::play(); cout << "6 "; int main() { Instrument *arr[] = { new Woodwind, new Brass, new Drum, new Tambourine for (int i = 0; i < sizeof(arr) / sizeof(instrument *); ++i) arr[i]->play(); a) 3 4 5 6 b) 1 3 1 4 1 5 1 6 c) 1 3 4 5 6 d) Compile Time Error: Virtual function can t have body Answer: b) Explanation:: Virtual function may have body 6

Question 10 What will be the output of the following code snippet? Marks: 1 class A { int i; class B { double d; A a; B b; a.i = 8; b.d = 9.7; A *p = &a; B *q = &b; p = (A*)&b; q = (B*)&a; cout << p->i << endl; cout << q->d << endl; a) 9.7 8 b) 8 9.7 c) GARBAGE d) Compile Time Error: casting is not possible Answer: c) Explanation: Force casting between unrelated classes Programming Questions Question 1 Problem Statement: Marks: 2 Consider the following code. Modify the code in editable section to match the public test cases. #include <iostream> using namespace std; class B { B() { cout << "98 "; // don t modify the "cout" ~B() { cout << "56 "; // Don t Edit/Modify the "cout" class D : public B { 7

int n; D(int p):n(p) { cout << n << " "; ~D() { cout << n*2 << " "; int main() { int n ; cin >> n ; B *baseptr = new D(n); delete baseptr; //--------------------------------------------------------------------- Public-1 2 98 2 4 56 Public-2 8 98 8 16 56 Private 3 98 3 6 56 Answer: virtual Question 2 Consider the skeletal code below. When the program is executed, the member functions (excluding the constructors and destructors) are called in the order: A::g B::g B::f A::g B::g C::g C::f. Fill up the blanks to match the test cases. Marks: 2 #include <iostream> using namespace std; class A { protected: int ai; A(int i) : ai(i) { void f() = 0; // Fill the blank or Remove blank void g() // Fill the blank or Remove blank { ++ai; 8

class B : public A { protected: int bi; B(int i) : A(i), bi(i) { void f() // Fill the blank or Remove blank { cout << ai << bi; // DO NOT EDIT THIS LINE void g() // Fill the blank or Remove blank { A::g(); class C : public B { int ci; C(int i) : B(i), ci(i) { void f() //Fill the blank or Remove blank { cout << ai << bi << ci; // DO NOT EDIT THIS LINE void g() //Fill the blank or Remove blank { B::g(); int main() { int x = 3 ; int y; cin >> y; A *p[] = { new B(x), new C(y) for(int i = 0; i < sizeof(p) / sizeof(a*); ++i) { p[i]->g(); p[i]->f(); Further, the input / output test case is as follows: Public 1 2 43322 Public 2 23 43242323 Private 4 43544 Answer: virtual // virtual // virtual or blank // virtual or blank // virtual or blank // virtual or blank Question 3 Consider the following code. write the proper definition of getarea() in the editable section so that the test cases would pass. Marks: 2 9

#include <iostream> using namespace std; class Shape { protected: int width, height; // Write the getarea() function here void setwidth(int w) { width = w; void setheight(int h) { height = h; class Rectangle : public Shape { int getarea() { return (width * height); class Triangle : public Shape { int getarea() { return (width * height) / 2; int main(void) { int x, y; cin >> y >> x; Rectangle Rect; Triangle Tri; Rect.setWidth(x); Rect.setHeight(y); Tri.setWidth(x); Tri.setHeight(y); Shape *shape[] = { &Rect, &Tri, 0 Shape **pshape = &shape[0]; while (*pshape) cout << (*pshape++)->getarea() << " "; 10

Public-1 3 7 21 10 Public-2 35 23 805 402 Private 6 9 54 27 Answer: virtual int getarea() = 0; Question 4 Consider the following code. Fill up the code in editable section to congratulate the Manager and the Clerk, so that outputs will be matched as given in the test cases. Marks: 2 #include <iostream> #include <string> using namespace std; class Employee { string Name; double salary; Employee(string fname, double sal) : Name(fName), salary(sal) { void show() { cout << Name << " " << salary; void addbonus(double bonus) { salary += bonus; class Manager :public Employee { Manager(string fname, double sal) : Employee(fName, sal) { 11

class Clerk :public Employee { Clerk(string fname, double sal) : Employee(fName, sal) { void congratulate(employee* emp) { emp->addbonus(200); emp->show(); cout << " "; int main() { Employee* emp; int sal_m, sal_c; cin >> sal_c >> sal_m; Manager m1("steve", sal_m); Clerk c1("kevin", sal_c); // Call the proper function to congratulate the Manager and the Clerk Public-1 2000 1000 Kevin 2200 Steve 1200 Public-2 4000 1000 Kevin 4200 Steve 1200 Private 6200 2500 Kevin 6400 Steve 2700 Answer: congratulate(&c1); congratulate(&m1); 12

Question 5 #include <iostream> using namespace std; class Shape { protected: int width, height; Shape(int a = 0, int b = 0) { width = a; height = b; // Fill the blank to run the code void area() { cout << "Parent class area :" << endl; class Rectangle : public Shape { Rectangle(int a = 0, int b = 0) :Shape(a, b) { void area() { cout << (width * height) << " "; class Triangle : public Shape{ Triangle(int a = 0, int b = 0) :Shape(a, b) { void area() { cout << (width * height / 2); // Main function for the program int main() { int a, b; cin >> a; cin >> b; Shape *shape; Rectangle rec(a, b); Triangle tri(a, b); // Write the code to call rectangle area. shape->area(); // Write the code to call triangle area. 13

shape->area(); Public-1 10 5 50 25 Public-2 10 25 250 125 Private 60 30 1800 900 Answer: virtual shape = &rec; shape = &tri; 14