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

Similar documents
Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Inheritance, and Polymorphism.

Chapter 14 Abstract Classes and Interfaces

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

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

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Use the template below and fill in the areas in Red to complete it.

CS 113 PRACTICE FINAL

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Polymorphism Part 1 1

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Java Object Oriented Design. CSC207 Fall 2014

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Object-Oriented Concepts

Questions Answer Key Questions Answer Key Questions Answer Key

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

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

Class, Variable, Constructor, Object, Method Questions

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Full file at Chapter 2 - Inheritance and Exception Handling

Polymorphism CSCI 201 Principles of Software Development

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CSE 303: Concepts and Tools for Software Development

Object Oriented Programming. Java-Lecture 11 Polymorphism

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Rules and syntax for inheritance. The boring stuff

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Object Oriented Software Design II

Object Oriented Software Design II

C11: Garbage Collection and Constructors

Inheritance Motivation

Practice for Chapter 11

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Object-Oriented Programming (OOP) Fundamental Principles of OOP

C10: Garbage Collection and Constructors

ECE 461 Fall 2011, Final Exam

Chapter 5. Inheritance

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Friend Functions, Inheritance

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

CMSC 202 Midterm Exam 1 Fall 2015

Java Fundamentals (II)

Inheritance and Polymorphism

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

Arrays Classes & Methods, Inheritance

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

INHERITANCE AND EXTENDING CLASSES

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

Chapter 10 Classes Continued. Fundamentals of Java

CS1150 Principles of Computer Science Objects and Classes

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JAVA Programming Language Homework I - OO concept

Implementing an ADT with a Class

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

Programming overview

Midterm Exam 5 April 20, 2015

CMSC 132: Object-Oriented Programming II

ITI Introduction to Computing II

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Inheritance and Polymorphism. CS180 Fall 2007

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

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

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

Inheritance. Transitivity

OVERRIDING. 7/11/2015 Budditha Hettige 82

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

CS11 Introduction to C++ Fall Lecture 7

ITI Introduction to Computing II

Questions Answer Key Questions Answer Key Questions Answer Key

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Inheritance and Polymorphism

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

CST242 Object-Oriented Programming Page 1

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

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

Comments are almost like C++

Week 5-1: ADT Design

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

ECE Fall 20l2, Second Exam

Transcription:

Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013

Question 1 What is the output of the following C++ program? #include <iostream> #include <string> using namespace std; class Circle public: Circle(double radius) this->radius = radius; void put() const cout << "Radius = " << radius; private: double radius; ; class ColoredCircle: public Circle public: ColoredCircle(double radius, string color); void put() const; private: string color; ; ColoredCircle::ColoredCircle(double radius, string color)

: Circle(radius), color(color) void ColoredCircle::put() const Circle::put(); cout << " Color = " << color; int main() ColoredCircle redcircle(100., "red"); Circle* circle1 = &redcircle; circle1->put(); cout << endl; Circle circle(50.); Circle* circle2 = &circle; circle2->put(); cout << endl; return 0; Output Radius = 100 Radius = 50 Question 2

change? Modify the program so that the put function is virtual. What is the output after that Answer 2 I changed the line "void put() const cout << "Radius = " << radius;" to "virtual void put() const cout << "Radius = " << radius;", then I got the following output. Output Radius = 100 Color = red Radius = 50 Question 3 Does Java allow both virtual and nonvirtual methods? If not, which does it allow? Rewrite this program in Java and identify at least four differences between the programs in the two languages. Answer 3 Circle and ColoredCircle Classes in Java // Circle.java public class Circle // Super class private double radius; // A member variable of super class Circle(double radius) // Constructor of super class this.radius = radius;

public void put() System.out.print("Radius = " + radius + " "); public static void main(string[] args) // Main method // Instantiates a super class object Circle circle1 = new ColeredCircle(100., "red"); circle1.put(); System.out.println(); // Instantiates a sub class object Circle circle2 = new Circle(50.); circle2.put(); System.out.println(); // ColeredCircle.java public class ColeredCircle extends Circle // Sub class private String color; ColeredCircle(double radius, String color) // Constructor of sub class super(radius); this.color = color;

@Override public final void put() // Overrided put() method super.put(); System.out.print("Color = " + color); Output Radius = 100.0 Color = red Radius = 50.0 as follows. I identified four differences on inheritance and method overriding between C++ and Java Overriding Modifiers Reference to Superclass C++ You need to add virtual operator to functions expressly. If you add const modifier to an overriding method in a superclass, you can compile the program by adding const to overridden method in a subclass. To reference to a superclass in a subclass, you use scope resolution operator. Java Basically, methods are virtual by default except for methods with final modifier. If multiple methods with the same name in a superclass and a subclass exist, the method in subclass is automatically overridden. If you add final modifier to overriding method, you cannot override the method in a subclass. To reference to a superclass in a subclass, you just use keyword super.

Calling Overridden Functions/Methods You can call overridden class member functions through pointer variables. You can call overridden class member methods through objects of the class. Question 4 This program contains an example of object slicing. On what line does it occur? Why must it happen? Answer 4 Object slicing is occurred in the line of "circle1 = redcircle;" in the main method as illustrated in Figure 1 and Figure 2 below. It happens when you assign an object value of subclass to a superclass object. Figure 1 Figure 2 Question 5

Explain why this never happens in Java. Do some investigating and determine how C# avoids this problem. Answer 5 Object slicing doesn t happen in Java because all object variables are references. When you assign a subclass instance to a superclass variable, you just copy the reference. In C#, using pointers are allowed only in classes or methods with unsafe context. Therefore, programmers can avoid problems due to object slicing unconsciously.