C++ Lecture 1 Friday, 4 July 2003. History of C++ l Built on top of C l C was developed in early 70s...

27
C++ Lecture 1 Friday, 4 July 2003

Transcript of C++ Lecture 1 Friday, 4 July 2003. History of C++ l Built on top of C l C was developed in early 70s...

Page 1: C++ Lecture 1 Friday, 4 July 2003. 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.

C++

Lecture 1Friday, 4 July 2003

Page 2: C++ Lecture 1 Friday, 4 July 2003. 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.

History of C++

Built on top of C C was developed in early 70s from

B and BCPL

Object oriented programming paradigm started with "Smalltalk" in 70s, C++ in 80s, and Java in late 90s.

Page 3: C++ Lecture 1 Friday, 4 July 2003. 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.

Procedural v.s. Object-Based

Traditional programming languages are procedural - "what to do" in each step (Fortran, Pascal, C)

Object-oriented programming emphasizes "what is" (object, their properties, etc)

Page 4: C++ Lecture 1 Friday, 4 July 2003. 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.

OOP

Object-oriented programming is much harder to learn, years of programming experience are needed to be proficient in C++

C++ supports both procedural and object-oriented programming

Page 5: C++ Lecture 1 Friday, 4 July 2003. 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.

Advantage of C++

Industry standard for software development

Marketable skill in IT jobs Most advanced features in object

oriented languages Graphical User Interface (GUI)

environment (e.g., Visual C++)

Page 6: C++ Lecture 1 Friday, 4 July 2003. 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.

What to Cover

Lecture 1, Ch 1,2 Lecture 2, Ch 3,4 Lecture 3, Ch 5,15 Lecture 4, Ch 6 Lecture 5, Ch 7 Lecture 6, Ch 9 Lecture 7, Ch 10

Lecture 8, Ch 11 Lecture 9, Ch 12 Lecture 10, Ch 13,

14 GUI with visual

C++

Page 7: C++ Lecture 1 Friday, 4 July 2003. 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.

Buy your textbook if you have not done so

"C++ How to Program"4th edition

Deitel & Deitel

Page 8: C++ Lecture 1 Friday, 4 July 2003. 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.

A Simple C++ Program, Fig 1.2

// starts a comment line <iostream> for input/output

library The purpose of a header file is to

include prototype declaration

Page 9: C++ Lecture 1 Friday, 4 July 2003. 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.

Datatypes in C++

Available built-in data types:

• int - integer

• double - double precision

• float - single precision

• char - character

Page 10: C++ Lecture 1 Friday, 4 July 2003. 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.

Char v.s. String

c = 'a'; // c is a single character

s = "string"; // s may be a pointer // to an array of // 7 characters

Page 11: C++ Lecture 1 Friday, 4 July 2003. 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.

Input/Output in C++

cout << "string"; // for output // to standard out

cin >> variable ; // for input // from standard in to variable

Page 12: C++ Lecture 1 Friday, 4 July 2003. 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.

Input/Output in C++

cout << "string1" << x << "string2" << endl;

// concatenating several outputs.

cin >> x >> y ; // input two // numbers

Page 13: C++ Lecture 1 Friday, 4 July 2003. 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.

Fig.1.6

Standard notations for • + addition

• -- subtraction

• * multiplication

• / division

• % modulus

Page 14: C++ Lecture 1 Friday, 4 July 2003. 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.

Conditional Statements

If (cond) statement ;

if (cond) { stat1 ; } else { stat2 ; } C.f. Fig.1.14

Page 15: C++ Lecture 1 Friday, 4 July 2003. 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.

Logical Comparison

== equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to

C.f. Fig.1.14

Page 16: C++ Lecture 1 Friday, 4 July 2003. 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.

Result of logical expression is 0 or 1

In C++, 0 means false, 1 means true (actually, any

nonzero values mean true)

e.g. k = 2 < 3; // k gets 1

Page 17: C++ Lecture 1 Friday, 4 July 2003. 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.

Loops with while( …)

while ( condition ) {statements;

}

Programming example, Fig.2.7. Class average grade.

Page 18: C++ Lecture 1 Friday, 4 July 2003. 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.

Assignment Operators

X op= Y is equivalent toX = X op Ywhere op can be +, -, *, /, %, etc

E.g.,c += 7; // same as c = c + 7;f /= 3; // same as f = f / 3;

Page 19: C++ Lecture 1 Friday, 4 July 2003. 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.

Increment and Decrement Operators

++ increases a value of a variable by one -- -- decreases the value of a variable by

one ++i value of expression is the value

after increment i++ value of expression is the value before

increment in any case, i is added by 1

c.f Fig.14

Page 20: C++ Lecture 1 Friday, 4 July 2003. 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.

"for" Loop

for( initialize; cond; incre ) { statements;

}

E.g.; for(int j = 0; j < 10; j++) {

…} C.f. Fig.2.21

Page 21: C++ Lecture 1 Friday, 4 July 2003. 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.

I/O Control (iomanip)

setw(w) sets the width of next output to w.

fixed, fixed location for ‘.’ setprecision(d) prints d digits after

decimal point

C.f. Fig.2.21

Page 22: C++ Lecture 1 Friday, 4 July 2003. 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.

Multiple Choices with "switch"

Switch (x) { case 'a': statements; break; case 'b': … default: …}

C.f. Fig.2.22

Page 23: C++ Lecture 1 Friday, 4 July 2003. 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.

Namespaces

New standard allows the implementation of namespaces.

Each software component (package) can declare a namespace, so name conflicts is minimized.

The standard C++ library has the name space std.

Page 24: C++ Lecture 1 Friday, 4 July 2003. 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.

Namespaces

Use the double colon "::" to specify the namespace of a variable.

E.g.,std::cout

The std prefix can be omitted if we say using namespace std;

Page 25: C++ Lecture 1 Friday, 4 July 2003. 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.

Exercise p.66, 1.19

A) What, if anything, is printed, assuming x = 2, y = 3.a) cout << x;b) cout << x + x;c) cout < < "x=";d) cout << "x = " << x;e) cout << x+y << " = " << y + x;f) z = x + y;g) cin >> x >> y;

Page 26: C++ Lecture 1 Friday, 4 July 2003. 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.

What does the program print? (p.156, 2.15)

#include <iostream>int main(){ int y, x = 1, total = 0; while( x <= 10) { y = x * x; cout << y << endl; total += y; ++x; } cout << "Total is " << total << endl; return 0;}

Page 27: C++ Lecture 1 Friday, 4 July 2003. 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.

Exercise p.163, 2.38

Write a complete program to compute exp(x), using the formulaexp(x) = 1 + x/1! + x^2/2! + …