COMP322 - Introduction to C++

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

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

Lecture 5: Inheritance

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

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Object Oriented Software Design II

Object Oriented Software Design II

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

What are the characteristics of Object Oriented programming language?

Increases Program Structure which results in greater reliability. Polymorphism

Object Oriented Programming. Solved MCQs - Part 2

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Fundamental Concepts and Definitions

Programming Languages: OO Paradigm, Polymorhism and Class Members

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Polymorphism. Zimmer CSCI 330


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

Sri Vidya College of Engineering & Technology

C++ Programming: Polymorphism

VIRTUAL FUNCTIONS Chapter 10

Polymorphism. Arizona State University 1

Object Oriented Programming with c++ Question Bank

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

Inheritance, and Polymorphism.

Subtyping (Dynamic Polymorphism)

Introduction to Programming Using Java (98-388)

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

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

Principles of Object Oriented Programming. Lecture 4

B.C.A 2017 OBJECT ORIENTED PROGRAMMING USING C++ BCA303T MODULE SPECIFICATION SHEET

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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

Programming, numerics and optimization

Chapter 5 Object-Oriented Programming

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

Constants, References

VALLIAMMAI ENGINEERING COLLEGE

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

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

Data Abstraction. Hwansoo Han

OBJECT ORIENTED PROGRAMMING USING C++

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

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

PROGRAMMING IN C++ COURSE CONTENT

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

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Object-Oriented Principles and Practice / C++

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Method Resolution Approaches. Dynamic Dispatch

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Study Guide to Exam 2

C++ Yanyan SHEN. slide 1

Software Design and Analysis for Engineers

Object-Oriented Principles and Practice / C++

Compiler construction 2009

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

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

Supporting Class / C++ Lecture Notes

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

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

Fast Introduction to Object Oriented Programming and C++

Lecture 10. Overriding & Casting About

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

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Instantiation of Template class

Object Oriented Programming in C++ Basics of OOP

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

COP 3530 Discussion Session #2 Ferhat Ay

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

W3101: Programming Languages C++ Ramana Isukapalli

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Absolute C++ Walter Savitch

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

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

INSTRUCTIONS TO CANDIDATES

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

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

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

EL2310 Scientific Programming

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS11 Advanced C++ Fall Lecture 7

Fundamentals of Programming Languages

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

Polymorphism Part 1 1

CPSC 427: Object-Oriented Programming

Short Notes of CS201

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Object typing and subtypes

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

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

Implements vs. Extends When Defining a Class

Transcription:

COMP322 - Introduction to C++ Winter 2011 Lecture 08 - Inheritance continued School of Computer Science McGill University March 8, 2011

Last Time Single Inheritance Polymorphism: Static Binding vs Dynamic Binding Abstract Classes

Public inheritance The use of the public keyword in the declaration of the derived class is the norm, but it must be present. One can specify private or protected inheritance, which hides inherited members. If you omit the public keyword, inheritance is private. class A { void f(); class B: A { // B inherits A privately void g(); int main() { A a; B b; a.f(); // OK b.g(); // OK b.f(); // Illegal

Static binding If a member function is not virtual, the choice of function to call is made at compile time: class A { int f(); class B : public A { int f(); int main() { B b; A *pa = &b; pa->f(); // Calls A::f() because pa is of type A * This is called static binding, and it is the default behavior in C++.

Dynamic binding If a member function is virtual, the choice of function to call is made at run time: class A { virtual int f(); class B : public A { int f(); int main() { B b; A *pa = &b; pa->f(); // Calls B::f() because pa points to a B * Called either dynamic binding, this is both more useful and less efficient than static binding.

Implementing pure virtual functions A pure virtual function can provide an implementation which could be used by derived classes. class A { virtual void f() = 0; void A::f() { //... class B : public A { void f(); void B::f() { A::f(); // call the base class // do more... The compiler will still refuse to create an object of class A!

Multiple inheritance Java includes the interface construct, which allows one to generically specify a group of functions which must be implemented by a derived class. C++ accomplishes the same thing through abstract classes and multiple inheritance. Multiple inheritance allows a class to be derived from two or more base classes. The derived class inherits all of the data and functions of each base class, which clearly raises the possibility of naming conflicts. Java interfaces are similar to a C++ abstract class with no data or function implementations. Both provide only the prototypes for functions which must be implemented by the derived class.

Multiple inheritance syntax The syntax of multiple inheritance is straightforward. Each base class can use private, public, or protected inheritance: class A { // base class 1 int x; void f(); class B { // base class 2 int y; void g(); class C : public A, public B { // C inherits from A & B int z; // Visible only within C //.. Class C will contain both functions and three variables.

Assignment compatibility A derived class is assignment compatible with any base class. class A { // base class 1 //... class B { // base class 2 //... class C : public A, public B { // C inherits from A & B //... int main() { A a; B b; C c; a = c; // OK b = c; // OK c = a; // Error return 0;

Assignment compatibility with pointers Assignment compatibility with pointers is maintained similarly. However, the conversion to different base classes may return different values. class A { // base class 1 //... class B { // base class 2 //... class C : public A, public B { // C is derived from A, B //... int main() { C c; A *pa = &c; B *pb = &c; //...

Sources of ambiguity Multiple inheritance can introduce ambiguities and conflicts: class A { void f(); class B { void f(); class C : public A, public B { void g() { f(); // Which f() do I invoke? This can be resolved by qualifying the call to f() as A::f(), for example.

Diamond inheritance An difficult situation arises when one class inherits from two others, both of which share a base class: class person { string name; void getname(); class student : public person { int year; // U0, U1, U2, etc.. void getyear(); class employee : public person { int year; // years of seniority void getyear(); class studentemployee : public student, public employee {

The diamond inheritance problem The problem is even worse than we might imagine at first glance. Our getyear() method is clearly ambiguous. So is the getname() method! Our student and employee classes both inherited from person. Internally, C++ represents derived class as a concatenation of the base and derived classes, so our studentemployee class contains two copies of person: person student person employee studentemployee

The diamond inheritance problem If we use getname() in our studentemployee class, it could refer to either person. This can be overcome with scope resolution: class studentemployee : public student, public employee { void f() { string s = employee::getname(); Assignment compatibility is now broken: int main() { person p1; studentemployee se1; p1 = se1; // Which person does the compiler use?? Again, there is a workaround: p1 = (employee) se1;

There are two solutions for diamond inheritance Avoid this situation at all costs! Use virtual inheritance: class person { string name; void getname(); class student : virtual public person { int year; // U0, U1, U2, etc.. void getyearofstudy(); class employee : virtual public person { int year; // years of seniority void getyearsofservice(); // avoid function name conflict class studentemployee : public student, public employee {

What is virtual inheritance? Virtual inheritance ensures that a single copy of the common base class is maintained in all derived classes. As with virtual functions and dynamic dispatch, the compiler adds a layer of indirection to accesses to the virtual base class. Virtual inheritance must be anticipated and applied above the point where any two classes with a common base class are joined. class A { class B: virtual public A { class C: virtual public A { class D: public B, public C { Rules for virtual inheritance are more complex than we can cover here.

Advanced type casting The complexity of C++ inheritance has inspired a number of additional type conversion operators. dynamic cast<type>(expression) - safely converts pointers and references among polymorphic types, with runtime checks. class A { /*... */ class B { /*... */ class C: public A, public B { /*... */ int main() { A *pa1 = new A; A *pa2 = new C; B *pb; C *pc; pc = dynamic_cast<c *>(pa1); // Returns NULL pc = dynamic_cast<c *>(pa2); // OK pb = dynamic_cast<b *>(pa2); // OK

Advanced type casting static cast<type>(expression) - converts among related classes with static checks. Unlike dynamic cast, it cannot consider the runtime type, it only considers the compile time type. int main() { A *pa1 = new A; A *pa2 = new C; B *pb; C *pc; pc = static_cast<c *>(pa1); // OK, but dangerous pc = static_cast<c *>(pa2); // OK pb = static_cast<b *>(pc); // OK pb = static_cast<b *>(pa2); // Compiler error

Advanced type casting reinterpret cast<type>(expression) - converts among any pointer types, with no checks or adjustments. This can lead to extremely dangerous situations, as we can convert among completely unrelated types! int main() { A *pa1 = new A; B *pb; C *pc; pc = reinterpret_cast<c *>(pa1); // Legal but dangerous pb = reinterpret_cast<b *>(pa1); // Legal but dangerous

Generic Programming Generic Programming is a programming paradigm for developing efficient, reusable software libraries

Introduction to Generic Programming Algorithms are written independently of data Data types are filled in during execution Number of algorithms are data-type independent: sorting searching finding n-th largest swapping, etc Write once, use many times philosophy dramatically reduces lines-of-code makes code much easier to maintain

Templates In C++, templates = generic programming A template is a routine in which some parameters are qualified by a type variable. The template is then specialized for each combination of argument types that occur in practice We can define Function Templates Class Templates In Java, generic programming is implemented using inheritance: All the collections API classes are written in terms of the Object class, and since all objects in Java are instances of this class, these collections are usable by all class types.

Function Templates Special functions that can operate with generic types A template parameter is a special kind of parameter used to pass a type as an argument Just like regular function parameters, but pass types to a function Declaration Format: template <class identifier > function_declaration;

Function Templates - Example template <class T> T getmax(t a, T b){ return (a>b? a: b); int main(){ int i=5,j=6,k; long l=10,m=5,n; k = getmax<int>(i,j); n = getmax<long>(l,m); cout<< k << endl; cout<< n <<endl; return 0; Note: You cannot mix int and long types: There is one template type only in definition.

Function Templates - Multiple template types Define template types after the template keyword template <class T1, class T2> T getmax(t1 a, T2 b){ return (a>b? a: b);

Class Templates Can have members that use template parameters as types template <class T> class myclass{ private: T *storage; //... More Next Time