Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C++ Lecture 1 Friday, 4 July 2003

2 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 started with "Smalltalk" in 70s, C++ in 80s, and Java in late 90s.

3 Procedural v.s. Object-Based l Traditional programming languages are procedural - "what to do" in each step (Fortran, Pascal, C) l Object-oriented programming emphasizes "what is" (object, their properties, etc)

4 OOP l Object-oriented programming is much harder to learn, years of programming experience are needed to be proficient in C++ l C++ supports both procedural and object-oriented programming

5 Advantage of C++ l Industry standard for software development l Marketable skill in IT jobs l Most advanced features in object oriented languages l Graphical User Interface (GUI) environment (e.g., Visual C++)

6 What to Cover l Lecture 1, Ch 1,2 l Lecture 2, Ch 3,4 l Lecture 3, Ch 5,15 l Lecture 4, Ch 6 l Lecture 5, Ch 7 l Lecture 6, Ch 9 l Lecture 7, Ch 10 l Lecture 8, Ch 11 l Lecture 9, Ch 12 l Lecture 10, Ch 13, 14 l GUI with visual C++

7 Buy your textbook if you have not done so "C++ How to Program" 4th edition Deitel & Deitel

8 A Simple C++ Program, Fig 1.2 l // starts a comment line l for input/output library l The purpose of a header file is to include prototype declaration

9 Datatypes in C++ l Available built-in data types: int - integer double - double precision float - single precision char - character

10 Char v.s. String l c = 'a'; // c is a single character l s = "string"; // s may be a pointer // to an array of // 7 characters

11 Input/Output in C++ l cout << "string"; // for output // to standard out l cin >> variable ; // for input // from standard in to variable

12 Input/Output in C++ l cout << "string1" << x << "string2" << endl; // concatenating several outputs. l cin >> x >> y ; // input two // numbers

13 Fig.1.6 l Standard notations for + addition -- subtraction * multiplication / division % modulus

14 Conditional Statements l If (cond) statement ; l if (cond) { stat1 ; } else { stat2 ; } C.f. Fig.1.14

15 Logical Comparison l ==equal to l !=not equal to l >greater than l <less than l >=greater than or equal to l <=less than or equal to C.f. Fig.1.14

16 Result of logical expression is 0 or 1 l In C++, 0 means false, 1 means true (actually, any nonzero values mean true) l e.g. k = 2 < 3; // k gets 1

17 Loops with while( …) while ( condition ) { statements; } l Programming example, Fig.2.7. Class average grade.

18 Assignment Operators l X op= Y is equivalent to X = X op Y where op can be +, -, *, /, %, etc l E.g., c += 7;// same as c = c + 7; f /= 3;// same as f = f / 3;

19 Increment and Decrement Operators l ++ increases a value of a variable by one l -- -- decreases the value of a variable by one l ++i value of expression is the value after increment l i++value of expression is the value before increment l in any case, i is added by 1 c.f Fig.14

20 "for" Loop for( initialize; cond; incre ) { statements; } l E.g.; for(int j = 0; j < 10; j++) { … } C.f. Fig.2.21

21 I/O Control (iomanip) l setw(w) sets the width of next output to w. l fixed, fixed location for ‘.’ l setprecision(d) prints d digits after decimal point C.f. Fig.2.21

22 Multiple Choices with "switch" Switch (x) { case 'a': statements; break; case 'b': … default: … } C.f. Fig.2.22

23 Namespaces l New standard allows the implementation of namespaces. l Each software component (package) can declare a namespace, so name conflicts is minimized. l The standard C++ library has the name space std.

24 Namespaces l Use the double colon "::" to specify the namespace of a variable. l E.g., std::cout l The std prefix can be omitted if we say using namespace std;

25 Exercise p.66, 1.19 l 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;

26 What does the program print? (p.156, 2.15) #include 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; }

27 Exercise p.163, 2.38 l Write a complete program to compute exp(x), using the formula exp(x) = 1 + x/1! + x^2/2! + …


Download ppt "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."

Similar presentations


Ads by Google