+ All Categories
Home > Documents > Chapter 2: Language C++

Chapter 2: Language C++

Date post: 03-Jan-2016
Category:
Upload: selma-holland
View: 39 times
Download: 0 times
Share this document with a friend
Description:
OBJECT ORIENTED PROGRAMMING USING C++. Chapter 2: Language C++. Lecturer: Nguyen Thi Hien Software Engineering Department E-mail: [email protected] Home page: hienngong.wordpress.com. Historical Notes. C++ owes most to C. Other ancestors are Simula67 and Algol68. - PowerPoint PPT Presentation
Popular Tags:
41
Lecturer: Nguyen Thi Hien Software Engineering Department E-mail: [email protected] Home page: hienngong.wordpress.com Chapter 2: Language C++ OBJECT ORIENTED PROGRAMMING USING C+ +
Transcript
Page 1: Chapter 2: Language C++

Lecturer: Nguyen Thi HienSoftware Engineering DepartmentE-mail: [email protected] page: hienngong.wordpress.com

Chapter 2: Language C++Chapter 2: Language C++

OBJECT ORIENTED PROGRAMMING USING C++

Page 2: Chapter 2: Language C++

Historical Notes

C++ owes most to C. Other ancestors are Simula67and 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 C1998: ISO/IEC 14882 specifies the standard

for C++

2

C++ 1987

Page 3: Chapter 2: Language 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.

Page 4: Chapter 2: Language C++

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.

Page 5: Chapter 2: Language C++

Today:

Extensive analysis of an example

program.

Data types, operators, functions and I/O.

Arrays, strings, pointers

Control structures.

5

Page 6: Chapter 2: Language C++

A typical C++ program// FileID: hello.cpp

// Title: The program doing something

#include <iostream.h>

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;

}

}

Page 7: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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).

Page 8: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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 */

Page 9: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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, ...

Page 10: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 11: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 12: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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).

Page 13: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 14: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 15: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 16: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 17: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 18: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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”.

Page 19: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 20: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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.

Page 21: Chapter 2: Language C++

A typical C++ program

// FileID: hello.cpp// Title: The program doing something

#include <iostream.h>

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

Page 22: Chapter 2: Language C++

Basics of C++ - data types

Some Fundamental data types:

char characters: ’a’, ’b’, ’\n’, ’\0’, ’7’int integers: 3, 6883, -5, 0double floating point numbers: 3.14, 7e9bool true or false.

Also: float, long, unsigned long, short, unsigned char, wchar_t

22

Page 23: Chapter 2: Language C++

Basics of C++ - variables

Declaring variables in a program: char a; int b; double c;

Assignment: b = 4; a = 'w’; c = -3.777; int x = 78;

23

Page 24: Chapter 2: Language C++

Basics of C++ - variables

Constants: const double PI=3.1415926; const int MAXBUFFER=20;

Note: the const keyword is also used for method parameters, methods and return values (later)

24

Page 25: Chapter 2: Language C++

Basics of C++ - operators

Arithmetic operators: +, -, *, /, %

Comparison: ==, !=, <, >, >=, <=

Logical: &&, ||, !

Assignment: =

Bitwise: &, |, ~,^

Shortcuts: +=, *=, ^=, (etc.)

Other: <<, >>, ? :, ->, ., ,

25

Page 26: Chapter 2: Language C++

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

Page 27: Chapter 2: Language C++

Basics of C++ - functions

int someFunction(double f, char c) { // …}

27

Name

Body

ParameterList

Return Type

Page 28: Chapter 2: Language C++

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

Page 29: Chapter 2: Language C++

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.

Page 30: Chapter 2: Language C++

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 <iostream.h>

30

Page 31: Chapter 2: Language C++

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

Page 32: Chapter 2: Language C++

Basics of C++ - Strings

There aren’t any strings in C++.

32

Page 33: Chapter 2: Language C++

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

Page 34: Chapter 2: Language C++

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

Page 35: Chapter 2: Language C++

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

Page 36: Chapter 2: Language C++

Control Structures - Decisions

The if statement:

if ( x > 0 ) {

cout << “positive”;

} else {

cout << “negative or zero”;

}

36

Page 37: Chapter 2: Language C++

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

Page 38: Chapter 2: Language C++

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 atthe end of each iteration

Terminating condition

Page 39: Chapter 2: Language C++

Control Structures - Iteration

The while loop:

while ( condition ) {

// do something

}

39

Equivalent to: for( ; condition ; ) { // do something }

Page 40: Chapter 2: Language C++

Control structures - do … while

The do … while loop:

do {

// something

} while( condition);

40

Equivalent to: // somethingwhile( condition) { // something }

Page 41: Chapter 2: Language C++

Control Structures

And finally:

Yes, C++ has a goto.

Don’t use it.

41


Recommended