Object-oriented Programming and Introduction to C++

Similar documents
Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CS2141 Software Development using C/C++ Compiling a C++ Program

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Lab 1: First Steps in C++ - Eclipse

Programming in C/C Lecture 3


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

AN OVERVIEW OF C++ 1

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

Dr. Md. Humayun Kabir CSE Department, BUET

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

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

Polymorphism Part 1 1

EL2310 Scientific Programming

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

Your First C++ Program. September 1, 2010

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

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

CE221 Programming in C++ Part 1 Introduction

Structure of a CRC card: Tips:

Separate Compilation of Multi-File Programs

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Lab 2: ADT Design & Implementation

Partha Sarathi Mandal

COMP322 - Introduction to C++ Lecture 01 - Introduction

C++ Programming for Non-C Programmers. Supplement

LOADING LIBRARY FILES IN C++

Why C++? C vs. C Design goals of C++ C vs. C++ - 2

Data Structures using OOP C++ Lecture 3

Practicum 5 Maps and Closures

CS 376b Computer Vision

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Polymorphism. Zimmer CSCI 330

Unit 1 : Principles of object oriented programming

Outline 2017/03/17. (sections from

Implementing an ADT with a Class

Unified Modeling Language a case study

Introducing C++ to Java Programmers

UEE1303(1070) S 12 Object-Oriented Programming in C++

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

CPSC 427: Object-Oriented Programming

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Inheritance, and Polymorphism.

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Chapter 1: Object-Oriented Programming Using C++

Object Orientated Analysis and Design. Benjamin Kenwright

Abstract Data Types (ADT) and C++ Classes

Short Notes of CS201

Discussion Week 1. TA: Kyle Dewey. Sunday, September 25, 11

C++ Lab 03 - C++ Functions

CS201 - Introduction to Programming Glossary By

Inheritance and Polymorphism

Classes: A Deeper Look

CS11 Intro C++ Spring 2018 Lecture 1

Constructor - example

CS32 Midterm Exam E01, F15, Phill Conrad, UC Santa Barbara Wednesday, 10/28/2015, 11am 12:15pm

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

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

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

Qt Introduction. Topics. C++ Build Process. Ch & Ch 3. 1) What's Qt? 2) How can we make a Qt console program? 3) How can we use dialogs?

EL2310 Scientific Programming

EL2310 Scientific Programming

Introduction to Programming session 24

CIS 190: C/C++ Programming. Classes in C++

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Object Oriented Design

CS 247: Software Engineering Principles. Modules

CSCE 110 PROGRAMMING FUNDAMENTALS

4. Structure of a C++ program

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

1a Computers, Problem Solving!

Midterm Exam 5 April 20, 2015

Programming, numerics and optimization

Use the dot operator to access a member of a specific object.

PIC 10A Objects/Classes

A SHORT COURSE ON C++

System Design and Programming II

Classes in C++98 and C++11

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Chapter 1: An Overview of Computers and Programming Languages. Objectives. Objectives (cont d.) Introduction

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

Scientific Computing

Object Oriented Programming. Solved MCQs - Part 2

Crash Course into. Prof. Dr. Renato Pajarola

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

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

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Exceptions, Case Study-Exception handling in C++.

C++ Programming Lecture 8 Software Engineering Group

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

This assignment requires that you complete the following tasks (in no particular order).

ENERGY 211 / CME 211. Evolution

Crash Course in C++ R F L Evans. www-users.york.ac.uk/~rfle500/

Part III Synchronization A bit of C++ and ThreadMentor

Circle all of the following which would make sense as the function prototype.

Object-oriented Programming in C++

Program Organization and Comments

Introduction to Object-Oriented Programming with C++

Transcription:

Object-oriented Programming and Introduction to C++ 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

(1) abstraction encapsulation inheritance messaging modularity polymorphism

(2) abstraction ability to represent entity as near to the reality as it is only possible encapsulation inheritance messaging modularity polymorphism

(3) abstraction encapsulation inheritance messaging modularity polymorphism

(4) abstraction encapsulation concealment of the explicit access to the object state inheritance messaging modularity polymorphism

(5) abstraction encapsulation inheritance messaging modularity polymorphism

(6) abstraction encapsulation inheritance extensibility of the functionality of implemented classes messaging modularity polymorphism

(7) abstraction encapsulation inheritance messaging modularity polymorphism

(8) abstraction encapsulation inheritance messaging communication with objects can be reduced to signals, objects should be able to manage themselves modularity polymorphism

(9) abstraction encapsulation inheritance messaging modularity polymorphism

(10) abstraction encapsulation inheritance messaging modularity adding new functionality should require minimum changes in the code polymorphism

(11) abstraction encapsulation inheritance messaging modularity polymorphism

(12) abstraction encapsulation inheritance messaging modularity polymorphism extension of inheritance paradigm that provides dynamic binding object type recognition at a runtime

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Paradigm differences (1) Procedural Programming Object-Oriented Programming Focus Algorithm Data Basic data Structure (structure instance) Class (object) unit Routine Function (procedure) Method (message) Routine target To perform (sub)task To change/view object state Routine area Logically consistent inseparable The minimum set of part of an algo- instruction needed to rithm change object state. (code clearness)

Paradigm differences (2) Data access pattern Data modification Dynamic Not possible type recognition Procedural Programming Object-Oriented Programming Everything except local Object provide a minimum possible access variables is public using interface methods called getters/setters. (messaging, encapsula- Data can be changed from any (aware of the structure!) code part tion) Self-management of an object the outside world should not be directly able to change object fields. (encapsulation) Supported (at least minimally like C++ s RTTI)

Paradigm differences (3) Procedural Programming Object-Oriented Programming Data initialization Dedicated functions Constructor / destructor / fi- nalization Memory Memory fully managed by Garbage collector (not management programmer necessary) Exceptions Function results Embedded mechanism Operators For types declared in language The operators can - one cannot rede- be used as functions fine operator for his structure (operator overloading)

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

favors code clearness and systematization is a natural extension of more human-friendly languages like UML thus allows developers to better use of domain knowledge makes it easer to distribute tasks over programmers holds mechanisms supporting code development (e.g. inheritance, modularity)...

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

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

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Header files Object-oriented programming basics poem.h Prosze 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 and filtering results > g++ -E poem.cpp egrep -v "^#"

Multiple usage of the same header file poem2.cpp #include "poem.h" #include "poem.h" by Jan Brzechwa and filtering results > g++ -E poem2.cpp egrep -v "^#"

Preventing from multiple file inclusion poem3.h #ifndef POEM3_H #define POEM3_H Prosze 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 and filtering results > g++ -E poem3.cpp egrep -v "^#"

Constants Object-oriented programming basics poem4.cpp #define Mis Niedzwiedz #include "poem3.h" #include "poem3.h" by anonymous group and filtering results > g++ -E poem4.cpp egrep -v "^#"

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

Creating object files and linking car.h #ifndef CAR_H #define CAR_H namespace cars { class Car { public: virtual void startengine(); }; } #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

Outline Object-oriented programming basics 1 Object-oriented programming basics 2 3

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 hyundai.cpp #include <iostream> #include "hyundai.h" using namespace cars; using namespace std; void Hyundai::startEngine() { Car::startEngine(); cout << "BRM" << endl; } volkswagen.h #ifndef VOLKSWAGEN_H #define VOLKSWAGEN_H #include "car.h" namespace cars { class Volkswagen: public Car { public: virtual void startengine(); }; } #endif volkswagen.cpp #include <iostream> #include "volkswagen.h" using namespace cars; using namespace std; void Volkswagen::startEngine() { Car::startEngine(); cout << "BRM BRM BRM" << endl; }

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; }

Dependencies between files (3) volkswagen.h car.h hyundai.h volkswagen.cpp car.cpp hyundai.cpp main2.cpp volkswagen.o car.o hyundai.o main2.o cars

Dependencies between files (4) volkswagen.h car.h hyundai.h volkswagen.cpp car.cpp hyundai.cpp main2.cpp volkswagen.o car.o hyundai.o main2.o cars

Dependencies between files (5) volkswagen.h car.h hyundai.h volkswagen.cpp car.cpp hyundai.cpp main2.cpp volkswagen.o car.o hyundai.o main2.o cars

Dependencies between files (6) volkswagen.h car.h hyundai.h volkswagen.cpp car.cpp hyundai.cpp main2.cpp volkswagen.o car.o hyundai.o main2.o cars

Dependencies between files (7) volkswagen.h car.h hyundai.h volkswagen.cpp car.cpp hyundai.cpp main2.cpp volkswagen.o car.o hyundai.o main2.o cars

Makefile Object-oriented programming basics Makefile all: cars cars: main2.o car.o volkswagen.o hyundai.o g++ main2.o car.o volkswagen.o hyundai.o -o cars main2.o: main2.cpp car.h volkswagen.h hyundai.h g++ -c main2.cpp -o main2.o car.o: car.cpp car.h g++ -c car.cpp -o car.o volkswagen.o: volkswagen.cpp volkswagen.h car.h g++ -c volkswagen.cpp -o volkswagen.o hyundai.o: hyundai.cpp hyundai.h car.h g++ -c hyundai.cpp -o hyundai.o clean: rm *.o rm cars running make command > make

Task Object-oriented programming basics Using -MM option of g++ compiler create bash script that will create Makefile that will take into account dependencies of files from directory (subdirectories should also be considered) where he is placed. Call him configure. NOTE! If in directory contains more than one file with main function configure should prompt in order to determine which one it should process.

Projects Games checkers chess go gomoku domino literaki (with AI) Utility programs spreadsheet (console) text editor (console)

Project specification Specification Each program should provide full history (containing moves in case of games and changes in case of utility programs). 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 permitted moves. On the other hand should allow every possible permitted moves. Each move should result in consequences prescribed 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 to copy/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 higher grade. Project will be done in group of pairs. Each pair should select project application till next week. Each application should 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, and chosen 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).