Computational Physics

Similar documents
Computational Physics Operating systems

Computational Physics

Function Overloading

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

Polymorphism Part 1 1

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

C++ (classes) Hwansoo Han

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

Implementing an ADT with a Class

Inheritance, and Polymorphism.

Object-Oriented Programming

Introduction Of Classes ( OOPS )

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Inheritance and Polymorphism

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

C++ 8. Constructors and Destructors

CSE 303: Concepts and Tools for Software Development

CSC1322 Object-Oriented Programming Concepts

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

Object Oriented Design

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Intermediate Programming, Spring 2017*

Scope. Scope is such an important thing that we ll review what we know about scope now:

EL2310 Scientific Programming

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CS250 Final Review Questions

EL2310 Scientific Programming

Introduction to Classes

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

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

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!

GEA 2017, Week 4. February 21, 2017

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

การทดลองท 8_2 Editor Buffer Array Implementation

CSCI 123 Introduction to Programming Concepts in C++

Recharge (int, int, int); //constructor declared void disply();

Comp151. Generic Programming: Container Classes

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

C++ STREAMS; INHERITANCE AS

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

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

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

PIC 10A Objects/Classes

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

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Introduction to Programming Using Java (98-388)

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Extending Classes (contd.) (Chapter 15) Questions:

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

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

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

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

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

Come and join us at WebLyceum

Absolute C++ Walter Savitch

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

Outline 2017/03/17. (sections from

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

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

Constructor - example

Interview Questions of C++

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

CS

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies

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

W3101: Programming Languages C++ Ramana Isukapalli

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!

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

CPSC 427: Object-Oriented Programming

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

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

Object-Oriented Design (OOD) and C++

Polymorphism. Zimmer CSCI 330

Increases Program Structure which results in greater reliability. Polymorphism

Dynamically Allocated Data Members, Overloading Operators

AN OVERVIEW OF C++ 1

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Midterm Review. PIC 10B Spring 2018

Midterm Exam 5 April 20, 2015

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

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

Chapter 9 Classes : A Deeper Look, Part 1

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

C++_ MARKS 40 MIN

Lab 2: ADT Design & Implementation

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

COEN244: Class & function templates

Object Oriented Software Design II

CS250 Final Review Questions

Abstraction in Software Development

G52CPP C++ Programming Lecture 13

Object Oriented Software Design II

Object oriented programming

Intermediate Programming & Design (C++) Classes in C++

Transcription:

Computational Physics numerical methods with C++ (and UNIX) 2018-19 Fernando Barao Instituto Superior Tecnico, Dep. Fisica email: fernando.barao@tecnico.ulisboa.pt Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (1) C++ classes inheritance (cont.) Base class header (IST.h) #ifndef IST #define IST class IST { IST(); // NEEDED default constructor IST(string, float); // constructor ~IST() {; //destructor void SetName(string); string GetName(); virtual void SetBranch(string)=0; protected: string name; float mark; #endif Derived class header (MEFT.h) #ifndef MEFT #define MEFT class MEFT : public IST { MEFT(string, float, string); //constr ~MEFT() {; //destructor void SetBranch(string); string GetBranch(); protected: string branch; //curso #endif Base class code (IST.C) #include "IST.h" IST::IST(string fname, float fmark) : name(fname), mark(fmark) {; //... code Derived class code (MEFT.C) #include "MEFT.h" MEFT::MEFT(string fname, float fmark) : IST(fname, fmark) {; void MEFT::SetBranch(string fbranch) {branch = fbranch; //... code Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (9)

An inheritance scheme for Fis Comp Let s define a base class that should define basic information common to all classes to be developed - cfc class the group name (string) the scholar year (string) the class name (string) virtual functions supposed to be redefined in derived classes The classes that derive from cfc class will inherit all members of base class and will: provide replacements for virtual s funcs add new data members add new functions A derived class can be a base of another derived class Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (10) Class header (cfc.h) cfc class: header file #ifndef cfc #define cfc #include <string> #include <iostream> using namespace std; class cfc { cfc() {groupname="; Year="; ClassName="; cfc(string fg, string fy) : groupname(fg), Year(fy) { string GetGroupName(); string GetYear(); void PrintGroupId(); virtual void Print() = 0; //generic print to be implemented in every derived class void SetClassName(string fc) {ClassName = fc; string GetClassName() {return ClassName; void PrintClassName() {cout << Class Name = << ClassName << endl; private: string groupname; string Year; string ClassName; //+...(nome do trabalho,...) #endif Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (11)

Class implementation (cfc.c) #include <iostream> using namespace std; #include "cfc.h" cfc class: code string cfc::getgroupname() { return groupname; string cfc::getyear() { return Year; void cfc::printgroupid() { cout << "group Name = " << groupname << endl; cout << "Scholar year = " << Year << endl; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (12) point1d class: header file Let s define a class to manipulate one-dimensional points: Class header (point1d.h) #ifndef point1d #define point1d #include "cfc.h" #include "point1d.h" class point1d : public cfc { // 1D points point1d(double fx=0.) : cfc("a01","2014-15"), x(fx) { SetClassName("point1D"); // default constructor (inlined) void move(double); //move to new position void move(point1d); //move to new position void Print(); //print virtual double Norma(); //calculate modulo protected: double x; // x coordinate #endif Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (13)

class: comments cfc abstract class due to pure virtual function Print() reminder: abstract class cannot be instatiated by itself! the virtual function must be defined by the derived classes point1d class has protected members x, which means visible to derived classes members constructor code is implemented inside header file inlined constructor shows that implementation can follow declaration There is default constructor (constructor with no arguments) destructor is not needed because there is no space allocated on heap by the class overloading of member functions move() Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (14) point1d class: code implementation Class code (point1d.c) #include <iostream> using namespace std; #include "point1d.h" void point1d::move(double fx) {x=fx; void point1d::move(point1d p) {x=p.x; void point1d::print() { PrintClassName(); cout << [point1d] x= << x << endl; double point1d::norma() { return x; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (15)

point2d class point2d.h class point2d : public point1d { point2d(double fx, double fy) : point1d(fx), y(fy) {;... private: double y; // y coordinate main program (main.c) YOU HAVE TO TRY IT!!!! #include "point2d.h" int main() { point2d a; // try this...! which constructor is being used? a.dump(); point2d b(0,0); b.dump(); point2d c(5,2); b.move(c); //b=(5,2) b.dump(); double d = Norma(b); Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (16) point2d class (cont.) You are going to have a compiler error due to the fact you are trying to instantiate a point2d using the default constructor (NOT IMPLEMENTED!) Implementation of a default constructor point2d() {x=0; y=0; You can define a much more generic constructor that is a default constructor (no arguments needed) and also accepts arguments point2d(double fx=0, double fy=0) : x(fx), y(fy) {; Example of use of the different constructors point2d a; // (0,0) point2d b(5); // (5,0) point2d b(5,2); // (5,2) Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (17)

class vector2d Let s make a class vector2d making use of the class point2d before defined; it will include two point2d data members dynamically allocated that will require the user to define copy s constructor and assignment a possible class definition with two points (vector2d.h) class vector2d : public cfc { vector2d(point2d pf, point2d pi) : cfc("a01", "2014-15"), Pf(pf), Pi(pi) { cout << "point2d constructor/1" << endl; vector2d(point2d pf) : cfc("a01", "2014-15"), Pf(pf), Pi() { cout << "point2d constructor/2" << endl; private: point2d Pi; //initial point point2d Pf; //final class definition with a point2d pointer (vector2d.h) class vector2d { vector2d(point2d pf, point2d pi); vector2d(point2d pf); private: point2d *P; //pointer class implementation (vector2d.c) vector2d::vector2d(point2d p2, point2d p1){ P = new point2d[2]; P[0] = p1; P[1] = p2; vector2d::vector2d(point2d pf) { P = new point2d[2]; P[0] = point2d(); //default constructor P[1] = pf; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (18) class vector2d (cont.) vector2d class: copy and assignment constructor declarations (vector2d.h) class vector2d { vector2d(const vector2d&); //copy constructor vector2d& operator=(const vector2d&); //copy assignment... vector2d class: copy and assignment constructor implementation (vector2d.c) vector2d::vector2d(const vector2d& t) {//copy constructor P = new point2d[2]; // array with two points created P[0] = t.p[0]; P[1] = t.p[1]; vector2d& vector2d::operator=(const vector2d& t) {//copy assignment if (this!= &t) { //this is a const pointer to current object (member func invoked) P[0] = t.p[0]; P[1] = t.p[1]; return *this; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (19)

class inheritance: virtual destructors When called, the destructor of the derived class calls also the base class destructor Nevertheless, there are situations where that do not happen It is better to have a base class virtual destructor (is a destructor that is also a virtual function) The virtual destructor can ensure a proper cleanup of an object class Base { virtual ~Base() { cout << "Base destructor called" << endl; class Derived: public Base { ~Derived() { cout << "Derived destructor called" << endl; // both destructors called (derived, base) Derived *D1 = new Derived(); delete D1; // only Base destructor is called if // no virtual destructor on base class Base *D2 = new Derived(); delete D2; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (20) C++ classes polymorphism pointers to base class The class inheritance allows the polymorphic characteristic that a pointer to a derived class is type-compatible with a pointer to its base class A class method argument can use generically a pointer to the base class for passing any derived class object only members of the base class are available to base class pointer To recover the original object a cast is needed: derived_class *p = (*derived_class) base_class_pointer; virtual functions A class that declares or inherits a virtual function is called a polymorphic class. A virtual method is a member function that can be redefined in a derived class. If the virtual member is =0 we are in presence of an abstract base class that cannot be instantiated by itself! Example: virtual string GetBranch(); Example: virtual string GetBranch() = 0; //pure virtual function Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (21)

C++ class static members class static member is a member of a class and not of the objects of the class. there will be exactly one copy of the static member per class a function that needs to have access to members of a class but need not to be invoked for a particular object, is called a static member function class vector2d { private: pointer2d *P; static point2d InitPoint; //init point defined for all objects static void SetInitPoint(const point2d& ); Note: private static data members cannot be accessed publicly (only from class members) Initializing static variable (in vector2d.c) //init static variable with (0,0) point2d vector2d::initpoint=point2d(); // implementation of the class code vector2d::vector2d(point2d p2, point2d p1) {... Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (22) C++ class static members (cont.) calling static function from main.c #include "vector2d.h" #include "point2d.h" int main() { //call static function and set static variable to (1,1) vector2d::setinitpoint(point2d(1,1)); Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (23)

C++ class static methods Static methods can be implemented in classes in order to provide functions within a class scope that does not need any private member the class works as a repository of functions that have to be called from the user-function with the scope operator class USERtools { // computes maximum value of array static double MaxValue(double*); #include "USERtools.h" int main() { double p[] = {0.23, 053, 2.3, 5.6, 7. double result = USERtools::MaxValue(p); Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (24) C++ inheritance example Playing with inheritance and polymorphism: C -> B -> A class A #define DEBUG #undef DEBUG #include <iostream> using namespace std; #include <cstdio> #include <cmath> class A { A(float fx=0.0) : x(fx) {cout << PRETTY_FUNCTION << "Constructor A called" << endl; virtual void Print() {cout << PRETTY_FUNCTION << "print A (x=" << x << ")" << endl; virtual ~A() { cout << PRETTY_FUNCTION << "destructor A called" << endl; protected: float x; Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (25)

C++ inheritance example class B class B: public A { //B(float fx=0.0) : x(fx) { // NOT CORRECT //B(float fx=0.0) { x=fx; // CORRECT but not fast B(float fx=0.0) : A(fx) { // CORRECT FAST cout << PRETTY_FUNCTION << "Constructor B called" << endl; ~B() { cout << PRETTY_FUNCTION << "destructor B called" << endl; void Print() { cout << PRETTY_FUNCTION << " print B (x=" << x << ")" << endl; virtual float Norma() {return fabs(x); class B inherits from class A Print() method redefined Norma() method defined as virtual Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (26)