object-oriented programming and introduction to c++ · object-oriented programming basics c++ in...
TRANSCRIPT
Object-oriented programming basicsC++ in Linux
How to pass?
Object-oriented Programmingand Introduction to C++
Wojciech Frohmberg1
1Department of Computer SciencePoznan University of Technology
2012.10.07 / OOP Laboratory
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (1)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (2)
abstraction – ability to represent entity as near to the realityas it is only possibleencapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (3)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (4)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation – concealment of the explicit access to theobject stateinheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (5)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (6)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance – extensibility of the functionality ofimplemented classesmessaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (7)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (8)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging – communication with objects can be reducedto signals, objects should be able to manage themselvesmodularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (9)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (10)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity – adding new functionality should requireminimum changes in the codepolymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (11)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism
– extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
OOP principles (12)
abstraction
– ability to represent entity as near to the realityas it is only possible
encapsulation
– concealment of the explicit access to theobject state
inheritance
– extensibility of the functionality ofimplemented classes
messaging
– communication with objects can be reducedto signals, objects should be able to manage themselves
modularity
– adding new functionality should requireminimum changes in the code
polymorphism – extension of inheritance paradigm thatprovides dynamic binding – object type recognition at aruntime
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Paradigm differences (1)
Procedural Programming Object-Oriented Pro-gramming
Focus Algorithm DataBasic dataunit
Structure (structure in-stance)
Class (object)
Routine Function (procedure) Method (message)Routine tar-get
To perform (sub)task To change/view objectstate
Routine area Logically consistent in-separable part of an algo-rithm
The minimum set ofinstruction needed tochange object state.(code clearness)
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Paradigm differences (2)
Procedural Programming Object-Oriented Pro-gramming
Data accesspattern
Everything except localvariables is public
Object provide a min-imum possible accessusing interface methodscalled getters/setters.(messaging, encapsula-tion)
Data modifi-cation
Data can be changedfrom any (aware of thestructure!) code part
Self-management of anobject – the outside worldshould not be directly ableto change object fields.(encapsulation)
Dynamictype recog-nition
Not possible Supported (at least mini-mally like C++’s RTTI)
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Paradigm differences (3)
Procedural Programming Object-Oriented Pro-gramming
Data initial-ization / fi-nalization
Dedicated functions Constructor / destructor
Memorymanage-ment
Memory fully managed byprogrammer
Garbage collector (notnecessary)
Exceptions Function results Embedded mechanismOperators For types declared in lan-
guage - one cannot rede-fine operator for his struc-ture
The operators canbe used as functions(operator overloading)
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
OOP principlesProcedural programming vs OOPAdvantages of OOP
Advantages of OOP
favors code clearness and systematizationis a natural extension of more human-friendly languageslike UML thus allows developers to better use of domainknowledgemakes it easer to distribute tasks over programmersholds mechanisms supporting code development (e.g.inheritance, modularity)...
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Hello world program in C++
hello.cpp#include <iostream>
using namespace std;
int main() {cout << "Hello world" << endl;return 0;
}
Compilation and running (from the command line)> g++ hello.cpp -o hello> ./hello
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Header files
poem.hProsze Panstwa oto Mis.Mis jest bardzo grzeczny dzis.Chetnie Panstwu lapke poda.Nie chce podac? A to szkoda...
poem.cpp#include "poem.h"
by Jan Brzechwa
Preprocessing and filtering results> g++ -E poem.cpp | egrep -v "^#"
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Multiple usage of the same header file
poem2.cpp#include "poem.h"#include "poem.h"
by Jan Brzechwa
Preprocessing and filtering results> g++ -E poem2.cpp | egrep -v "^#"
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Preventing from multiple file inclusion
poem3.h#ifndef POEM3_H#define POEM3_HProsze Panstwa oto Mis.Mis jest bardzo grzeczny dzis.Chetnie Panstwu lapke poda.Nie chce podac? A to szkoda...#endif
poem3.cpp#include "poem3.h"#include "poem3.h"
by Jan Brzechwa
Preprocessing and filtering results> g++ -E poem3.cpp | egrep -v "^#"
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Constants
poem4.cpp#define Mis Niedzwiedz
#include "poem3.h"#include "poem3.h"
by anonymous group
Preprocessing and filtering results> g++ -E poem4.cpp | egrep -v "^#"
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Creating object files and linking
car.h#ifndef CAR_H#define CAR_H
namespace cars {class Car {public:
virtual voidstartEngine();
};}
#endif
car.cpp#include <iostream>#include "car.h"
using namespace cars;using namespace std;
void Car::startEngine() {cout << "BRRRRRMM"
<< endl;}
main.cpp#include "car.h"
using namespace cars;
int main() {Car car;car.startEngine();return 0;
}
creating object files, linking and running> g++ -c car.cpp -o car.o> g++ -c main.cpp -o main.o> g++ *.o -o cars> ./cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Outline
1 Object-oriented programming basicsOOP principlesProcedural programming vs OOPAdvantages of OOP
2 C++ in LinuxGetting startedPreprocessingLinkingMakefiles
3 How to pass?
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (1)
hyundai.h#ifndef HYUNDAI_H#define HYUNDAI_H#include "car.h"namespace cars {
class Hyundai: public Car {public:
virtual void startEngine();};
}#endif
volkswagen.h#ifndef VOLKSWAGEN_H#define VOLKSWAGEN_H#include "car.h"namespace cars {
class Volkswagen: public Car {public:
virtual void startEngine();};
}#endif
hyundai.cpp#include <iostream>#include "hyundai.h"
using namespace cars;using namespace std;
void Hyundai::startEngine() {Car::startEngine();cout << "BRM" << endl;
}
volkswagen.cpp#include <iostream>#include "volkswagen.h"
using namespace cars;using namespace std;
void Volkswagen::startEngine() {Car::startEngine();cout << "BRM BRM BRM" << endl;
}
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (2)
main2.cpp#include "hyundai.h"#include "volkswagen.h"
using namespace cars;
int main() {Car *car;car = new Volkswagen();car->startEngine();delete car;car = new Hyundai();car->startEngine();return 0;
}
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (3)
car.h hyundai.hvolkswagen.h
car.cpp hyundai.cppvolkswagen.cpp main2.cpp
car.o hyundai.ovolkswagen.o main2.o
cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (4)
car.h hyundai.hvolkswagen.h
car.cpp hyundai.cppvolkswagen.cpp main2.cpp
car.o hyundai.ovolkswagen.o main2.o
cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (5)
car.h hyundai.hvolkswagen.h
car.cpp hyundai.cppvolkswagen.cpp main2.cpp
car.o hyundai.ovolkswagen.o main2.o
cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (6)
car.h hyundai.hvolkswagen.h
car.cpp hyundai.cppvolkswagen.cpp main2.cpp
car.o hyundai.ovolkswagen.o main2.o
cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Dependencies between files (7)
car.h hyundai.hvolkswagen.h
car.cpp hyundai.cppvolkswagen.cpp main2.cpp
car.o hyundai.ovolkswagen.o main2.o
cars
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Makefile
Makefileall: cars
cars: main2.o car.o volkswagen.o hyundai.og++ main2.o car.o volkswagen.o hyundai.o -o cars
main2.o: main2.cpp car.h volkswagen.h hyundai.hg++ -c main2.cpp -o main2.o
car.o: car.cpp car.hg++ -c car.cpp -o car.o
volkswagen.o: volkswagen.cpp volkswagen.h car.hg++ -c volkswagen.cpp -o volkswagen.o
hyundai.o: hyundai.cpp hyundai.h car.hg++ -c hyundai.cpp -o hyundai.o
clean:rm *.orm cars
running make command> make
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Getting startedPreprocessingLinkingMakefiles
Task
Using -MM option of g++ compiler create bash script that willcreate Makefile that will take into account dependencies offiles from directory (subdirectories should also be considered)where he is placed. Call him configure.NOTE! If in directory contains more than one file with mainfunction configure should prompt in order to determine whichone it should process.
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Projects
Gamescheckerschessgogomokudominoliteraki (with AI)
Utility programs
spreadsheet (console)text editor (console)
Wojciech Frohmberg Object-oriented Programming and Introduction to C++
Object-oriented programming basicsC++ in Linux
How to pass?
Project specification
SpecificationEach program should provide full history (containing moves in case of games and changes in case of utilityprograms). Programs should be intuitive and easy to use.Games should be able to identify the final state (e.g. check-mat in chess). Games should allow only permittedmoves. On the other hand should allow every possible permitted moves. Each move should result in consequencesprescribed by game rules (e.g. when we surround the opponent’s pawn in go they should be taken as prisoners).Utility application should be as useful as possible (e.g. spreadsheet should allow to count a number of functions –sum, mean value, variance and standard deviation, but also should allow to use standard operators like (-, +, *, /)and allows to pass addresses of the cells (relative and absolute). Both spreadsheet and text editor should allow tocopy/cut and paste and saving to file (reading from saved file as well).None of the application requires usage of graphical library(!). You use it at your own risk – this won’t result in highergrade.Project will be done in group of pairs. Each pair should select project application till next week. Each applicationshould be chosen only once for the laboratory group. Declarations of chosen application should be sent by e-mail(add [PO] including brackets to mail subject, group members name, hour and the day of your PO classes, andchosen application in body). First declaration “wins”.Project deadline passes 2012.11.19. Please send me your project till 2012.11.16 to 3 o’clock. Every week of delay =-0.5.Any doubts should be solved be e-mail (add [PO] to mail subject unless you want it to be treated as SPAM).
Wojciech Frohmberg Object-oriented Programming and Introduction to C++