EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

EL2310 Scientific Programming

eingebetteter Systeme

CS24 Week 3 Lecture 1

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


CSE 303: Concepts and Tools for Software Development

Programming, numerics and optimization

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

Classes, Constructors, etc., in C++

CS11 Introduction to C++ Fall Lecture 1

AN OVERVIEW OF C++ 1

Interview Questions of C++

Data Structures using OOP C++ Lecture 3

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

Absolute C++ Walter Savitch

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Chapter 10 Introduction to Classes

CSI33 Data Structures

Object Oriented Software Design II

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

A Tour of the C++ Programming Language

Lecture 14: more class, C++ streams

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

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

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

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Object Oriented Programming. Solved MCQs - Part 2

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

Short Notes of CS201

Lecture 18 Tao Wang 1

COMP322 - Introduction to C++

CS201 - Introduction to Programming Glossary By

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

Introduction to Programming session 24

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

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

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

Fast Introduction to Object Oriented Programming and C++

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

Programming in C++: Assignment Week 4

CS 11 C++ track: lecture 1

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

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

CS304 Object Oriented Programming Final Term

Engineering Tools III: OOP in C++

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

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

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

PIC 10A Objects/Classes

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

A Tour of the C++ Programming Language

CS

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


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

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

GEA 2017, Week 4. February 21, 2017

CS11 Intro C++ Spring 2018 Lecture 1

Polymorphism. Zimmer CSCI 330

Object-Oriented Principles and Practice / C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

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

Chapter 1 Getting Started

Inheritance, and Polymorphism.

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

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

CE221 Programming in C++ Part 1 Introduction

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Dr. Md. Humayun Kabir CSE Department, BUET

A A B U n i v e r s i t y

Implementing an ADT with a Class

Object Reference and Memory Allocation. Questions:

C++ 8. Constructors and Destructors

QUIZ. What is wrong with this code that uses default arguments?

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Classes in C++98 and C++11

Lecture 7. Log into Linux New documents posted to course webpage

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CSCE 110 PROGRAMMING FUNDAMENTALS

CS93SI Handout 04 Spring 2006 Apr Review Answers

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

PHY4321 Summary Notes

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Introduction to C++ Systems Programming

Midterm Exam 5 April 20, 2015

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

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

CS250 Final Review Questions

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

CSE 333 Lecture 9 - intro to C++

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

COMSW Introduction to Computer Programming in C

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

W3101: Programming Languages C++ Ramana Isukapalli

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

Polymorphism Part 1 1

Transcription:

(pronobis@kth.se)

Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

Last time Intro to C++ Differences between C and C++ Intro to OOP

Today Object Oriented Programming

Wrap Up Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

Wrap Up C++ Compiler Use g++ instead of gcc Usage and command line options are the same as for gcc Make sure you know how to use make for this part of the course!

Wrap Up Declaration of variables You no longer need to declare the variable at the beginning of the function (scope), as was the case for pre C99 Useful rule of thumb: Declare variables close to where they re used. For instance: i only defined within loop for(int i=0;i<n;i++){...} Use specific names for counters, e.g. i,j,k,...

Wrap Up Namespaces In C all function share a common namespace This means that there can only be one function for each function name In C++ can be placed in namespaces Syntax: namespace NamespaceName { void fcn();... } To access a function fcn in namespace A A::fcn To avid typing namespace name in every statement: using namespace std

Wrap Up Printing to Screen In C++ we use streams for input and output Output is handled with the stream cout and cerr In C: printf("the value is %d\n", value); In C++: cout << "The value is " << value << endl; Just like in C you can format the output in a stream You can use cout.width(10) number of characters for output to fill cout.precision(3) number of digits cout.fill( 0 ) pad with a certain character

Wrap Up Getting input from the user Use streams also to get input from console Use the cin stream Ex: int value; cin >> value; If you want to read an entire line, use getline Ex: string line; getline(cin, line); cout << "The input was " << line << endl;

Wrap Up References Constrained and safer pointers Compare int a; int a; int *pa = &a; int &ra = a; int *pa = NULL; - *pa = 10; ra = 10; => a==10 int b; int b; pa = &b; - int *pc; - pc = pa; -

Wrap Up Passing Arguments by Reference in C++ Declaration: void fcn(int &x); Any changed to x inside fcn will affect the parameter used in the function call Ex: void fcn(int &x) { x = 42; } int main() { int x = 1; fcn(x); cout << "x=" << x << endl; } Will change value of x in the scope of main to 42

Wrap Up Dynamic Memory Allocation in C++ In C++ the new and delete operators are used In C we used malloc and free If you allocate an array with new you need to delete with delete [] Ex: int *p = new int[10]; p[0] = 42; delete [] p; Typical mistake, forgotten []

Introduction to Object Oriented Paradigm Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

Introduction to Object Oriented Paradigm The Object-Oriented Paradigm The motivation: We are trying to solve complex problems Complex code with many functions and names Difficult to keep track of all details How can we deal with the complexity? Grouping related things Abstracting things away Creating hierarchies of things This also improves: Code re-use Reliability and debugging

Introduction to Object Oriented Paradigm Key Concepts of OOP (types) Instances (objects) Methods Interfaces Access protection - information hiding Encapsulation Composition / aggregation Inheritance Polymorphism

Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

A class is an extension of a struct A class can have both data member and function members (methods) bring together data and operations related to that data Like C structs, classes define new data types Unlike structs, they also define how operators work on the new types

Class definition Syntax: class ClassName { public: void fcn(); private: int m X; }; // Do not forget the semicolon!!! m X is a member data void fcn() is a member function public is an access specifier specifying that everything below can be access from outside the class private is an access specifier specifying that everything below is hidden from outside of the class

Access specifiers There are three access specifiers: public private protected No access specifier specified assumes it is private Data and function members that are private cannot be accessed from outside the class Ex: m X above cannot be accessed from outside protected will be discussed later

C++ Structs C++ also uses struct In C++ struct is just like a class (much more than the C struct!) The only difference is the default access protection: class Name { int m X; // Private }; struct Name { int m X; // Public };

and Objects define data types Objects are instances of classes Objects correspond to variables Declaring an object: ClassName variablename;

and Namespace The class defines a namespace Hence function names inside a class do not name clash with other functions Example: the member variable m X above is fully specified as ClassName::m X

Task 1 Implement a class the defines a Car Should have a member variable for number of wheels Should have methods to get the number of wheels Write program that instantiate a Car and print number of wheels

Constructor When an object of a certain class is created the so called constructor is called Constructor is a special kind of method. The constructor tells how to setup the objects The constructor that does not take any arguments is called the default constructor The constructor has the same name as the class and has no return type class A { public: A() {} };

Constructor Some types cannot be assigned, only initialized, e.g. references These data members should be initialized in the initializer list of the constructor Try to do as much of the initialization in the initialization in the list rather than using assignment in the body of the constructor Variables are initialized in the order they appear in the list class A { public: A():m X(1) {} private: int m X; };

Constructor class A { public: A():m X(1) {} int getvalue() { return m X; } private: int m X; }; A a; std::cout << a.getvalue() << std::endl;

Constructor You can define several different constructors class MyClass { public: MyClass():m X(1) {} MyClass(int value):m X(value) {} int getvalue() { return m X; } private: int m X; }; MyClass a; // Default constructor MyClass aa(42); // Constructor with argument std::cout << a.getvalue() << std::endl; std::cout << aa.getvalue() << std::endl;

Destructor When an object is deleted the destructor is called The destructor should clean up things For example free up dynamically allocated memory There is only 1 destructor If not declared a default one is used which will not free up dynamic memory Syntax: ClassName(); Class A { public: A(); // Constructor Ã(); // Destructor... };

Task 2 Write a class Complex for a complex number Provide 3 constructors default - which should create a complex number with value 0 having one argument - should create a real value having two arguments - should create a complex number with real and imaginary part

Source and header file Normally you split the definition from the declaration like in C The definition goes into the header file.h The declaration goes into the source file.cpp Header file ex: class A{ public: A(); private: int m X; }; Source file ex: #include "A.h" A::A():m X(0)

More on and Members Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

More on and Members this pointer Inside class methods you can refer to the object with this pointer The this pointer cannot be assigned (done automatically)

More on and Members const Can have const function arguments Ex: void fcn(const string &s); Pass the string as a reference into the function but commit to not change it For classes this can be used to commit to not change an object as well Ex: void fcn(int arg) const; The function fcn commits to not change anything in the object it belongs to Can only call const functions from a const function or with a const object

More on and Members 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

More on and Members Task 3 Start from the Complex class from last time Add a static int member Every time a new complex number is created the static variable should be incremented Implement the member function Complex& add(const Complex &c); which should add c to the object How does the number of created objects change if we change the function to Complex& add(complex c);

Operator Overloading Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading

Operator Overloading Operator overloading Operators behave just like functions Compare Complex& add(const Complex &c); Complex& +=(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

Operator Overloading Task 4 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

Operator Overloading Next Time C Help Sessions: Wednesday 13-15 Room 304 Thursday 13-14 Room 523 Inheritance, Virtual Functions and Templates C-project deadline Thursday 4th of October