Programming in C++: Assignment Week 3

Similar documents
Programming in C++: Assignment Week 3

Programming in C++: Assignment Week 1

Programming in C++: Assignment Week 2

Programming in C++: Assignment Week 4

Programming in C++: Assignment Week 4

Recharge (int, int, int); //constructor declared void disply();

C++ 8. Constructors and Destructors

More class design with C++ Starting Savitch Chap. 11

AN OVERVIEW OF C++ 1

Advanced Systems Programming

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

G52CPP C++ Programming Lecture 13

Constants, References

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Programming in C++: Programming Test-2

Pointers. Developed By Ms. K.M.Sanghavi

Programming in C++: Assignment Week 6

Programming in C++: Assignment Week 8

EL2310 Scientific Programming

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

Exam 3 Chapters 7 & 9

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CMPS 221 Sample Final

Programming in C++: Assignment Week 8

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Classes and Objects. Class scope: - private members are only accessible by the class methods.

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

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Module Operator Overloading and Type Conversion. Table of Contents


Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CMSC 202 Midterm Exam 1 Fall 2015

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Pointers II. Class 31

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

COMP 2355 Introduction to Systems Programming

Object-Oriented Principles and Practice / C++

University of Toronto

CS 11 C++ track: lecture 1

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Separate Compilation Model

Object-Oriented Programming, Iouliia Skliarova

CSCE 110 PROGRAMMING FUNDAMENTALS

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

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Constructors & Destructors

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

ComS 228 Exam 1. September 27, 2004

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

6.096 Introduction to C++ January (IAP) 2009

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

CPSC 427: Object-Oriented Programming

C++ For Science and Engineering Lecture 15

Overloading Operators in C++

Constructors for classes

Laboratorio di Tecnologie dell'informazione

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

5.3.1 Different Numbers of Arguments Example:

Classes and Pointers: Some Peculiarities

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Function Overloading

Intermediate Programming, Spring 2017*

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Homework Assignment #1

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

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

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CPSC 427: Object-Oriented Programming

C++ For Science and Engineering Lecture 27

More Tutorial on C++:

Implementing Abstract Data Types (ADT) using Classes

Ch. 12: Operator Overloading

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

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

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

Array Elements as Function Parameters

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Object Oriented Software Design II

Pointers, Dynamic Data, and Reference Types

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Midterm Review. PIC 10B Spring 2018

The University of Nottingham

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

CPSC 427: Object-Oriented Programming

Dynamic arrays / C Strings

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Transcription:

Programming in C++: Assignment Week 3 Total Marks : 20 August 6, 2017 Question 1 What is the output of the sizeof operator for t in the following code snippet? sizeof(int) = 4) Mark 1 (Assume #include<iostream> class Test { int var; int arr[9]; void display() { int a; ; Test t; cout << sizeof(t) << " "; a) 40 b) 36 c) 44 d) Default size: 0 Answer: a) Explanation: Sum of memory requirements for all the data members Question 2 What will be the output of the following program? Mark 1 #include<iostream> class Sample { int data_; Sample(): data_(2){ ; 1

Sample s; s.data_ = 1; cout << Sample.data_; a) 1 b) 2 c) Compilation Error: Sample() is private d) 0 Answer: c) Explanation: Constructor is private, hence object construction not possible Question 3 What is the output of the program? Mark 1 #include<iostream> class Test { private: int x_; int y_; void func() { x_ = y_ = 1; cout << x_ << " " << y_; ; Test t; t.func(); a) 1 1 b) Compilation error: Constructor not defined c) Compilation error: Cannot access private member x and y d) Compilation error: Illegal access of func() Answer: a) Explanation: private members can be accessed in public methods Question 4 Consider Object S of class Sample. What is the type of this pointer? Mark 1 a) S const * const this 2

b) S * const this c) S * this d) const S const * this Answer: b) Explanation: As per syntax of this pointer Question 5 What will be the output of the program? Mark 2 #include <string> class Sample { string name; Sample(string s): name(s) { cout << name << " Created" << " "; ; ~Sample() { cout << name << " Destroyed" << " "; Sample s1("s1"), s2("s2"); a) S1 Created S2 Created S2 Destroyed S1 Destroyed b) S1 Created S2 Created S1 Destroyed S2 Destroyed c) S2 Created S1 Created S2 Destroyed S1 Destroyed d) S1 Created S1 Destroyed S2 Created S2 Destroyed Answer: a) Explanation: order of calling constructors and destructors, when the object goes out of scope. The last constructed class is destroyed first. See Slides. 3

I Programming Assignment Question 1 Fill the blank with the proper constructor and copy constructor to get the output as per the test cases. Marks 2 class Complex { double *re, *im; Complex( ) { re = new double(r); im = new double(m); Complex( ){ re = new double; im = new double; *re = *t.re; *im= *t.im; ~Complex(){ delete re, im; ; double x, y, z; cin >> x >> y >> z; Complex n1(x,y); cout << *n1.re << "+" << *n1.im << "i "; Complex n2 = n1; cout << *n2.re << "+" << *n2.im << "i "; *n1.im = z; cout << *n2.re << "+" << *n2.im << "i "; cout << *n1.re << "+" << *n1.im << "i "; Answer: double r, double m // const Complex &t Explanation: The first parameters are for the constructor, the second arguments are for the copy constructor which passes a constant Complex object, so that the value of the data members are not changed. a. Input: 4, 5, 6 Output: 4+5i 4+5i 4+5i 4+6i b. Input: 4, 5, 5 Output: 4+5i 4+5i 4+5i 4+5i c. Input: 6 7 8 Output: 6+7i 6+7i 6+7i 6+8i 4

Question 2 Fill the blank in the constructor to get the output as per the test cases. Marks 2 class Sample { int data_ ; char graph_, data_or_graph_; Sample( ): data_(x), data_or_graph_(z), graph_(p){ cout << data_ << " " << data_or_graph_<< " " << graph_ <<" "<<endl; ; int x; char y; cin>>x >> y ; Sample s1(x, y), s3; Answer: int x = 6, char z = C, char p = A // data (x), data or graph (z), graph (y) Explanation: : Evaluation of S3 gives 6 C A hence we get the default values. The rest of the syntax is as per slides. a. Input: 4 D Output: 4 D A 6 C A b. Input: 71 N Output: 71 N A 6 C A Question 3 Fill in blank with proper access specifier and function definitions of the class Stack to get the output as per the test cases. Marks 2 #include <vector> #include<string.h> class Stack { : // Write the appropriate Access specifier vector<char> data_; int top_; int empty() { ; void push(char x) { ; void pop() { ; char top() { ; ; Stack s; 5

char str[20]; cin >> str; s.data_.resize(100); s.top_ = -1; for(int i = 0; i < strlen(str) ; ++i) s.push(str[i]); while (!s.empty()) { cout << s.top(); s.pop(); s.pop(); Answer: public // return (top == -1) // data [++top ] = x // top // return data [top ] Explanation: Access specifier will be public as the data members are accessed outside class. The functions are standard stack functions, refer slides a. Input: erty Output: yr b. Input: ghjilk Output: kih c. Input: ADAM ; Output: MD Question 4 Look into the main() function write the proper constructor by filling the blank to get the output as per the test cases. Marks 2 #include <cmath> class Complex { private: double re_, im_; Complex(double re = 4.0, double im = 5.0): re_(re), im_(im) { cout << "Ctor: (" << re_ << ", " << im_ << ")" << endl; ~Complex() { cout << "Dtor: (" << re_ << ", " << im_ << ")" << endl; void print() { cout << " " << re_ << "+j" << im_ << " " << endl; ; ; cout << "main" << endl; double x, y; cin >> x; cin >> y; Complex d(x); Complex e; c.print(); d.print(); 6

Answer: Complex c(8, 4) Explanation: Complex object defined before the scope of main a. Input: 5 6 Output Ctor: (8, 4) main Ctor: (5, 5) Ctor: (4, 5) 8+j4 5+j5 Dtor: (4, 5) Dtor: (5, 5) Dtor: (8, 4) b. Input: 2.5 3.5; Output Ctor: (8, 4) main Ctor: (2.5, 5) Ctor: (4, 5) 8+j4 2.5+j5 Dtor: (4, 5) Dtor: (2.5, 5) Dtor: (8, 4) Question 5 The program indicates the concept of mutability. Fill the blank with appropriate kew words to satisfy the given test casesmarks 2 class MyClass { int mem_; int x_; MyClass(int m, int mm) : mem_(m), x_(mm) { int getxmem() { return x_; void setxmem(int i) { x_ = i; ; int x, y,z; cin >> x; cin >> y; 7

cin >> z; const MyClass myconstobj(x, y); myconstobj.setxmem(z); cout << myconstobj.getxmem() << endl; Answer: mutable // const // const Explanation: A mutable data member x only can be accessed and updated in a const member function. a. Input: 5 7 8 ; Output: 8 b. Input: 0, 1, 0 ; Output: 0 c. Input: 11, 11, 11 ; Output: 11 8