1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {



Similar documents
Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

C++ Overloading, Constructors, Assignment operator

Introduction to Programming Block Tutorial C/C++

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

C++FA 5.1 PRACTICE MID-TERM EXAM

C++ INTERVIEW QUESTIONS

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

C++ Crash Kurs. C++ Object-Oriented Programming

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Java Interview Questions and Answers

Coding conventions and C++-style

The C Programming Language course syllabus associate level

Glossary of Object Oriented Terms

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

C++ Programming Language

CIS 190: C/C++ Programming. Polymorphism

Compile-time type versus run-time type. Consider the parameter to this function:

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

An Incomplete C++ Primer. University of Wyoming MA 5310

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Object Oriented Software Design II

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

Advanced Data Structures

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Implementation Aspects of OO-Languages

CORBA Programming with TAOX11. The C++11 CORBA Implementation

El Dorado Union High School District Educational Services

AP Computer Science Java Subset

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Moving from CS 61A Scheme to CS 61B Java

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Computer Programming C++ Classes and Objects 15 th Lecture

C++FA 3.1 OPTIMIZING C++

Class 16: Function Parameters and Polymorphism

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Semantic Analysis: Types and Type Checking

Description of Class Mutation Mutation Operators for Java

Storage and linkage specifiers

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Sources: On the Web: Slides will be available on:

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

CS 111 Classes I 1. Software Organization View to this point:

Constructor, Destructor, Accessibility and Virtual Functions

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Part I. Multiple Choice Questions (2 points each):

CISC 181 Project 3 Designing Classes for Bank Accounts

Introduction to Object-Oriented Programming

Konzepte objektorientierter Programmierung

1. Polymorphism in C++...2

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Chapter 5 Names, Bindings, Type Checking, and Scopes

Variable Base Interface

Curriculum Map. Discipline: Computer Science Course: C++

Applied Informatics C++ Coding Style Guide

16 Collection Classes

sqlpp11 - An SQL Library Worthy of Modern C++

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Contents. 9-1 Copyright (c) N. Afshartous

OpenCL Static C++ Kernel Language Extension

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Subject Name: Object Oriented Programming in C++ Subject Code:

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Java Application Developer Certificate Program Competencies

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

History OOP languages Year Language 1967 Simula Smalltalk

Classes and Pointers: Some Peculiarities (cont d.)

Object Oriented Software Design II

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Comp151. Definitions & Declarations

URI and UUID. Identifying things on the Web.

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

The programming language C. sws1 1

Ch 7-1. Object-Oriented Programming and Classes

Crash Course in Java

J a v a Quiz (Unit 3, Test 0 Practice)

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

13 Classes & Objects with Constructors/Destructors

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Fundamentals of Java Programming

Keil C51 Cross Compiler

Java (12 Weeks) Introduction to Java Programming Language

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Introduction to Object-Oriented Programming

Pemrograman Dasar. Basic Elements Of Java

Transcription:

Operators C and C++ 6. Operators Inheritance Virtual Alastair R. Beresford University of Cambridge Lent Term 2008 C++ allows the programmer to overload the built-in operators For example, a new test for equality: 1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } An operator can be defined or declared within the body of a class, and in this case one fewer argument is required; for example: 1 bool Complex::operator==(Complex b) { 2 return re==b.real() && im==b.imag(); 3 } Almost all operators can be overloaded 1 / 19 2 / 19 Streams Overloaded operators also work with built-in types Overloading is used to define a C++ printf ; for example: 1 #include <iostream> 2 3 int main() { 4 const char* s = "char array"; 5 6 std::cout << s << std::endl; 7 8 //Unexpected output; prints &s[0] 9 std::cout.operator<<(s).operator<<(std::endl); 10 11 //Expected output; prints s 12 std::operator<<(std::cout,s); 13 std::cout.operator<<(std::endl); 14 return 0; 15 } The this pointer If an operator is defined in the body of a class, it may need to return a reference to the current object The keyword this can be used For example: 1 Complex& Complex::operator+=(Complex b) { 2 re += b.real(); 3 this->im += b.imag(); 4 return *this; 5 } 3 / 19 4 / 19

Class instances as member variables Temporary objects A class can have an instance of another class as a member variable How can we pass arguments to the class constructor? New notation for a constructor: 1 class X { 2 Complex c; 3 Complex d; 4 X(double a, double b): c(a,b), d(b) { 5... 6 } 7 }; This notation must be used to initialise const and reference members It can also be more efficient Temporary objects are often created during execution A temporary which is not bound to a reference or named object exists only during evaluation of a full expression Example: the string class has a function c_str() which returns a pointer to a C representation of a string: 1 string a("a "), b("string"); 2 const char *s = (a+b).c_str(); //Wrong 3... 4 //s still in scope here, but the temporary holding 5 //"a+b" has been deallocated 6... 5 / 19 6 / 19 Friends Inheritance C++ allows a class to inherit features of another: A (non-member) friend function can access the private members of a class instance it befriends This can be done by placing the function declaration inside the class definition and prefixing it with the keyword friend; for example: 1 class Matrix { 2... 3 friend Vector operator*(const Matrix&, \ 4 const Vector&); 5... 7 } 5 }; 6 7 class bicycle : public vehicle { 8 bool panniers; 9 public: 10 bicycle(bool p):vehicle(2),panniers(p) {} 11 }; 12 13 int main() { 14 bicycle(false); 15 } 7 / 19 8 / 19

Derived member function call Example Default derived member function call semantics differ from Java: 5 int maxspeed() {return 60;} 7 8 class bicycle : public vehicle { 9 int panniers; 10 public: 11 bicycle(bool p=true):vehicle(2),panniers(p) {} 12 int maxspeed() {return panniers? 12 : 15;} 13 }; 1 #include <iostream> 2 #include "example13.hh" 3 4 void print_speed(vehicle &v, bicycle &b) { 5 std::cout << v.maxspeed() << " "; 6 std::cout << b.maxspeed() << std::endl; 7 } 8 9 int main() { 10 bicycle b = bicycle(true); 11 print_speed(b,b); //prints "60 12" 12 } 9 / 19 10 / 19 Virtual functions Virtual functions Non-virtual member functions are called depending on the static type of the variable, pointer or reference Since a derived class can be cast to a base class, this prevents a derived class from overloading a function To get polymorphic behaviour, declare the function virtual in the superclass: 5 virtual int maxspeed() {return 60;} In general, for a virtual function, selecting the right function has to be run-time decision; for example: 1 bicycle b; 2 vehicle v; 3 vehicle* pv; 4 5 user_input()? pv = &b : pv = &v; 6 7 std::cout << pv->maxspeed() << std::endl; 8 } 11 / 19 12 / 19

Enabling virtual functions Abstract classes To enable virtual functions, the compiler generates a virtual function table or vtable A vtable contains a pointer to the correct function for each object instance The vtable is an example of indirection The vtable introduces run-time overhead Sometimes a base class is an un-implementable concept In this case we can create an abstract class: 1 class shape { 2 public: 3 virtual void draw() = 0; 4 } It is not possible to instantiate an abstract class: shape s; //Wrong A derived class can provide an implementation for some (or all) the abstract functions A derived class with no abstract functions can be instantiated 13 / 19 14 / 19 Example Multiple inheritance 1 class shape { 2 public: 3 virtual void draw() = 0; 4 }; 5 6 class circle : public shape { 7 public: 8 //... 9 void draw() { /* impl */ } 10 }; It is possible to inherit from multiple base classes; for example: 1 class ShapelyVehicle: public vehicle, public shape { 2... 3 } Members from both base classes exist in the derived class If there is a name clash, explicit naming is required This is done by specifying the class name; for example: ShapelyVehicle sv; sv.vehicle::maxspeed(); 15 / 19 16 / 19

Multiple instances of a base class Virtual base classes With multiple inheritance, we can build: 1 class A {}; 2 class B : public A {}; 3 class C : public A {}; 4 class D : public B, C {}; This means we have two instances of A even though we only have a single instance of D This is legal C++, but means all references to A must be stated explicitly: 1 D d; 2 d.b::a::var=3; 3 d.c::a::var=4; Alternatively, we can have a single instance of the base class Such a virtual base class is shared amongst all those deriving from it 1 class Vehicle {int VIN;}; 2 class Boat : public virtual Vehicle {... }; 3 class Car : public virtual Vehicle {... }; 4 class JamesBondCar : public Boat, public Car {... }; 17 / 19 18 / 19 Exercises 1. If a function f has a static instance of a class as a local variable, when might the class constructor be called? 2. Write a class Matrix which allows a programmer to define two dimensional matrices. Overload the common operators (e.g. +, -, *, and /) 3. Write a class Vector which allows a programmer to define a vector of length two. Modify your Matrix and Vector classes so that they interoperate correctly (e.g. v2 = m*v1 should work as expected) 4. Why should destructors in an abstract class almost always be declared virtual? 19 / 19