EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

EL2310 Scientific Programming

EL2310 Scientific Programming

Programming, numerics and optimization

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

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

C++ Inheritance and Encapsulation

Object-Oriented Programming

What are the characteristics of Object Oriented programming language?

C The new standard

Inheritance, and Polymorphism.

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Introduction to Programming Using Java (98-388)

And Even More and More C++ Fundamentals of Computer Science

Review: C++ Basic Concepts. Dr. Yingwu Zhu

CS11 Introduction to C++ Fall Lecture 1

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CGS 2405 Advanced Programming with C++ Course Justification

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

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

CSE 303: Concepts and Tools for Software Development

Data Abstraction. Hwansoo Han

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Object Oriented Programming in C#

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

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

Inheritance and Polymorphism


CS304 Object Oriented Programming Final Term

C++ Inheritance and Encapsulation

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

CSI33 Data Structures

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

eingebetteter Systeme

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

Object Oriented Programming. Solved MCQs - Part 2

Object-Oriented Programming

More C++ : Vectors, Classes, Inheritance, Templates

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

Interview Questions of C++

Object Orientated Analysis and Design. Benjamin Kenwright

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

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

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

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!

Financial computing with C++

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

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

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

C++ Alexander Baltatzis

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!

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

Part X. Advanced C ++

C++ Important Questions with Answers

G52CPP C++ Programming Lecture 9

CS304 Object Oriented Programming

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

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

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

G Programming Languages - Fall 2012

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

8. Object-oriented Programming. June 15, 2010

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

CS11 Introduction to C++ Fall Lecture 7

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

COMP6771 Advanced C++ Programming

CHAPTER 5 GENERAL OOP CONCEPTS

CS24 Week 3 Lecture 1

COEN244: Class & function templates

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

Short Notes of CS201

Object-Oriented Principles and Practice / C++

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

JAVA: A Primer. By: Amrita Rajagopal

Object Oriented Programming in C++

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

D Programming Language

Object-Oriented Principles and Practice / C++

CS201 - Introduction to Programming Glossary By

Classes. Christian Schumacher, Info1 D-MAVT 2013

Polymorphism Part 1 1

ENCAPSULATION AND POLYMORPHISM

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

2D1358 Object Oriented Program Construction in C++ Exercises & Labs. Course Registration / Accounts. Course Literature

CS11 Intro C++ Spring 2018 Lecture 1

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Transcription:

Lecture 15: Inheritance and Polymorphism, STL (pronobis@kth.se)

Overview Overview Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

Wrap Up Last time Classes, classes, classes

Wrap Up Today Even more about classes Function / operator overloading Inheritance Virtual functions STL

Wrap Up Repetition Namespaces Call by Reference: & Classes: extension of structs data, functions constructor, destructor C++ Structs Classes and source/header files

Additional Bits about Classes Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

Additional Bits about Classes Class diagrams / UML

Additional Bits about Classes Dynamic allocation of objects One reason to use dynamic memory allocation (new/delete): Moving around pointers to BIG chunks of memory (avoiding unnecessary copying) Makes sense not only for arrays Objects can also be BIG (e.g. database object can be 500MB!) Typically, we dynamically allocate objects We free memory when the object is no longer needed We pass objects by reference (* or &) to functions Example: Database db = new Database("mydatabase.db"); usedb(db); // void usedb(database *db) delete db; db = NULL;

Additional Bits about Classes this pointer Inside class methods you can refer to the object with this pointer The this pointer cannot be assigned (done automatically) Useful when we want to pass this class by reference: Example: class Database { void mymethod() { usedb(this); // void usedb(database *db) } }

Additional Bits about Classes const arguments Function arguments can be const Ex: void fcn(const string &s); String s is passed by reference and cannot be changed!

Additional Bits about Classes const methods Use const to state that a method doesn t change the object class Database { void fcn1(int arg) const; void fcn2(int arg); } fcn1 is not allowed to change anything in the object We can only call const methods from a const method or on a const object Example: const Database d; d.fcn1(1); // Ok, fcn1 is const d.fcn2(1); // Error!, fcn2 is not const

Additional Bits about Classes Static members Members (both functions and data) can be declared static A static member is the same across all objects; it s a member of the class, not any single object That is all instantiated objects share the same static member You can use a static class member without instantiating any object You need to define static data member Ex: (in source file) int A::m Counter = 0; if m Counter is a static data member of class A Static methods can only use static data members!

Overloading Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

Overloading Function overloading We can create functions and methods with the same name, but different arguments It is not possible to overload by changing return type Example: void method(); void method(int a); void method(int b, double c); void method(int b); WRONG! int method(int b); WRONG!

Overloading Operator overloading Operators behave just like functions Compare Complex& add(const Complex &c); Complex& operator+=(const Complex &c); You can overload (provide your own implementation of) most operators This way you can make them behave in a proper way for your class It will not change the behavior for other classes only the one which overloads the operator Some operators are member functions, some are defined outside class

Overloading Task 1 Use the Complex number class from before. Overload/implement: std::ostream& operator<<(std::ostream &os, const Complex &c); Complex operator+(const Complex &c1, const Complex &c2) Complex& operator+=(const Complex &c); function) Complex& operator=(const Complex &c); function) (member (member

Inheritance Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

Inheritance Inheritance Inheritance is a way to show a relation like is a Ex: a car is a vehicle A car inherits many of its properties from being a vehicle These same properties could be inherited by a truck or a bus Syntax: class Car : public Vehicle specifies that Car inherits from Vehicle

Inheritance Inheritance and Constructors If you have three classes A, B and C, where B inherits from A (class B: public A) C inherits from B (class C: public B) When you create C: C c; the constructor from the base classes (B and A) will be run first Execution order 1. Constructor of A 2. Constructor of B 3. Constructor of C

Inheritance Access specifiers private: can be accessed from: inside of the class public: can be accessed from: inside of the class subclasses outside of the class protected: can be accessed from: inside of the class subclasses

Polymorphism and Virtual Functions Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

Polymorphism and Virtual Functions Polymorphism A variable/function can have more than one form Example of polymorphism: operator/function overloading We can have sub-type polymorphism: a variable can be of more than one form A variable of a base type can hold an object of a sub-type In C++ implemented using references or pointers to base classes

Polymorphism and Virtual Functions Polymorphism example class Vehicle {...} class Car: public Vehicle {...} Vehicle *v1 = new Vehicle(); Vehicle *v2 = new Car(); v2 is a Car hidden inside a variable of type pointer to Vehicle! We can then write: v1 = new Car(); So, v1 can hold both a Car and a Vehicle (or even a Truck!) Polymorphism!

Polymorphism and Virtual Functions Subclasses as arguments to function If a function requires as argument a pointer/reference to an object of class A We can provide a pointer/reference to any subclass of A

Polymorphism and Virtual Functions Accessing methods class Vehicle { void drive(); } class Car: public Vehicle { void opentrunk(); } Vehicle *v = new Car(); v->drive(); runs drive() from the Vehicle part of the Car v->opentrunk(); NOT POSSIBLE! But: ((Car *)v)->opentunk(); WORKS!

Polymorphism and Virtual Functions Overloading in sub-classes We can overload a method in a sub-class class Vehicle { void drive(); } class Car: public Vehicle { void drive(); } Vehicle *v1 = new Vehicle(); Vehicle *v2 = new Car(); Car *c = new Car(); v1->drive(); and v2->drive(); run drive() from the Vehicle c->drive(); runs drive() from the Car

Polymorphism and Virtual Functions virtual functions What if we want the object know what it really is and run the correct drive() method? Declare the method with the keyword virtual class Vehicle { virtual void drive(); } class Car: public Vehicle { virtual void drive(); } Vehicle *v1 = new Vehicle(); Vehicle *v2 = new Car(); v1->drive(); runs drive() from the Vehicle v2->drive(); runs drive() from the Car

Polymorphism and Virtual Functions Polymorphism with virtual functions What virtual function to run is determined at run-time Depends on the real type of objects Works for both pointers and references

The Standard Template Library (STL) Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

The Standard Template Library (STL) Template Templates offers a way to write code agnostic to data types You write a template for the code Compiler generates a version for each data type you use it with We can write both template classes and functions Example of a template function: template <class T> T getmax (T a, T b) { if(a>b) {return a} else {return b} } getmax<int>(4,5) returns 5 getmax<double>(4.2, 4.1) returns 4.2

The Standard Template Library (STL) Standard Template Library: STL The Standard Template Library (STL) provides classes for: Collections: lists, vectors, sets, maps Defined as templates: can store data of any type! Examples: std::list<t> Ex: std::list<std::string> names; std::vector<t> Ex: std::vector<double> values; std::set<t> Ex: std::set<std::string> nameofperson; std::map<t1,t2> Ex: std::map<int, std::string> nameofmonth; Ex: std::map<std::string, int> monthnumberbyname;

The Standard Template Library (STL) Standard Template Library: STL Different collections are optimized for different use, e.g.: std::list<t> Cannot access elements with x[i], need to use so called iterators to step through the list, can add/remove elements at low cost std::vector<t> Can access elements with x[i], but resizing is more costly std::set<t> Does not allow for redundant elements std::map<t1,t2> Provides a mapping from one object to another More in C++ Library Reference, e.g. http://www.cplusplus.com/reference/stl/list/

Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism and Virtual Functions The Standard Template Library (STL)

MATLAB - What you should have learned: Be comfortable working with MATLAB Preparing scripts and functions using basic elements of programming (loops, branching,...) Taking advantage of in-built functions (load data, plot data), especially the visualization capabilities. Translating a mathematical problem into MATLAB code. Understand MATLAB code if you see it. Know when (and how) to use MATLAB in another course.

MATLAB - What you should have learned: Be comfortable working with MATLAB Preparing scripts and functions using basic elements of programming (loops, branching,...) Taking advantage of in-built functions (load data, plot data), especially the visualization capabilities. Translating a mathematical problem into MATLAB code. Understand MATLAB code if you see it. Know when (and how) to use MATLAB in another course.

MATLAB - What you should have learned: Be comfortable working with MATLAB Preparing scripts and functions using basic elements of programming (loops, branching,...) Taking advantage of in-built functions (load data, plot data), especially the visualization capabilities. Translating a mathematical problem into MATLAB code. Understand MATLAB code if you see it. Know when (and how) to use MATLAB in another course.

MATLAB - What you should have learned: Be comfortable working with MATLAB Preparing scripts and functions using basic elements of programming (loops, branching,...) Taking advantage of in-built functions (load data, plot data), especially the visualization capabilities. Translating a mathematical problem into MATLAB code. Understand MATLAB code if you see it. Know when (and how) to use MATLAB in another course.

C - What you should have learned: Working with C: how to write, compile, link, execute. Declaring and initializing variables, basic data types, pointers(!), memory allocation(!)... Preparing programs using basic elements of programming (loops, branching,...) Taking advantage of libraries (e.g. for printing data) Understand C code if you see it. Know when (and how) to use C in another course.

C - What you should have learned: Working with C: how to write, compile, link, execute. Declaring and initializing variables, basic data types, pointers(!), memory allocation(!)... Preparing programs using basic elements of programming (loops, branching,...) Taking advantage of libraries (e.g. for printing data) Understand C code if you see it. Know when (and how) to use C in another course.

C - What you should have learned: Working with C: how to write, compile, link, execute. Declaring and initializing variables, basic data types, pointers(!), memory allocation(!)... Preparing programs using basic elements of programming (loops, branching,...) Taking advantage of libraries (e.g. for printing data) Understand C code if you see it. Know when (and how) to use C in another course.

C - What you should have learned: Working with C: how to write, compile, link, execute. Declaring and initializing variables, basic data types, pointers(!), memory allocation(!)... Preparing programs using basic elements of programming (loops, branching,...) Taking advantage of libraries (e.g. for printing data) Understand C code if you see it. Know when (and how) to use C in another course.

C++ - What you should have learned: What of C you can use in C++ and what C++ has to offer more (or in a different way)... especially, the Object Oriented Programming Paradigm(!): Encapsulation, Polymorphism, Inheritance. Declaring classes and instantiating objects, accessing members,... Understanding of conceptual programming, i.e. hiding of functions, declaring of static, const, virtual... Understand C++ code if you see it. Know when (and how) to use C++ in another course.

C++ - What you should have learned: What of C you can use in C++ and what C++ has to offer more (or in a different way)... especially, the Object Oriented Programming Paradigm(!): Encapsulation, Polymorphism, Inheritance. Declaring classes and instantiating objects, accessing members,... Understanding of conceptual programming, i.e. hiding of functions, declaring of static, const, virtual... Understand C++ code if you see it. Know when (and how) to use C++ in another course.

C++ - What you should have learned: What of C you can use in C++ and what C++ has to offer more (or in a different way)... especially, the Object Oriented Programming Paradigm(!): Encapsulation, Polymorphism, Inheritance. Declaring classes and instantiating objects, accessing members,... Understanding of conceptual programming, i.e. hiding of functions, declaring of static, const, virtual... Understand C++ code if you see it. Know when (and how) to use C++ in another course.

C++ - What you should have learned: What of C you can use in C++ and what C++ has to offer more (or in a different way)... especially, the Object Oriented Programming Paradigm(!): Encapsulation, Polymorphism, Inheritance. Declaring classes and instantiating objects, accessing members,... Understanding of conceptual programming, i.e. hiding of functions, declaring of static, const, virtual... Understand C++ code if you see it. Know when (and how) to use C++ in another course.

C++ - What you should have learned: What of C you can use in C++ and what C++ has to offer more (or in a different way)... especially, the Object Oriented Programming Paradigm(!): Encapsulation, Polymorphism, Inheritance. Declaring classes and instantiating objects, accessing members,... Understanding of conceptual programming, i.e. hiding of functions, declaring of static, const, virtual... Understand C++ code if you see it. Know when (and how) to use C++ in another course.

In general: Have an understanding for basic concepts in programming Be skilled enough using MATLAB, so it does not pose a problem in other courses Solve problems and implement algorithms in C and C++ Be able to read and understand existing code written in C or C++ Know the importance of writing code which others can understand, change, correct and build upon

In general: Have an understanding for basic concepts in programming Be skilled enough using MATLAB, so it does not pose a problem in other courses Solve problems and implement algorithms in C and C++ Be able to read and understand existing code written in C or C++ Know the importance of writing code which others can understand, change, correct and build upon

In general: Have an understanding for basic concepts in programming Be skilled enough using MATLAB, so it does not pose a problem in other courses Solve problems and implement algorithms in C and C++ Be able to read and understand existing code written in C or C++ Know the importance of writing code which others can understand, change, correct and build upon

In general: Have an understanding for basic concepts in programming Be skilled enough using MATLAB, so it does not pose a problem in other courses Solve problems and implement algorithms in C and C++ Be able to read and understand existing code written in C or C++ Know the importance of writing code which others can understand, change, correct and build upon

In general: Have an understanding for basic concepts in programming Be skilled enough using MATLAB, so it does not pose a problem in other courses Solve problems and implement algorithms in C and C++ Be able to read and understand existing code written in C or C++ Know the importance of writing code which others can understand, change, correct and build upon

Summary We have tools, but we haven t done much Computer Science yet Algorithms: Sorting, Mapping,... Data structures: Trees, Graphs,... Complexity Discrete Math...

How to continue? The aim of this course was to get you started Hundreds of references and books You should know where to search for info when you need it Some more concentrated programming courses at KTH: DD2385 Programutvecklingsteknik DD2387 Programsystemkonstruktion med C++ DD2390 Internet programming DD2257 Visualization Experience(!) - your own project.

Still to do: Your Evaluation Finish C/C++ projects Final submission deadline is next period The course is only pass or fail Our Evaluation Will be available through BILDA after the C++ project For collecting feedback and opinions about the course

Still to do: Your Evaluation Finish C/C++ projects Final submission deadline is next period The course is only pass or fail Our Evaluation Will be available through BILDA after the C++ project For collecting feedback and opinions about the course

Next Time Invited lecture!