1 / 27

C++: History, Features, Advantages & Basics for Beginners

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.

rtonia
Download Presentation

C++: History, Features, Advantages & Basics for Beginners

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Lecture 1 Friday, 4 July 2003

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

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

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

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

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

  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 • // starts a comment line • <iostream> for input/output library • The purpose of a header file is to include prototype declaration

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

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

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

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

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

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

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

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

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

  18. 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;

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

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

  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

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

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

  24. 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;

  25. 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;

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

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

More Related