Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 7: User-Defined Functions II
Chapter 10.
Lecture 1 The Basics (Review of Familiar Topics).
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Guide To UNIX Using Linux Third Edition
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Fundamentals of C and C++ Programming Control Structures and Functions.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
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.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Chapter 15 - C++ As A "Better C"
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Programming Languages and Paradigms
Basic Elements of C++.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Paradigms
Student Book An Introduction
C Basics.
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.
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Chapter 7 Additional Control Structures
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
COP 3330 Object-oriented Programming in C++
Fundamental Programming
C++ Programming Basics
C++ Programming Lecture 20 Strings
COMS 261 Computer Science I
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++ OBJECT ORIENTED PROGRAMMING USING C++

Historical Notes  C++ owes most to C. Other ancestors are Simula67 and Algol68.  First versions of C++ in 1980 under the name “C with classes”. Since 1983 the name C++ is used.  1990: ANSI/ISO 9899 defines a standard for C  1998: ISO/IEC specifies the standard for C++ 2 C

C++ and C  C is a subset of C++. Advantages: Existing C libraries can be used, efficient code can be generated. But: C++ has the same caveats and problems as C (e.g. pointer arithmetic,…).  C++ can be used both as a low level and as a high level language. 3 We focus on the high level aspects.

C++ and Java  Java is a full object oriented language, all code has to go into classes.  C++ - in contrast - is a hybrid language, capable both of functional and object oriented programming. 4 So, C++ is more powerful but also more difficult to handle than Java.

Today:  Extensive analysis of an example program.  Data types, operators, functions and I/O.  Arrays, strings, pointers  Control structures. 5

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; }

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } The header of the file. Should contain general information as file name, author, description, etc. The compiler ignores these lines (see next slide).

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } This is a C++ comment. Lines starting with // are ignored by the compiler. Also ignored is text enclosed by /* and */

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } A pre-processor directive. Lines starting with a # are interpreted by a pre- processor before the compiler processes the file. Other important directives are #define, #ifdef, #endif, #pragma,...

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } In this example we include the file iostream.h into the program. iostream.h defines classes and objects related to input and output. We need it here because cout is declared there.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } Observation: cout is not a keyword of C++ but an identifier defined in the iostream library.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } A function prototype. In this line the compiler is told about a function with the name doSomething and its signature. The function here has one parameter (int) and no return value (void).

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } Also global variables and class declarations usually come here.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } The main function is the entry point of a C++ program. Each C++ program must have exactly one main() method. The return value of main() is an int. This value is passed to the system which invoked the C++ program.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } The code which implements the main function is enclosed in { and }. Statements enclosed in { and } are called a block.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } We declare a variable p of type int. Other important data types of C++ are char, unsigned int, long, unsigned long, float, double, bool. The variable p is immediately initialised with the value 7.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } The function doSomething() is called with the parameter p.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } This statement prints the string “I have something done.” to the screen. The “stream manipulator” endl outputs a newline and then “flushes the output buffer”.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } Functions with a return value which is not void use the return keyword in order to return their value to the calling function. In the special situation here, the main() method has no calling function. The value 0 is passed back to system when the program is finished. Usually 0 means that the program worked correctly.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } The implementation of the previously defined function doSomething.

A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; }

Basics of C++ - data types  Some Fundamental data types:  charcharacters: ’a’, ’b’, ’\n’, ’\0’, ’7’  intintegers: 3, 6883, -5, 0  doublefloating point numbers: 3.14, 7e9  bool true or false.  Also: float, long, unsigned long, short, unsigned char, wchar_t 22

Basics of C++ - variables  Declaring variables in a program:  char a;  int b;  double c;  Assignment:  b = 4; a = 'w’; c = ;  int x = 78; 23

Basics of C++ - variables  Constants:  const double PI= ;  const int MAXBUFFER=20;  Note: the const keyword is also used for method parameters, methods and return values (later) 24

Basics of C++ - operators  Arithmetic operators:  +, -, *, /, %  Comparison:  ==, !=,, >=, <=  Logical:  &&, ||, !  Assignment:  =  Bitwise:  &, |, ~,^  Shortcuts:  +=, *=, ^=, (etc.)  Other:  >, ? :, ->,.,, 25

Basics of C++ - operators  The unary operators ++ and --:  ++ increment by 1  -- decrement by 1  The language C++ got its name by this operator!  Note, that i++ and ++i have different behaviour […] 26

Basics of C++ - functions int someFunction(double f, char c) { // … } 27 Name Body Parameter List Return Type

Basics of C++ - functions  Please note, that a function is specified by the name and the parameter types. The following functions are all different:  int exampleFunction(int i, char c);  int exampleFunction(double f);  int exampleFunction();  int exampleFunction(char c, int i); 28

Basics of C++ - functions  Pass by reference, example:  void square(int &v) { v = v * v; }  In contrast, pass by value:  int square(int v) { return v * v; } 29 The parameter v is not copied.

Basics of C++: I/O  For output use cout, e.g.  cout << “The result is: “ << result << endl;  For input use cin, e.g.  cin >> x;  Note that iostream.h must be included via  #include 30

Basics of C++ - arrays  Declaration:  int numbers[10];  Declaration & Initialisation:  int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };  Access:  numbers[6] = 2483;  cout << “The fourth prime is “ << primes[4]; 31

Basics of C++ - Strings  There aren’t any strings in C++. 32

Basics of C++ - Strings  There aren’t any strings in C++. O.k., that’s only half of the truth. In fact, by convention, strings are represented in C++ as ’\0’ terminated arrays of characters. In addition, the file string.h declares useful string manipulating functions, e.g. strcpy, strlen which deal with ’\0’ terminated character arrays. 33

Basics of C++ - pointer  A pointer points to a memory location which contains data of a particular type. The contents of a pointer is the address of some data  Declaration:  int *p;  double *aDoublePointer; 34

Basics of C++ - pointer  In C++ pointer and arrays are strongly related (“array = pointer + memory”).  int primes[] = {2, 3, 5, 7, 11 };  int *aPr = primes;  aPr++;  cout << “The third prime is “ << *(aPr + 2); 35 pointer arithmetic The * operator accesses the data on the memory address

Control Structures - Decisions  The if statement: if ( x > 0 ) { cout << “positive”; } else { cout << “negative or zero”; } 36

Control Structures - Decisions  The switch statement - example: int x; cout << "Enter choice (1, 2, or 3)"; cin >> x; switch(x) { case 1: doThis(); break; case 2: doThat(); break; case 3: doSomethingElse(); break; default: cout << "Sorry, invalid Input"; } 37

Control Structures - Iteration  The for loop: for(k = 0; k < 10; k++ ) { cout << “The square of “ << k << “ is “ << k * k << endl; } 38 Start condition Action taking place at the end of each iteration Terminating condition

Control Structures - Iteration  The while loop: while ( condition ) { // do something } 39 Equivalent to: for( ; condition ; ) { // do something }

Control structures - do … while  The do … while loop: do { // something } while( condition); 40 Equivalent to: // something while( condition) { // something }

Control Structures  And finally:  Yes, C++ has a goto. Don’t use it. 41