Outline 2017/03/17. (sections from

Size: px
Start display at page:

Download "Outline 2017/03/17. (sections from"

Transcription

1 Outline 2017/03/17 clarifications I/O basic namespaces and structures (recall) Object Oriented programming (8.1) Classes ( ) public, protected and private Constructors and destructors Getters and setters Example public, protected and private Constructors and destructors Getters and setters compilation how to Giving life to classes: Overloading (7.6), Inheritance (11), and Polymorphism (12) (sections from Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

2 Last time examples and exercises Download, look, compile, and run: hello.cpp, typessize.cpp, pointers.cpp, morefunctions.cpp, morefunctions2.cpp, morefunctions3.cpp, morefunctionswrong.cpp, constdeclarations.cpp, strings.cpp write a program which calculates the area of a given trapezoid and print the result on the STDOUT change it in order to accept as input from the command line three numbers - minor basis, major basis and height of a trapezoid: bash>./area Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

3 I/O basics Input/Output operations on files are handled by the fstream class by C++ in a similar way the iostream class handles the STDIN and STDOUT. In physics most of I/O pass through ROOT classes, as we will see in future lessons. Input from text files using C++ is however needed in some lab courses. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

4 io.cpp: I/O basics #include <iostream> #include <fstream> using namespace std; int main(){ ifstream in; // declare an object called in needed to handle files in.open("mydata.dat"); // use the open function to open the file float a1,a2,a3,a4,t1,t2,t3,t4; // called mydata.dat int nlines = 0; bool forever = true; while ( forever ) { // loop forever, or until the file ends in >> a1 >> a2 >> a3 >> a4 >> t1 >> t2 >> t3 >> t4; // read the file if (!in.good() ) break; // alias for if ( in.good() == 0 ) break; if (nlines < 5) cout << a1 << " " << a2 << " " << a3 << << a4 << " " << t1 << " " << t2 << " << t3 << " " << t4 << endl; // NOTICE endl = endline ~same as \n nlines++; cout << " found " << nlines << " points \n"; in.close(); return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

5 Namespaces Namespace: a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces. Namespaces usually group names based on their functionality. Example (not C++): Identifier Namespace identifier Local name /home/user/readme.txt /home/user (path) readme.txt (filename) Example: you have default sin function which accept radians values as input; now you want to create a sin function which accepts degrees as input but you want still to be able to use the radians version. You could create a function sindegrees but a cleaner way is to define a namespace where sin function accepts radians and a different namespace where sin function accepts degrees, being able to choose at will which of the namespace to use. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

6 Namespaces How to use a namespace in your program? by using the scope operator :: NAMESPACE_NAME::localname std::cout<< using cout from namespace std \n ; How to change namespace in a block or globally? using namespace NAMESPACE_NAME; using namespace std; cout << std:: is used by default \n ; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

7 Structures C++ allows us to create our own user-defined aggregate data types. An aggregate data type is a data type that groups multiple individual variables together. One of the simplest aggregate data type is the structure. A structure allows us to group variables of mixed data types together into a single unit. How to define a structure: struct NAME { typea variable1; typeb variable2; ; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

8 Structures How to use a structure in the program: struct Trapezium { float minorbasis; float majorbasis; float height; bool isisosceles; ; int main(){... Trapezium mytrapezium; mytrapezium.minorbasis = 6.; mytrapezium.majorbasis = 8.; mytrapezium.height = 5; mytrapezium.isisosceles = true;... Trapezium yourtrapezium; yourtrapezium.isisosceles = true;... Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

9 Structures How to use a structure in the program (heap memory example): struct Trapezium { float minorbasis; float majorbasis; float height; bool isisosceles; ; int main(){... Trapezium *mytrapezium = new Trapezium(); mytrapezium->minorbasis = 6.; mytrapezium->majorbasis = 8.; mytrapezium->height = 5; mytrapezium->isisosceles = true;... delete mytrapezium;... Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

10 Object-Oriented (OO) programming Non-structured program Procedural program main program main program procedure1 procedure3 procedure2 Modular program main program data module 2 Object Oriented program object 1 object 3 module 1 procedure1 procedure2 procedure3 object 2 object 4 Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

11 Object-Oriented concepts What is object-oriented programming? Take a look around you, everywhere you look are objects. Most objects have two major components to them: 1) a list of properties (eg. weight, color, size, texture, shape, etc ). 2) Some number of actions that either they can perform, or that can be performed on them (e.g. being opened, having something poured into it, etc ). These two components are inseparable. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

12 Object-Oriented concepts With traditional programming, the properties (data) and actions (functions) are separate entities not a very intuitive representation of reality. We are intuitively used to thinking about things as objects, and expect to be able to perform actions with/on those objects. Object-oriented programming provides us with the ability to design objects that have both characteristics (sometimes called attributes, fields, or properties) and behaviors (methods or features), all tied together in one package. This allows programs to be written in a more modular fashion, which makes them easier to write and understand, and also provides a higher degree of codereusability. Objects provide a more intuitive way to work with our data by allowing us to define how we interact with the objects, and how they interact with other objects. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

13 Object-Oriented concepts Class: part of the system, it describes something Object: class declaration in the program Class member: something owned by the class (property) Method: function of the class, it describes what the class can do (action) When we use a class in a program we write a declaration to use it: this is called instantiating the class. The variable used in the declaration is called an instance of the class or an object. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

14 Defining a class, syntax A class can contain members and functions of any type, class is like an extended structure: class polygon { typea variable1; typeb function(typec); protected: typea variable2; ; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

15 Defining a class Members and methods of a class are available everywhere in the class implementation, class acts like a namespace: class polygon { typea variable1; typeb function(typec); ; typeb polygon::function(typec myvar){ typea localvar = sin(variable1);... Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

16 Headers and implementations When we have a class we usually want it to be used by more than a single program, we want to make a library out of it. We will need to split our code. Usually the header file: classname.h will contain the class declaration, the members and methods definition. The implementation file: classname.cpp will contain the implementation of the methods of the class, how the object is contructed and destructed, what the methods are doing, etc. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

17 Headers and implementations Eventually a shared library will be created (.so file). In the user program to be able to use our class we will need to: 1) add the header file; 2) link the shared library when compiling. class polygon { typea variable1; typeb function(typec); ; typeb polygon::function(typec myvar){ typea localvar = sin(variable1);... Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

18 Headers and implementations Eventually a shared library will be created (.so file). In the user program to be able to use our class we will need to: 1) add the header file; 2) link the shared library when compiling. class polygon { typea variable1; FILE polygon.h typeb function(typec); ; typeb polygon::function(typec myvar){ typea localvar = sin(variable1);... FILE polygon.cpp Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

19 Class definition, the three levels of data hiding public, protected and are access modifiers and help us implement information hiding. They tell the compiler which other classes should have access to the fields or methods being defined: public - Any class can refer to the members or call the methods. protected - Only the current class and subclasses (class daughters) of this class will have access to the members or methods. private - Only the current class will have access to the members or methods. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

20 Class definition, three levels of data hiding: example public protected private the garden, table, seats outside this building anybody can use them rooms inside this building only friends can use it our colleagues, people that has to do with Physics Departement this computing room only us are authorized to use it in this moment Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

21 Class definition, three levels of data hiding: protected seldom used public protected private the garden, table, seats outside this building anybody can use them rooms inside this building only friends can use it our colleagues, people that has to do with Physics Departement this computing room only us are authorized to use it in this moment Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

22 Class definition, three levels of data hiding: example II public protected variables and methods that can be accessed directly in our program (myprogram.cpp) variables and methods than can be accessed by sub-classes private variables and methods that can be accessed only inside the declaration and implementation of the class (polygon.h and polygon.cpp) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

23 Constructors and Destructors constructors and destructors are special methods of a class: they are automatically called when the object come to life (constructor) and die (destructor) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

24 Constructor usage The constructor is used to: allocate memory needed by the class initialize data fields (when and if needed) The constructor returns an object of the class type. The constructor name must be equal to its class name. The constructor can accept input arguments. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

25 Destructor usage The destructor is used to: de-allocate memory used by the class must call a delete for any new defined in the constructor or by class methods The destructor returns void. The destructor name must be equal to its class name prefixed by a tilde symbol: ~. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

26 Getters and Setters: definition Getters and Setters are public methods of a class used to access private members instead of using public members Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

27 Getters: how to In the declaration: int a; int GetA(); int _a; in the implementation: int MyClass::GetA(){ return _a; in the main program: int m=myclass->a; int m=myclass->geta(); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

28 Setters: how to In the declaration: int a; void SetA(int value); int _a; in the implementation: void MyClass::SetA(int value){ _a = value; in the main program: MyClass->a = 10; MyClass->SetA(10); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

29 Getters and Setters: why? Getters and Setters: hide class members to the rest of the world (less prone to introduce bugs); allow internal management to be changed without needs in changing the application programs; you can perform validation; can allow different access levels to the same variable, for example GetA() can be public while SetA() can be private. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

30 How to arrange files to write a class When we have a class we usually want it to be used by more than a single program, we want to make a library out of it. We will need to split our code. Usually the header file: classname.h will contain the class declaration, the members and methods definition. The implementation file: classname.cpp will contain the implementation of the methods of the class, how the object is contructed and destructed, what the methods are doing, etc. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

31 What to do to use a class Eventually a shared library will be created (.so file). In the user program to be able to use our class we will need to: 1) add the header file in the source code; 2) instantiate and use the object in the source code; 3) link the shared library when compiling; 4) tell the executable where to find the library at run time. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

32 At least working on 3 source files class Polygon { typea variable1; FILE polygon.h typeb function(typec); ; typeb Polygon::function(typeC myvar){ typea localvar = sin(variable1);... FILE polygon.cpp #include <polygon.h> int main(){ typec perimeter; Polygon *trapezium = new Polygon(); trapezium->function(perimeter); return 0; FILE myprogram.cpp class header file class implementation file program file Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

33 At least working on 3 source files class Polygon { typea variable1; FILE polygon.h typeb function(typec); ; typeb Polygon::function(typeC myvar){ typea localvar = sin(variable1);... FILE polygon.cpp #include <polygon.h> int main(){ typec perimeter; Polygon *trapezium = new Polygon(); trapezium->function(perimeter); return 0; FILE myprogram.cpp what the object is made of and what it can do how the object does things object final usage Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

34 Building a class, example: header,.h file implementation,.cpp file user program,.cpp file Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

35 Building a class, trapezium example: trapezium.h file trapezium.cpp file classtest.cpp file #ifndef TRAPEZIUM_H #define TRAPEZIUM_H #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

36 Building a class, trapezium example: trapezium.h file trapezium.cpp file classtest.cpp file #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

37 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file trapezium.cpp file classtest.cpp file #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); ; delete t; return 0; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

38 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; Trapezium(); trapezium.h file trapezium.cpp file classtest.cpp file #include <trapezium.h> Trapezium::Trapezium(){ #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); delete t; return 0; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

39 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ Trapezium(); trapezium.h file trapezium.cpp file classtest.cpp file float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); delete t; return 0; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

40 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; Trapezium(); trapezium.h file trapezium.cpp file classtest.cpp file float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->_majoraxis << delete t; return 0; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

41 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; Trapezium(); trapezium.h file trapezium.cpp file classtest.cpp file float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->_majoraxis << delete t; return 0; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

42 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << delete t; return 0; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

43 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; float Trapezium::GetMinorAxis(){ return _minoraxis; float Trapezium::GetHeight(){ return _height; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

44 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; float Trapezium::GetMinorAxis(){ return _minoraxis; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->_majoraxis = 10.; ; #endif // TRAPEZIUM_H float Trapezium::GetHeight(){ return _height; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

45 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); float _majoraxis; float _minoraxis; float _height; #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; float Trapezium::GetMinorAxis(){ return _minoraxis; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->_majoraxis = 10.; ; #endif // TRAPEZIUM_H float Trapezium::GetHeight(){ return _height; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

46 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; float Trapezium::GetHeight(){ return _height; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

47 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); void SetMinorAxis(float cm); void SetHeight(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; void Trapezium::SetMinorAxis(float cm){ _minoraxis = cm; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << float Trapezium::GetHeight(){ return _height; delete t; return 0; void Trapezium::SetHeight(float cm){ _height = cm; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

48 Classes as namespaces: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file Trapezium(); float GetMajorAxis(); float _majoraxis; float _minoraxis; float _height; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

49 Classes as namespaces: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file Trapezium(); float GetMajorAxis(); int var1; float _majoraxis; float _minoraxis; float _height; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

50 Classes as namespaces: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file Trapezium(); float GetMajorAxis(); int var1; float _majoraxis; float _minoraxis; float _height; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ if ( var1 < 5 )... return _majoraxis; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

51 Classes as namespaces: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file Trapezium(); float GetMajorAxis(); int var1; float _majoraxis; float _minoraxis; float _height; int _var2; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ if ( var1 < 5 )... if ( _var2 > 15 )... return _majoraxis; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

52 Classes as namespaces: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file Trapezium(); float GetMajorAxis(); int var1; void method1(); float _majoraxis; float _minoraxis; float _height; int _var2; void _method2(); #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ if ( var1 < 5 )... if ( _var2 > 15 )... method1(); trapezium.cpp file... _method2(); return _majoraxis; void Trapezium::method1(){ void Trapezium::_method2(){ Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

53 The «this» operator #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file Trapezium(); float GetMajorAxis(); int var1; void method1(); float _majoraxis; float _minoraxis; float _height; int _var2; void _method2(); #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ if ( this->var1 < 5 )... if ( this->_var2 > 15 )... this->method1(); trapezium.cpp file... this->_method2(); return _majoraxis; void Trapezium::method1(){ Operator this : the this pointer is an implicit parameter to all member variables and methods. Therefore, inside a member method, this may be used to refer to the invoking object. void Trapezium::_method2(){ Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

54 Classes: inline methods #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file Trapezium(); float GetMajorAxis(); float _majoraxis; float _minoraxis; float _height; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

55 Classes: inline methods #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ Trapezium(); trapezium.h file float GetMajorAxis(){return _majoraxis;; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float _majoraxis; float _minoraxis; float _height; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

56 Classes: inline methods #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ trapezium.h file Trapezium(); inline float GetMajorAxis(){return _majoraxis;; trapezium.cpp file #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float _majoraxis; float _minoraxis; float _height; ; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

57 Code compilation: how to 1. create the shared library containing the class: I. create the class object g++ -Wall -fpic I./ -c trapezium.cpp -o trapezium.o issue all compilation warnings search for headers files in the directory./ (this directory) position independent code (necessary for so libs) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

58 Code compilation: how to 1. create the shared library containing the class: I. create the class object g++ -Wall -fpic I./ -c trapezium.cpp -o trapezium.o II. create the shared object g++ -Wall -shared -o libtrapezium.so trapezium.o create a shared library name MUST start with lib Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

59 Code compilation: how to 2. create the main program executable: III. create the program object g++ -Wall -c classtest.cpp -I./ -o classtest.o search for headers files in the directory./ (this directory) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

60 Code compilation: how to 2. create the main program executable: III. create the program object g++ -Wall -c classtest.cpp -I./ -o classtest.o IV. create the executable g++ -Wall -o classtest classtest.o -L./ -ltrapezium search for libraries files in the directory./ (this directory) NB: for compilation only! this program will need a shared library called libtrapezium.so Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

61 Code compilation: summary >ls trapezium.cpp trapezium.h classtest.cpp >g++ -Wall -fpic -c trapezium.cpp -I./ -o trapezium.o >g++ -Wall -shared -o libtrapezium.so trapezium.o >g++ -Wall -c classtest.cpp -I./ -o classtest.o >g++ -Wall -o classtest classtest.o -L./ -ltrapezium >ls trapezium.cpp trapezium.h classtest.cpp trapezium.o libtrapezium.so classtest.o classtest Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

62 Running the executable: how to >ls trapezium.cpp trapezium.h classtest.cpp trapezium.o libtrapezium.so classtest.o classtest >./classtest./classtest: error while loading shared libraries: libtrapezium.so: cannot open shared object file: No such file or directory >export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH >./classtest A = 0 a = 0 h = 0 A = 10 a = 4 h = 2 > Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

63 Overloading Overloading: two (or more) methods (or functions or operators like +, -, ==,...]) with the same name but accepting different parameter(s) are seen as different methods (functions or operators) by the compiler void Print(); void Print(float a); void Print(int b); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

64 Overloading void Print(){ cout << hello! \n ; void Print(float a){ cout << Float: << a << \n ; void Print(int b){ cout << Int: << b << \n ; int main (){ Print(); Print(5.); Print(5); return 0; hello! Float: 5 Int: 5 Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

65 Building a class, trapezium example: #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; trapezium.h file trapezium.cpp file classtest.cpp file Trapezium(); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); void SetMinorAxis(float cm); void SetHeight(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <trapezium.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; void Trapezium::SetMinorAxis(float cm){ _minoraxis = cm; #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << float Trapezium::GetHeight(){ return _height; delete t; return 0; void Trapezium::SetHeight(float cm){ _height = cm; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

66 Trapezium example: constructor overloading #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; TrapeziumO.h file TrapeziumO.cpp file classtesto.cpp file Trapezium(); Trapezium(float minor, float major, float height); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); void SetMinorAxis(float cm); void SetHeight(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <TrapeziumO.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; Trapezium::Trapezium(float minor, float major, float height){ _majoraxis = major; _minoraxis = minor; _height = height; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; void Trapezium::SetMinorAxis(float cm){ _minoraxis = cm; float Trapezium::GetHeight(){ return _height; void Trapezium::SetHeight(float cm){ _height = cm; #include <iostream> #include <TrapeziumO.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << Trapezium *s= new Trapezium(1.,1.,2.); cout << A = << s->getmajoraxis() << cout << a = << s->getminoraxis() << cout << h = << s->getheight() << \n delete s; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

67 Trapezium example: constructor overloading #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; TrapeziumO.h file TrapeziumO.cpp file classtesto.cpp file Trapezium(); Trapezium(float minor, float major, float height); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); void SetMinorAxis(float cm); void SetHeight(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <TrapeziumO.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; Trapezium::Trapezium(float minor, float major, float height){ SetMajorAxis(major); _minoraxis = minor; _height = height; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; void Trapezium::SetMinorAxis(float cm){ _minoraxis = cm; float Trapezium::GetHeight(){ return _height; void Trapezium::SetHeight(float cm){ _height = cm; #include <iostream> #include <TrapeziumO.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << Trapezium *s= new Trapezium(1.,1.,2.); cout << A = << s->getmajoraxis() << cout << a = << s->getminoraxis() << cout << h = << s->getheight() << \n delete s; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

68 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Trapezium Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

69 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

70 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Polygon Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

71 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Polygon circle??? Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

72 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Polygon Elipses Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

73 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. Geometrical figures Polygon Elipses Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

74 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. area, perimeter,... Geometrical figures number of sides, lenght of radii,... Polygon Elipses Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

75 Inheritance Inheritance: a class can be defined in terms of an older, pre-existing class. This produces a new class (derived or daughter class) having all the functionality of the older class (base or mom class), and additionally defining its own specific functionality. class derivedclass : public BaseClass{... Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

76 Inheritance DerivedClass will have full access to all members and method in the public zone of BaseClass it is like copying the BaseClass public code in the DerivedClass public declaration plus the members and methods defined in the DerivedClass class DerivedClass : public BaseClass{... float derivedonlyvariable;// new float Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

77 Multiple inheritance multiple inheritance: a class can have more than one parent! in that case it brings in her public zone all public members and methods of all the parent classes. It is declared this way: class daughter : public Mom, public Dad { class kid: public Mom, public Dad, public Uncle, { area, perimeter,... Colors Geometrical figures number of sides, lenght of radii,... Polygon Elipses Trapezium Triangle Pentagon Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

78 #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; Trapezium example: inheritance TrapeziumO.h file TrapeziumO.cpp file classtesto.cpp file Trapezium(); Trapezium(float minor, float major, float height); float GetMajorAxis(); float GetMinorAxis(); float GetHeight(); void SetMajorAxis(float cm); void SetMinorAxis(float cm); void SetHeight(float cm); float _majoraxis; float _minoraxis; float _height; #endif // TRAPEZIUM_H #include <TrapeziumO.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; Trapezium::Trapezium(float minor, float major, float height){ _majoraxis = major; _minoraxis = minor; _height = height; float Trapezium::GetMajorAxis(){ return _majoraxis; void Trapezium::SetMajorAxis(float cm){ _majoraxis = cm; float Trapezium::GetMinorAxis(){ return _minoraxis; void Trapezium::SetMinorAxis(float cm){ _minoraxis = cm; float Trapezium::GetHeight(){ return _height; void Trapezium::SetHeight(float cm){ _height = cm; #include <iostream> #include <TrapeziumO.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

79 #ifndef TRAPEZIUM_H #define TRAPEZIUM_H class Trapezium{ ; Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); float _majoraxis; Trapezium example: inheritance TrapeziumO.h file TrapeziumO.cpp file classtesti.cpp file #include <TrapeziumO.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <TrapeziumO.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; #endif // TRAPEZIUM_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

80 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; class Trapezium{ Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file #include <Polygon.h> Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

81 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; class Trapezium{ Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

82 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

83 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

84 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << delete t; return 0; number of sides = 4 float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

85 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->_nsides << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

86 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->_nsides << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

87 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); int GetTrapeziumNsides(); float _majoraxis; ; #endif // POLYGON_H #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); int Trapezium::GetTrapeziumNsides(){ return _nsides; #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

88 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); int GetTrapeziumNsides(); float _majoraxis; ; #endif // POLYGON_H #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); int Trapezium::GetTrapeziumNsides(){ return _nsides; #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

89 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); int GetTrapeziumNsides(); float _majoraxis; ; #endif // POLYGON_H #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); int Trapezium::GetTrapeziumNsides(){ return GetNsides(); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

90 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << delete t; return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

91 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = new Polygon(); cout << number of p-sides = << p->getnsides() << float _majoraxis; ; #endif // POLYGON_H delete p; delete t; return 0; number of sides = 0 Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

92 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = new Polygon(); cout << number of p-sides = << p->getnsides() << float _majoraxis; cout << number of p-sides = << p->_nsides() << ; #endif // POLYGON_H delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

93 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = new Polygon(); cout << number of p-sides = << p->getnsides() << float _majoraxis; cout << number of p-sides = << p->_nsides() << ; #endif // POLYGON_H delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

94 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = new Polygon(); cout << number of p-sides = << p->getnsides() << float _majoraxis; cout << p-axis = << p->getmajoraxis() << ; #endif // POLYGON_H delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

95 #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Trapezium example: inheritance Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = new Polygon(); cout << number of p-sides = << p->getnsides() << float _majoraxis; cout << p-axis = << p->getmajoraxis() << ; #endif // POLYGON_H delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

96 Trapezium example: inheritance, pointing to derived objects #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = t; float _majoraxis; ; #endif // POLYGON_H delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

97 Trapezium example: inheritance, pointing to derived objects #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = t; cout << number of sides = << p->getnsides() << float _majoraxis; ; #endif // POLYGON_H delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

98 Trapezium example: inheritance, pointing to derived objects #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = t; cout << number of sides = << p->getnsides() << float _majoraxis; ; #endif // POLYGON_H delete t; return 0; number of sides = 4 Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

99 Trapezium example: inheritance, pointing to derived objects #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = t; cout << number of sides = << p->getnsides() << float _majoraxis; ; #endif // POLYGON_H cout << p-axis = << p->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

100 Trapezium example: inheritance, pointing to derived objects #ifndef POLYGON_H #define POLYGON_H class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; Polygon.h file Polygon.cpp file classtesti.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <Polygon.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <iostream> #include <Polygon.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << t->setmajoraxis(10.); cout << A = << t->getmajoraxis() << cout << number of sides = << t->getnsides() << Polygon *p = t; cout << number of sides = << p->getnsides() << float _majoraxis; ; #endif // POLYGON_H cout << p-axis = << p->getmajoraxis() << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

101 Inheritance, pointing to derived objects: Trapezium *t = new Trapezium(); Rectangle *r = new Rectangle(); Triangulum *i = new Triangulum(); Polygon *p[3] = {t,r,i; for (int i=0; i<3; i++){ cout << nsides << p[i]->getnsides(); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

102 Access to members in public inheritance class A{ int apub; protected: int aprot; int apriv; not members: A: apub Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

103 Access to members in public inheritance class A{ int apub; protected: int aprot; int apriv; class B : public A { int bpub; protected: int bprot; int bpriv; not members: A: apub B: apub B: bpub Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

104 Access to members in public inheritance class A{ int apub; protected: int aprot; int apriv; class B : public A { int bpub; protected: int bprot; int bpriv; not members: A: apub B: apub B: bpub B: A: apub A: aprot Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

105 Access to members in protected inheritance class A{ int apub; protected: int aprot; int apriv; class B : protected A { int bpub; protected: int bprot; int bpriv; not members: A: apub B: apub B: bpub B: A: apub A: aprot Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

106 Access to members in private inheritance class A{ int apub; protected: int aprot; int apriv; class B : private A { int bpub; protected: int bprot; int bpriv; not members: A: apub B: apub B: bpub B: A: apub A: aprot Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

107 Polymorphism StarTrek: Captain Kirk was in trouble, as usual. He met an extremely beautiful lady who, however, later on changed into a hideous troll. Kirk was quite surprised, but the lady told him: Didn't you know I am a polymorph? Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

108 Polymorphism, towards complex objects with multiple properties Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

109 Polymorphism, same action but different implementation, CUT Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

110 Polymorphism, definition In a programming language that exhibits polymorphism, objects of classes belonging to the same hierarchical tree (i.e. inherited from a common base class) may possess functions bearing the same name, but each having different behaviors. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

111 Polymorphism, definition As an example, let us assume there is a base class named Animals from which the subclasses Horse, Fish and Bird are derived. Let us also assume that the Animals class has a function named Move, which is inherited by all subclasses mentioned. With polymorphism, each subclass may have its own way of implementing the function. So, for example, when the Move function is called in an object of the Horse class, the function might respond by displaying trotting on the screen. On the other hand, when the same function is called in an object of the Fish class, swimming might be displayed on the screen. In the case of a Bird object, it may be flying. In effect, polymorphism trims down the work of the developer because he can now create a sort of general class with all the attributes and behaviors that he envisions for it. When the time comes for the developer to create more specific subclasses with certain unique attributes and behaviors, the developer can simply alter code in the specific portions where the behaviors will differ. All other portions of the code can be left as is. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

112 Polymorphism, why? 1) To be more generic: it is the ability to present the same interface for differing underlying forms, the ability to use the same base object to perform different daughter actions. 2) To implement a protocol: to impose requirements (when the action is declared but not implemented in the base class). Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

113 Polymorphism, same object different actions: Trapezium *t = new Trapezium(); Rectangle *r = new Rectangle(); Triangulum *i = new Triangulum(); Polygon *p[3] = {t,r,i; for (int i=0; i<3; i++){ cout << nsides << p[i]->getnsides(); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

114 Polymorphism, same object different actions: Trapezium *t = new Trapezium(); Rectangle *r = new Rectangle(); Triangulum *i = new Triangulum(); Polygon *p[3] = {t,r,i; for (int i=0; i<3; i++){ cout << nsides << p[i]->getnsides(); cout << nsides << p[i]->calculatearea(); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

115 Polymorphism, late binding Polymorphism is implemented using a feature called late binding. It's called that way because the decision which function to call (a base class function or a function of a derived class) cannot be made at compile-time, but is postponed until the program is actually executed: only then it is determined which member function will actually be called. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

116 Polymorphism, early and late binding Binding: is the decision made by the compiler to assign a specific function in the main code to a specific function declaration. void Print(){ cout << hello! \n ; void Print(float a){ cout << Float: << a << \n ; void Print(int b){ cout << Int: << b << \n ; int main (){ Print(); Print(5.); Print(5); return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

117 Polymorphism, early and late binding Early binding: assignment is done at compilation time. void Print(){ cout << hello! \n ; void Print(float a){ cout << Float: << a << \n ; void Print(int b){ cout << Int: << b << \n ; int main (){ Print(); Print(5.); Print(5); return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

118 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ void process(); void _hello(); void Base::_hello(){ cout << "base hello\n"; base->process(); delete base; return 0; ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

119 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ void process(); void _hello(); void Base::_hello(){ cout << "base hello\n"; base->process(); delete base; return 0; base hello ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

120 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); void _hello(); class Derived : public Base{ void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); delete derived; delete base; return 0; void _hello(); ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

121 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); void _hello(); class Derived : public Base{ void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); delete derived; delete base; return 0; base hello void _hello(); ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

122 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); void _hello(); class Derived : public Base{ ; void _hello(); #endif // MYCLASS_H void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); delete base; return 0; Derived's author's reasoning was as follows: Base's implementation of _hello is not appropriate; a Derived class object can remedy that by providing an appropriate implementation. Furthermore our author reasoned: since the type of an object determines the interface that is used, process must call Derived::_hello as _hello is called via process from a Derived class object. Unfortunately our author's reasoning is flawed, due to early binding. When Base::process was compiled static binding caused the compiler to bind the _hello call to Base::_hello. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

123 Polymorphism, early and late binding Late binding: assignment is done at run time. NB: C++ default is early binding! Late binding is necessary to implement the polymorphism. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

124 Polymorphism, early and late binding Late binding: assignment is done at run time. NB: C++ default is early binding! Late binding is necessary to implement the polymorphism. «virtual» specifier! Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

125 Example: early binding #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); void _hello(); class Derived : public Base{ void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); delete derived; delete base; return 0; base hello void _hello(); ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

126 Example: late binding, virtual specifier #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); virtual void _hello(); class Derived : public Base{ void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); delete derived; delete base; return 0; derived hello void _hello(); ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

127 Example: late binding, virtual specifier #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ void process(); virtual void _hello(); void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); Base *reftoderived = derived; ; class Derived : public Base{ void _hello(); reftoderived->process(); delete derived; delete base; return 0; derived hello ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

128 Example: late binding, virtual specifier #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ void process(); virtual void _hello(); void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); base hello Base *reftoderived = derived; ; class Derived : public Base{ void _hello(); reftoderived->process(); delete derived; delete base; return 0; derived hello ; #endif // MYCLASS_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

129 Example: late binding, virtual specifier #ifndef MYCLASS_H #define MYCLASS_H #include <iostream> using namespace std; #include <myclass.h> void Base::process(){ _hello(); #include <myclass.h> int main(){ Base *base = new Base(); class Base{ ; void process(); virtual void _hello(); class Derived : public Base{ void Base::_hello(){ cout << "base hello\n"; void Derived::_hello(){ cout << derived hello\n"; void AnotherDerived::_hello(){ cout << another derived hello\n"; base->process(); Derived *derived = new Derived(); derived->process(); AnotherDerived *anotherderived = new AnotherDerived(); Base *generic; base hello ; void _hello(); class AnotherDerived : public Base{ ; void _hello(); generic = base; generic->process(); generic = derived; generic->process(); generic = anotherderived; generic->process(); delete anotherderived; delete derived; delete base; derived hello another derived hello #endif // MYCLASS_H return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

130 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); int _nsides; ; PolygonP.h file PolygonP.cpp file classtestp.cpp file class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <PolygonP.h> int main(){ return 0; float _majoraxis; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

131 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; PolygonP.h file PolygonP.cpp file classtestp.cpp file class Trapezium : public Polygon{ #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; #include <PolygonP.h> int main(){ return 0; Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); float _majoraxis; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

132 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); float _majoraxis; PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); delete p; delete t; return 0; ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

133 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); float _majoraxis; PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); delete p; delete t; return 0; polygon! polygon! polygon! polygon! ; #endif // POLYGON_H Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

134 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); void PrintName(); float _majoraxis; ; #endif // POLYGON_H PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); void Trapezium::PrintName(){ std::cout<< trapezium!\n ; #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

135 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); void PrintName(); float _majoraxis; ; #endif // POLYGON_H PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); void Trapezium::PrintName(){ std::cout<< trapezium!\n ; #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); delete p; delete t; return 0; trapezium! polygon! trapezium! polygon! Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

136 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName(); int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); void PrintName(); float _majoraxis; ; #endif // POLYGON_H PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); void Trapezium::PrintName(){ std::cout<< trapezium!\n ; #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); t->polygon::printname(); delete p; delete t; return 0; trapezium! polygon! trapezium! polygon! polygon! Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

137 Polymorphism, abstract base classes The base class Polygon is provided with its own concrete implementations of its virtual member PrintName(). However, virtual member functions do not necessarily have to be implemented in base classes. When the implementations of virtual members are omitted from base classes the class imposes requirements upon derived classes. The derived classes are required to (MUST!) provide the `missing implementations' This approach, in some languages (like C#, Delphi and Java) known as an interface, defines a protocol. Derived classes must obey the protocol by implementing the as yet not implemented members. If a class contains at least one member whose implementation is missing no objects of that class can be declared in the program. Such incompletely defined classes are always base classes. They enforce a protocol by merely declaring names, return values and arguments of some of their members. These classes are call abstract classes or abstract base classes. Derived classes become non-abtract classes by implementing the as yet not implemented members. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

138 Pure virtual methods pure virtual methods declaration: if method void Print() is not implemented in class Base then it must be declared this way: virtual void Print() = 0; classes containing pure virtual methods cannot be instantiated in the program: Base *b = new Base(); Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

139 Trapezium example: virtual void PrintName() #ifndef POLYGON_H #define POLYGON_H #include <iostream> class Polygon{ Polygon(); void SetNsides(int nos); int GetNsides(); virtual void PrintName() = 0; int _nsides; ; class Trapezium : public Polygon{ Trapezium(); float GetMajorAxis(); void SetMajorAxis(float cm); void PrintName(); float _majoraxis; ; #endif // POLYGON_H PolygonP.h file PolygonP.cpp file classtestp.cpp file #include <PolygonP.h> Polygon::Polygon(){ _nsides = 0; int Polygon::GetNsides(){ return _nsides; void Polygon::SetNsides(int nos){ _nsides = nos; void Polygon::PrintName(){ std::cout<< polygon!\n ; Trapezium::Trapezium(){ _majoraxis = 0.; _minoraxis = 0.; _height = 0.; SetNsides(4); void Trapezium::PrintName(){ std::cout<< trapezium!\n ; #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(); t->printname(); Polygon *p = new Polygon(); p->printname(); Polygon *any; any = t; any->printname(); any = p; any->printname(); delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

140 2017/03/17 take home messages use fstream for I/O from files namespaces: like directories and filenames structures: aggregate of different data types objects have properties (weight, colour, ) and can perform or used to perform actions (pulling, hitting, ) a class represent an object class members are the properties class methods are the actions header: properties and definition of actions implementaton: how actions are taken how to compile (slides ) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

141 2017/03/17 take home messages overloading: two functions/methods can be implemented differentely if different input variables are used (public) inheritance: a daughter class brings in her public zone all public members and methods of the mother class polymorphism: different objects have the possibility to act in a different way to the same command. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

142 Exercises Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

143 Exercise, now and next Friday 1. Download from moodle the three files trapezium.h, trapezium.cpp, classtest.cpp 2. Try to compile and run the program (slides of this presentation). Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

144 Exercise, now and next Friday 3. Add a public method to the class called calculatearea returning a float with the calculated trapezium area using the major/minor bases and height stored as class members: modify trapezium.h and trapezium.cpp in order to make working the user program on the right of this slide. #include <iostream> #include <trapezium.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << t->setmajoraxis(10.); t->setminoraxis(4.); t->setheight(2.); float area = t->calculatearea(); cout << A = << t->getmajoraxis() << cout << a = << t->getminoraxis() << cout << h = << t->getheight() << cout << Area is << area << delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

145 Exercises 1. Download from moodle the following files: Polygon.cpp classtesti.cpp Polygon.h TrapeziumO.h classtesto.cpp TrapeziumO.cpp PolygonP.cpp PolygonP.h classtestp.cpp 2. Compile and run the executables: classtesto (overloading) classtesti (inheritance) classtestp (polymorphism) Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

146 Exercises 3. Add: overloading: a SetMajorAxis(float cm, bool checkrange) inheritance: a private variable _area inside polygon class with proper getter and setter polymorphism: virtualize calculatearea in polygon class, implement it in the derived class trapezium Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

147 Exercise 1 OVERLOADING: to TrapeziumO.h /.cpp add a method called: SetMajorAxis(float cm, bool checkrange) which check that the major axis is between 1 and 10, if not it prints a warning. #include <iostream> #include <TrapeziumO.h> using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << "A = " << t->getmajoraxis() << "\n"; cout << "a = " << t->getminoraxis() << "\n"; cout << "h = " << t->getheight() << "\n"; t->setmajoraxis(10.); cout << "A = " << t->getmajoraxis() << "\n"; t->setmajoraxis(5., true); cout << "A = " << t->getmajoraxis() << "\n"; t->setmajoraxis(15., true); cout << "A = " << t->getmajoraxis() << "\n"; delete t; return 0; warning, out of range! Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

148 Exercise 2 #include <iostream> #include <Polygon.h> INHERITANCE: to Polygon.h /.cpp add a private variable _area to polygon class with proper getter and setter using namespace std; int main(){ Trapezium *t = new Trapezium(); cout << "A = " << t->getmajoraxis() << "\n"; t->setmajoraxis(10.); cout << "A = " << t->getmajoraxis() << "\n"; cout << "number of sides = " << t->getnsides() << "\n"; t->setarea(192.); cout << area of trapezium is " << t->getarea() << "\n"; Polygon *p = new Polygon(); p->setarea(792.); cout << area of polygon is " << p->getarea() << "\n"; delete p; delete t; return 0; Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

149 Exercise 3 POLYMORPHISM: into PolygonP.h /.cpp virtualize calculatearea in polygon class, implement it in the derived class trapezium #include <PolygonP.h> int main(){ Trapezium *t = new Trapezium(2.,2.,1.); t->printname(); Polygon *p = new Polygon(); p->printname(); p->calculatearea(); t->calculatearea(); delete p; delete t; return 0; cannot calculate area of unknown polygon calculating trapezium area, it is: 2. Emiliano Mocchiutti, INFN Trieste Programmazione C++ per la Fisica Università degli Studi di Trieste, 2016/

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

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

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III Distributed Real-Time Control Systems Lecture 14 Intro to C++ Part III 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

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

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Abstraction in Software Development

Abstraction in Software Development Abstract Data Types Programmer-created data types that specify values that can be stored (type of data) operations that can be done on the values The user of an abstract data type (ADT) does not need to

More information

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

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

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

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

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies Distributed Real-Time Control Systems Chapter 13 C++ Class Hierarchies 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (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

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

CSc 328, Spring 2004 Final Examination May 12, 2004

CSc 328, Spring 2004 Final Examination May 12, 2004 Name: CSc 328, Spring 2004 Final Examination May 12, 2004 READ THIS FIRST Fill in your name above. Do not turn this page until you are told to begin. Books, and photocopies of pages from books MAY NOT

More information

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

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

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

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

CS11 Intro C++ Spring 2018 Lecture 1

CS11 Intro C++ Spring 2018 Lecture 1 CS11 Intro C++ Spring 2018 Lecture 1 Welcome to CS11 Intro C++! An introduction to the C++ programming language and tools Prerequisites: CS11 C track, or equivalent experience with a curly-brace language,

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory. Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance. Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making

More information

Class Destructors constant member functions

Class Destructors constant member functions Dynamic Memory Management Class Destructors constant member functions Shahram Rahatlou Corso di Programmazione++ Roma, 6 April 2009 Using Class Constructors #include Using std::vector; Datum average(vector&

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

Lecture 2, September 4

Lecture 2, September 4 Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

Object-Oriented Programming in C++

Object-Oriented Programming in C++ Object-Oriented Programming in C++ Pre-Lecture 9: Advanced topics Prof Niels Walet (Niels.Walet@manchester.ac.uk) Room 7.07, Schuster Building March 24, 2015 Prelecture 9 Outline This prelecture largely

More information

Friend Functions, Inheritance

Friend Functions, Inheritance Friend Functions, Inheritance Friend Function Private data member of a class can not be accessed by an object of another class Similarly protected data member function of a class can not be accessed by

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 9, 2016 Outline Outline 1 Chapter 9: C++ Classes Outline Chapter 9: C++ Classes 1 Chapter 9: C++ Classes Class Syntax

More information

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

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

C++ 8. Constructors and Destructors

C++ 8. Constructors and Destructors 8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of

More information

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes

More information

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Introduction to Programming session 24

Introduction to Programming session 24 Introduction to Programming session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel sslides Sharif Universityof Technology Outlines Introduction

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

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

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member

More information

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

the gamedesigninitiative at cornell university Lecture 7 C++ Overview Lecture 7 Lecture 7 So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

More information

COMSW Introduction to Computer Programming in C

COMSW Introduction to Computer Programming in C OMSW 1003-1 Introduction to omputer Programming in Lecture 23 Spring 2011 Instructor: Michele Merler http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html 1 Today Trees (from PP hapter 17) ++ and object

More information

CS11 Introduction to C++ Fall Lecture 1

CS11 Introduction to C++ Fall Lecture 1 CS11 Introduction to C++ Fall 2006-2007 Lecture 1 Welcome! 8 Lectures (~1 hour) Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7 Lab Assignments on course website Available on Monday

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

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

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

File I/O Christian Schumacher, Info1 D-MAVT 2013

File I/O Christian Schumacher, Info1 D-MAVT 2013 File I/O Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Input and Output in C++ Stream objects Formatted output Writing and reading files References General Remarks I/O operations are essential

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ 1 Slide 2 Chapter 9 Separate Compilation and Namespaces Created by David Mann, North Idaho College Slide 3 Overview Separate Compilation (9.1) Namespaces (9.2) Slide

More information

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

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming Chapter 13: Introduction to Classes 1 13.1 Procedural and Object-Oriented Programming 2 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a

More information

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

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

C++_ MARKS 40 MIN

C++_ MARKS 40 MIN C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

#include <iostream> #include <cstdlib>

#include <iostream> #include <cstdlib> Classes and Objects Classes The structure data type can be used in both C and C++ Usually a structure is used to store just data, however it can also be used to store functions that can work on the data.

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

Partha Sarathi Mandal

Partha Sarathi Mandal MA 253: Data Structures Lab with OOP Tutorial 1 http://www.iitg.ernet.in/psm/indexing_ma253/y13/index.html Partha Sarathi Mandal psm@iitg.ernet.in Dept. of Mathematics, IIT Guwahati Reference Books Cormen,

More information

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes

More information

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

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

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Reliable C++ development - session 1: From C to C++ (and some C++ features) Reliable C++ development - session 1: From C to C++ (and some C++ features) Thibault CHOLEZ - thibault.cholez@loria.fr TELECOM Nancy - Université de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas

More information

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE PART A UNIT I 1. Differentiate object oriented programming from procedure oriented programming. 2. Define abstraction and encapsulation. 3. Differentiate

More information

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

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

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 Virtual Functions in C++ Employee /\ / \ ---- Manager 2 Case 1: class Employee { string firstname, lastname; //... Employee( string fnam, string lnam

More information

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

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Friendship Inheritance Multiple Inheritance Polymorphism Virtual Members Abstract Base Classes File Input/Output Friendship

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

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

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

Computational Physics

Computational Physics 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

More information

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

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

C++ basics Getting started with, and Data Types.

C++ basics Getting started with, and Data Types. C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

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

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Topics 7.1 Abstract Data Types 7.2 Object-Oriented Programming

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O Outline EDAF30 Programming in C++ 2. Introduction. More on function calls and types. Sven Gestegård Robertz Computer Science, LTH 2018 1 Function calls and parameter passing 2 Pointers, arrays, and references

More information

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

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?

More information

G52CPP C++ Programming Lecture 17

G52CPP C++ Programming Lecture 17 G52CPP C++ Programming Lecture 17 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Exceptions How to throw (return) different error values as exceptions And catch the exceptions

More information

2. It is possible for a structure variable to be a member of another structure variable.

2. It is possible for a structure variable to be a member of another structure variable. FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information