Introduction. Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.)

Slides:



Advertisements
Similar presentations
C++ Introduction.
Advertisements

Chapter 3 Objects, types, and values Bjarne Stroustrup
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Chapter 18 Vectors and Arrays
Chapter 9 Technicalities: Classes, etc. Bjarne Stroustrup
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming in C++ John Galletly.
Bjarne Stroustrup started work on C with Classes in 1979 at Bell Labs. In 1983 it was renamed C++.
C++ Programming Languages
C++ Classes & Data Abstraction
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Three types of computer languages
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Chapter 2: Java Fundamentals Java Program Structure.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Introduction to C++ Programming
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
COMPUTER SCIENCE I C++ INTRODUCTION
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
Monday, Jan 6, 2003Kate Gregory with material from Deitel and Deitel CO 204 Object Oriented Programming 2003 Trent University Kate Gregory.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Programming Language Principles Lecture 30 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming.
Learners Support Publications Introduction to C++
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Chapter 15 - C++ As A "Better C"
Introduction to Programming
CSE 374 Programming Concepts & Tools
PROGRAMMING WITH C ++.
C++ Lesson 1.
Introduction to C++ (Extensions to C)
Introduction to C++ Systems Programming.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Object-Orientated Programming
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
جامعة البحر الاحمر كلية العلوم التطبيقية قسم الفيزياء التطبيقية الفصل الداسي الثاني IIالمقرر: حاسوب د. خالد عثمان العالم.
Object-Oriented Programming Part 1
Object-Oriented Programming
Brought to you by C++ Tutorial Brought to you by
جامعة البحر الاحمر كلية العلوم التطبيقية قسمي الحاسوب وتقنية المعلومات الفصل الداسي الثاني المقرر: اساليب برمجة 1 محاضرة رقم 1 د. خالد عثمان العالم.
Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Chapter 2: Java Fundamentals
CECS 130 Final Exam Review Session
Presentation transcript:

Introduction

Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.) course regulations course regulations course materials course materials useful resources useful resources

Plan 1. Introduction 2. Not object oriented C++ extensions 3. OOP, class, object 4. Constructor, destructor 5. Operators 6. static, const and volatile 7. Inheritance, derived classes 8. Virtual methods 9. Multiple inheritance 10. Templates 11. Exceptions 12. Libraries

C++ history C++ is, but some minor details, a superset of the C language (both alive) C++ is, but some minor details, a superset of the C language (both alive) C is a successor of BCPL language (dead) C is a successor of BCPL language (dead) there was no A language there was no A language

C++ — a C with classes data abstraction tool data abstraction tool object oriented programming language object oriented programming language better C better C

Books on C++ Bjarne Stroustrup „Język C++” WNT W-wa Bjarne Stroustrup „Język C++” WNT W-wa International Standard for Information Systems—Programming Language C+ + (draft), ANSI

Books on C++ Stroustrup B.: Projektowanie i rozwój języka C++. WNT, Warszawa Stroustrup B.: Projektowanie i rozwój języka C++. WNT, Warszawa Plauger P.J., Biblioteka standardowa C ++. WNT, Warszawa Plauger P.J., Biblioteka standardowa C ++. WNT, Warszawa

Books on C++ Lippman S.B., Lajoie J.: Podstawy języka C++. WNT, Warszawa Lippman S.B., Lajoie J.: Podstawy języka C++. WNT, Warszawa Tondo C. L., Leung B. P.: Podstawy języka C++. Ćwiczenia i rozwiązania. WNT, Warszawa Tondo C. L., Leung B. P.: Podstawy języka C++. Ćwiczenia i rozwiązania. WNT, Warszawa Grębosz J.: Symfonia C++. RM, W-wa, wyd. 4. Grębosz J.: Symfonia C++. RM, W-wa, wyd. 4. Grębosz J.: Pasja C++. RM, W-wa, wyd. 2. Grębosz J.: Pasja C++. RM, W-wa, wyd. 2.

Not object oriented C++ extensions

Comment /* C comment, valid also in C++, may be several lines long */ // C++ one line comment // ends with End Of Line

Comment We may still use the preprocessor to comment out large blocks of source code #if 0 /* C comment, valid also in C++, may be several lines long */ // C++ comment ends with End Of Line #endif

Comment /* use C comment, or a C++ comment for several lines long comments */ // however, do not mix them !!! void foo()// here stick to C++ comments { return; // they are more convenient }

Standard input and output We may still use C functions We may still use C functions We should use C++ operators We should use C++ operators #include #include int a,b,c,d; cout << „input a and b \n” ; cin >> a; cin >> b; cout << „input c and d \n” cin >> c >> d;

Standard input and output Streams are not so susceptible to errors as (vide scanf() ) Streams are not so susceptible to errors as (vide scanf() ) Streams Streams cin - std. input cin - std. input cout- std. output cout- std. output cerr- std. error output cerr- std. error output Manipulating strings, setting precision, checking stream status, etc. – as functional as in Manipulating strings, setting precision, checking stream status, etc. – as functional as in We should not mix C++ operators and functions We should not mix C++ operators and functions

Declarations are statements declaration inside block is a statement declaration inside block is a statement it may be placed after other statements it may be placed after other statements{ int i=12; cout << „statement”; int j=i; for (int k=0; k<20; k++) j+=k;}

Declaration in „for” int i = 42; int a[10]; for (int i = 0; i < 10; i++) a[i] = i; int j = i; // j = 42

Declaration in „for” for ( forinitstatement; condition; expression ) is equivalent to { forinitstatement while ( condition ) { statement expression ; }

goto more restrictions than in C: more restrictions than in C: inside block only inside block only cannot jump over initialization cannot jump over initialization

goto a: int foo() {b: goto b; int i=7; c: return i; }d: void foofoo() {e:return;}

Types strong type control (as compared to C) – to permit error detection by compiler strong type control (as compared to C) – to permit error detection by compiler automatic type conversions – when it does not lead to ambiguity automatic type conversions – when it does not lead to ambiguity whenever it is possible no precision or information is lost (not guaranteed for all conversions: char/short/int/long/single/float/double/signed/u nsigned) whenever it is possible no precision or information is lost (not guaranteed for all conversions: char/short/int/long/single/float/double/signed/u nsigned)

Types int type assumed when no type specified int type assumed when no type specified unsigned u; const a; static i; explicit specifying the int type is adviced explicit specifying the int type is adviced

Type void * no automatic conversion to other pointer types no automatic conversion to other pointer types void *malloc(size_t size); char *str; int *pi; str = (char *) malloc(10); pi = (int *)malloc(100);

Type void * automatic conversion from any pointer type (but const * and volatile * types) automatic conversion from any pointer type (but const * and volatile * types) void free(void *block); free(str); free(pi);

const named constants are to replace #defined C constants named constants are to replace #defined C constants const five = 5; void fooj(const int ci); consts – regular variables, with address, size, operators, but not to be modified consts – regular variables, with address, size, operators, but not to be modified const prevents programmer from mistakes const prevents programmer from mistakes use of consts permits optimization by a compiler use of consts permits optimization by a compiler

const and pointers pointer to const: const char *pc=”asdf” (or char const *pc=”asdf”) pointer to const: const char *pc=”asdf” (or char const *pc=”asdf”) const pointer: char * const cp=”asdcf”; const pointer: char * const cp=”asdcf”; const pointer to const: const char * const cpc=”asdcf”; const pointer to const: const char * const cpc=”asdcf”; mistakes: pc[1]=‘2’; cp++; cpc[1]=‘b’; cpc--; it is ok. to assign address of non-const to pointer to const it is ok. to assign address of non-const to pointer to const

enum enum numbers {ZERO, ONE, FIVE=5, SIX}; „numbers” is optional name of a new type „numbers” is optional name of a new type conversion from enum to int is automatic conversion from enum to int is automatic why enum is not so popular? why enum is not so popular?

References reference to type T: T& it is a pointer that looks like a variable it is a pointer that looks like a variable int i=7, // variable int & iref=i;// reference must be initialized // i.e. associated with something (i) iref++;// now i==8

References void swap(int &i1, int &i2); int a=2, b=3; swap(a,b); void swap(int &i1, int &i2) { int i3; i3=i1;i1=i2;i2=i3;} i1i2i3ab ?

References – problems problem: actual parameter type is not the same as formal, or is an expression problem: actual parameter type is not the same as formal, or is an expression char c; swap(a+20, c); arguments are converted to const (const int), compiler expects int (not const), it should exit with error, usually issues only a warning. obviously nothing gets swapped

References – problems // int &ir=7; ERROR! 7 is constant const int &ir1=7, // OK &ir2=i+f; // OK /* ir1 and ir2 – references to constant temporary objects living as long as ir1 and ir2 */

References – an useful example int & min(int &i1, int&i2) { return i1<i2 ? i1 : i2; } int a=10, b=20, c; c=min(a,b); min(a,b)++; //now a==11 !

References may be misleading may be misleading avoid functions that alter its arguments avoid functions that alter its arguments use it when You actually need it use it when You actually need it for saving memory for saving memory for saving time for saving time for returning objects to be further processed for returning objects to be further processed use const references use const references

Functions – Prototypes Function prototypes are required!!! (return value, name, arguments) #include „foo_prototypes.h” #include „foo_prototypes.h” void foo(); void foo(); void foo() {}; void foo() {};

Functions – inline inline int sum(int a, int b) { return a+b; } destined to replace macro definitions destined to replace macro definitions inline keyword is only a recommendation for compiler inline keyword is only a recommendation for compiler automatic inline function expansion automatic inline function expansion function expanded inline has no address function expanded inline has no address

Overloaded functions many functions, the same name, OK. in C++ many functions, the same name, OK. in C++ int f(int, int); int f(int); int f(long);

Overloaded functions Address of the overloaded function – compiler has to know which one to pick Address of the overloaded function – compiler has to know which one to pick int (*pd)(long);//OK pd=f; void *pf; // pf=f;// ERROR!

Overloaded functions Parameters of overloaded function must differ, Parameters of overloaded function must differ, Type returned is not considered during compilation, it is examined when function is called Type returned is not considered during compilation, it is examined when function is called int f(int, int); int f(int); int f(long); // OK // int f(int); double f(int); ERROR int ff(int); int ff(double); // OK. // ff(1L) ERROR! ambiguous // void *p=ff(1) ERROR! type

Matching overloaded functions no more then one conversion per argument pick lowest match level, ambiguity is an error exact match (no conversion, but table to pointer, function to pointer, T to const T) promotions (int to long, char to int, unsigned to long unsigned, float to double, etc.) standard conversions (unsigned int ↔ int, int ↔ double, derived * to base *) user defined conversions variable argument list (…)

Matching overloaded functions int f(int); double f(double) void f(); int i1=f(1); // OK int i2=f(1.0); // OK, call double f(double), // then convert result to int //int i3=f(„Hi”); ERROR! no standard conversion //char * to int or double

Overloaded functions convenient extension of manual and automatic conversions convenient extension of manual and automatic conversions int cmp (double a, double b) { return a – b; } int cmp (char *a, char *b) { return strcmp(a,b); }

Overloaded functions we still benefit from automatic conversions we still benefit from automatic conversions cmp (1, 2);//OK., conversion to double prior //to call cmp (double a, double b) we may improve code performance we may improve code performance int cmp (int a, int b)// now cmp(1, 2) without conversion { return a – b; }

Default function arguments void line(int len, char c=‘*’) { for (int i=0; i<len; i++) cout << c; } now this: line(x); now this: line(x); means:line(x, ‘*’); and this: line(x, ‘o’); and this: line(x, ‘o’); means:line(x, ‘o’);

Default function arguments int fun(int i1, int i2=20, int i3=30); starting from first default argument, all remaining also have to be default starting from first default argument, all remaining also have to be default function with default arguments is not overloaded, function with default arguments is not overloaded, &fun(int) == &fun(int, int) we may overload: we may overload: int fun(int i1, int i2); //declaration is not ambiguous, //but the call fun (int, int) is!

Default function arguments mind the „type * =” — „=” must be preceded by space mind the „type * =” — „=” must be preceded by space defaults may be defined only once (formally either in prototype or in definition). defaults may be defined only once (formally either in prototype or in definition). Advice: define overloads in a header file!

Variable function argument list in C we wrote: in C we wrote: int printf(char *, …); in C++ it is so much simpler: in C++ it is so much simpler: int printf(char * …); // comma not required, but only if // variable preceding „…” has no default value. // Macros for accessing args still in // Macros for accessing args still in in C++ it is so much simpler: in C++ it is so much simpler: use overloading instead variable argument list

Memory management allocating memory: operator new allocating memory: operator new syntax: new Type int * pi = new int; // pi = (int*)malloc(sizeof(int)) deallocating: operator delete deallocating: operator delete syntax: delete pointer delete pi; // pi value not altered by delete operator

Memory management dealing with vectors (arrays) dealing with vectors (arrays) int * pai = new int [x]; delete [] pai; programmer must remember whether pointer points to scalar or vector (compiler does not check) programmer must remember whether pointer points to scalar or vector (compiler does not check) multidimensional arrays multidimensional arrays int *pmai = new int[x][20][30] // all dimensions, but the first one have to // be known at the compilation time delete [] pmai;

Memory management we may define function (new_handler) to call when there is not enough memory we may define function (new_handler) to call when there is not enough memory set_new_handler(); // set_new_handler(); // if new_handler is not defined new returns 0 (NULL) if new_handler is not defined new returns 0 (NULL) delete NULL; does nothing delete NULL; does nothing do not mix new/delete and malloc/free do not mix new/delete and malloc/free

Other extensions Templates Templates Exceptions Exceptions Namespaces Namespaces