270 likes | 289 Views
Learn about the history, features, and advantages of C++ programming, including GUI development, built on C language, object-oriented programming, data types, I/O control, loops, and namespaces. Practice basic syntax with examples provided. Suitable for beginners.
E N D
C++ Lecture 1 Friday, 4 July 2003
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.
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)
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
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++)
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++ What to Cover
Buy your textbook if you have not done so "C++ How to Program" 4th edition Deitel & Deitel
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
Datatypes in C++ • Available built-in data types: • int - integer • double - double precision • float - single precision • char - character
Char v.s. String • c = 'a'; // c is a single character • s = "string"; // s may be a pointer // to an array of // 7 characters
Input/Output in C++ • cout << "string"; // for output // to standard out • cin >> variable ; // for input // from standard in to variable
Input/Output in C++ • cout << "string1" << x << "string2" << endl; // concatenating several outputs. • cin >> x >> y ; // input two // numbers
Fig.1.6 • Standard notations for • + addition • -- subtraction • * multiplication • / division • % modulus
Conditional Statements • If (cond) statement ; • if (cond) { stat1 ; } else { stat2 ; } C.f. Fig.1.14
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
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
Loops with while( …) while ( condition ) { statements; } • Programming example, Fig.2.7. Class average grade.
Assignment Operators • X op= Y is equivalent to X = X op Y where op can be +, -, *, /, %, etc • E.g., c += 7; // same as c = c + 7; f /= 3; // same as f = f / 3;
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
"for" Loop for( initialize; cond; incre ) { statements; } • E.g.; for(int j = 0; j < 10; j++) { … } C.f. Fig.2.21
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
Multiple Choices with "switch" Switch (x) { case 'a': statements; break; case 'b': … default: … } C.f. Fig.2.22
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.
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;
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;
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; }
Exercise p.163, 2.38 • Write a complete program to compute exp(x), using the formula exp(x) = 1 + x/1! + x^2/2! + …