COP 3530 Discussion Session #2 Ferhat Ay

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

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

Inheritance, and Polymorphism.

Object Oriented Programming

CGS 2405 Advanced Programming with C++ Course Justification

Data Abstraction. Hwansoo Han

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

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

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

Polymorphism. Zimmer CSCI 330

Data Structures and Other Objects Using C++

Programming, numerics and optimization

Polymorphism. Arizona State University 1

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

Short Notes of CS201

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS201 - Introduction to Programming Glossary By

CS250 Final Review Questions

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

COMP322 - Introduction to C++

Written by John Bell for CS 342, Spring 2018

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (classes) Hwansoo Han

AN OVERVIEW OF C++ 1

Make Classes Useful Again

Classes and Pointers: Some Peculiarities

Object Oriented Software Design II

Object Oriented Software Design II

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

VIRTUAL FUNCTIONS Chapter 10

Midterm Exam 5 April 20, 2015

C++ Important Questions with Answers

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 1: Object-Oriented Programming Using C++

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

C++ 8. Constructors and Destructors

C++ Inheritance and Encapsulation

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

CSE 303: Concepts and Tools for Software Development

Java Object Oriented Design. CSC207 Fall 2014

OBJECT ORIENTED PROGRAMMING

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

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

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

Object Oriented Programming. Solved MCQs - Part 2

EL2310 Scientific Programming

This examination has 11 pages. Check that you have a complete paper.

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

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

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

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

Object-Oriented Concept

15: Polymorphism & Virtual Functions

Fast Introduction to Object Oriented Programming and C++

CS 11 C++ track: lecture 1

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Homework 4. Any questions?

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Polymorphism Part 1 1

ECE 3574: Dynamic Polymorphism using Inheritance

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

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

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

Inheritance. Program construction in C++ for Scientific Computing. School of Engineering Sciences. Introduction. Michael Hanke.

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

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 11. The first objective here is to make use of the list class definition from a previous lab. Recall the class definition for ShapeList.

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Object Oriented Programming: Inheritance Polymorphism

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

Introduction to Object-Oriented Programming with C++

Object Oriented Programming with c++ Question Bank

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!

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

CS250 Final Review Questions

CS

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

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

CS304 Object Oriented Programming

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

Seminar on Simulation of Telecommunication Sistems. Introduction to object-oriented programming: C++

Review Questions for Final Exam

Inheritance, Polymorphism and the Object Memory Model


EL2310 Scientific Programming

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Object Oriented Programming

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

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

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Transcription:

COP 3530 Discussion Session #2 Ferhat Ay COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 1

Few things about me My name is difficult! Fer-hat, Fur-hot, Far-had, Frad, Fred, Frank, or just say F. I m a PhD student (terrible life choice) Research interests: Bioinformatics, Biological Networks http://www.cise.ufl.edu/~fay Interests: Soccer, driving Email me: fay@cise.ufl.edu Facebook page? Some sort of communication? COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 2

Why are we here? Inheritance Static Binding - Dynamic binding (Virtual functions) Polymorphism Multiple Inheritance Abstract Classes (Pure Virtual Functions) Hints about HW1!! Game time! COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 3

Inheritance base class, parent class, superclass derived class, child class, subclass COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 4

What a derived class ------ Derived Class can inherit All the member data (may not always have the right to access) All the member functions (may not always have the right to access) The same initial data layout as the base class cannot inherit X Constructors, destructors, assignment operator can add + New member data + New member functions + New constructors, destructors COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 5

Member Functions COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 6

Constructors, Destructors COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 7

Creation, Deletion Derived object created: Space allocated for the full object (base + derived members) Constructor of base class is called to initialize base data members Constructor of derived class is called to initialize derived class data members Derived class object is good to go Destroyed (goes out of scope or deleted) : Destructor of derived class is called first Destructor of base class is called next All the allocated space for the object is given back COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 8

Static vs Dynamic Binding COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 9

Virtual Destructor Rule If a class has at least one virtual member function, a virtual destructor is needed. A virtual destructor causes the compiler to use dynamic binding when calling the destructor. class Mario{.. public:.. virtual ~Mario() {cout<< Game Over! <<endl;}.. } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 10

Polymorphism in C++ Static Polymorphism Function/Operator Overloading Class/function templates Dynamic polymorphism Polymorphism through inheritance/virtual member functions COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 11

Polymorphism-Example #include <iostream> class Person { public: virtual void print(){std::cout << "a person";} virtual ~Person() {} }; class Student: public Person { public: void print() {std::cout << "a student";} }; int main(){ Person* p = new Student; // A generic pointer, // set to point a Student p > print(); // "a student delete p; return 0; } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 12

Multiple Inheritance -Ambiguity class W { /*... */ }; class X : public W { /*... */ }; class Y : public W { /*... */ }; class Z : public X, public Y { /*... */ }; int main () { Z z; X* xptr = &z; // valid Y* yptr = &z; // valid W* wptr = &z; // error, ambiguous reference to class W // X's W or Y's W? } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 13

Multiple Inheritance Non-ambiguous class W { /*... */ }; class X : public virtual W { /*... */ }; class Y : public virtual W { /*... */ }; class Z : public X, public Y { /*... */ }; int main () { Z z; X* xptr = &z; // valid Y* yptr = &z; // valid W* wptr = &z; // valid, W is virtual, only one W exists } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 14

Restricted Inheritance COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 15

Casting in Inheritance COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 16

Abstract (Base) Classes class Shape { public: }; private: Shape ( Point2d& position, Color& c) : center(position), color(c) {}; virtual void rotate(double radians) = 0; virtual bool draw(screen &) = 0; virtual ~Shape(void) = 0; Point2d center; Color color; COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 17

Abstract (Base) Classes COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 18

One more example COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 19

COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 20

COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 21

COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 22

COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 23

Program 1.11 template <class T> void make2darray(t ** &X, int noofrows, int noofcols) { X = new T* [noofrows]; } for(int i=0; i < noofrows; i++){ } X[i] = new T [noofcols]; COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 24

Program 1.12 template <class T> void delete2darray(t ** &X, int noofrows) { for(int i=0; i < noofrows; i++){ delete [] X[i]; } delete []X; X=NULL; } HW1 COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 25

References & Resources: Google http://ocw.mit.edu/index.htm http://www.csse.monash.edu.au/~cema/courses/cse2305/topics/05.09.inheritance/html/text.html http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp http://www.cplusplus.com/doc/tutorial/inheritance/ http://www.compgeom.com/~piyush/teach/3330/slides/l14_inheritance.pdf http://cpp.datastructures.net/ COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 26

Anyone bored of World of Warcraft? loose lose COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 27