1 / 70

Chapter 2. Introduction to C++

Chapter 2. Introduction to C++. 2.1 The Parts of a C++ Program. C++ programs have parts and components that serve specific purposes. Program 2-1. //A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is great fun!”; } Program Output:

olin
Download Presentation

Chapter 2. Introduction to C++

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. Chapter 2. Introduction to C++

  2. 2.1 The Parts of a C++ Program • C++ programs have parts and components that serve specific purposes.

  3. Program 2-1 //A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is great fun!”; } Program Output: Programming is great fun!

  4. Table 2-1

  5. 2.2 The cout Object • Use the cout object to display information on the computer’s screen. • The cout object is referred to as the standard output object. • Its job is to output information using the standard output device

  6. Program 2-2 // A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is “ << “great fun!”; } Output: Programming is great fun!

  7. Program 2-3 // A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is “; cout << “ great fun!”; } Output: Programming is great fun!

  8. Program 2-4 // An unruly printing program #include <iostream.h> void main(void) { cout << "The following items were top sellers"; cout << "during the month of June:"; cout << "Computer games"; cout << "Coffee"; cout << "Aspirin"; } Program Output The following items were top sellersduring the month ofJune:Computer gamesCoffeeAspirin

  9. New lines • cout does not produce a newline at the end of a statement • To produce a newline, use either the stream manipulator endl • or the escape sequence \n

  10. Program 2-5 // A well-adjusted printing program #include <iostream.h> void main(void) { cout << "The following items were top sellers" << endl; cout << "during the month of June:" << endl; cout << "Computer games" << endl; cout << "Coffee" << endl; cout << "Aspirin" << endl; }

  11. Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

  12. Program 2-6 // Another well-adjusted printing program #include <iostream.h> void main(void) { cout << "The following items were top sellers" << endl; cout << "during the month of June:" << endl; cout << "Computer games" << endl << "Coffee"; cout << endl << "Aspirin" << endl; }

  13. Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

  14. Program 2-7 // Yet another well-adjusted printing program #include <iostream.h> using namespace std; void main(void) { cout << "The following items were top sellers\n"; cout << "during the month of June:\n"; cout << "Computer games\nCoffee"; cout << "\nAspirin\n"; }

  15. Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

  16. Table 2-2

  17. 2.3 The #include Directive • The #include directive causes the contents of another file to be inserted into the program • Preprocessor directives are not C++ statements and do not require semicolons at the end

  18. 2.5 Variables and Constants • Variables represent storage locations in the computer’s memory. Constants are data items whose values do not change while the program is running. • Every variable must have a declaration.

  19. Program 2-8 #include <iostream.h> void main(void) { int value; value = 5; cout << “The value is “ << value << endl; } Program Output: The value is 5

  20. Assignment statements: Value = 5; //This line is an assignment statement. • The assignment statement evaluates the expression on the right of the equal sign then stores it into the variable named on the left of the equal sign • The data type of the variable was in integer, so the data type of the expression on the right should evaluate to an integer as well.

  21. Constants • A variable is called a “variable” because its value may be changed. A constant, on the other hand, is a data item whose value does not change during the program’s execution.

  22. Program 2-10 #include <iostream.h> void main (void) { int apples; apples = 20; cout<< “Today we sold “ << apples << “ bushels\n”; cout << “of apples.\n”; }

  23. Program Output Today we sold 20 bushels of apples. Where are the constants in program 2-10?

  24. Constants from Program 2-10

  25. 2.6 Focus on Software Engineering: Identifiers • Must not be a key word • Should be mnemonic (give an indication of what the variable is used for) • The first character must be a letter or an underscore • The remaining may be letters, digits, or underscores • Upper and lower case letters are distinct

  26. Table 2-3 The C++ Key Words

  27. Table 2-4 Some Variable Names

  28. 2.7 Integer Data Types • There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables only hold whole numbers.

  29. Table 2-5

  30. Program 2-11 // This program has variables of several of the integer types. #include <iostream.h> void main(void) { int checking; unsigned int miles; long days; checking = -20; miles = 4276; days = 184086; cout << "We have made a long journey of " << miles; cout << " miles.\n"; cout << "Our checking account balance is " << checking; cout << "\nExactly " << days << " days ago Columbus "; cout << "stood on this spot.\n"; }

  31. Program Output We have made a long journey of 4276 miles. Our checking account balance is -20 Exactly 184086 days ago Columbus stood on this spot.

  32. Program 2-12 // This program shows three variables declared on the same // line. #include <iostream.h> void main(void) { int floors,rooms,suites; floors = 15; rooms = 300; suites = 30; cout << "The Grande Hotel has " << floors << " floors\n"; cout << "with " << rooms << " rooms and " << suites; cout << " suites.\n"; }

  33. Program Output The Grande Hotel has 15 floors with 300 rooms and 30 suites.

  34. Hexadecimal and Octal Constants • Hexadecimal numbers are preceded by 0x • Hexadecimal F4 would be expressed in C++ as 0xF4 • Octal numbers are preceded by a 0 • Octal 31 would be written as 031

  35. 2.8 The char Data Type • Usually 1 byte long • Internally stored as an integer • ASCII character set shows integer representation for each character • ‘A’ == 65, ‘B’ == 66, ‘C’ == 67, etc. • Single quotes denote a character, double quotes denote a string

  36. Program 2-13 // This program demonstrates the close relationship between // characters and integers. #include <iostream.h> void main(void) { char letter; letter = 65; cout << letter << endl; letter = 66; cout << letter << endl; }

  37. Program Output A B

  38. Program 2-14 // This program uses character constants #include <iostream.h> void main(void) { char letter; letter = 'A'; cout << letter << endl; letter = 'B'; cout << letter << endl; }

  39. Program Output A B

  40. C-Strings • C-Strings are consecutive sequences of characters and can occupy several bytes of memory. • C-Strings always have a null terminator at the end. This marks the end of the string. • Escape sequences are always stored internally as a single character.

  41. Program 2-15 // This program uses character constants #include <iostream.h> void main(void) { char letter; letter = 'A'; cout << letter << '\n'; letter = 'B'; cout << letter << '\n'; }

  42. Program Output A B

  43. Review key points regarding characters, strings, and C-strings: • Printable characters are internally represented by numeric codes. Most computers use ASCII codes for this purpose. • Characters occupy a single byte of memory. • C-Strings are consecutive sequences of characters and can occupy several bytes of memory. • C-Strings always have a null terminator at the end. This marks the end of the C-string. • Character constants are always enclosed in single quotation marks. • String constants are always enclosed in double quotation marks. • Escape sequences are always stored internally as a single character.

  44. 2.9 The C++ string Class • The string class is an abstract data type. • It provides capabilities that make working with strings easy and intuitive. • You must us the #include <string> statement.

  45. 2.10 Floating Point Data Types • Floating point data types are used to declare variables that can hold real numbers

  46. Table 2-7

  47. Program 2-17 // This program uses floating point data types #include <iostream.h> void main(void) { float distance; double mass; distance = 1.495979E11; mass = 1.989E30; cout << "The Sun is " << distance << " kilometers away.\n"; cout << "The Sun\'s mass is " << mass << " kilograms.\n"; }

  48. Program Output The Sun is 1.4959e+11 kilometers away. The Sun's mass is 1.989e+30 kilograms.

  49. Floating Point Constants • May be expressed in a variety of ways • E notation • decimal notation • (no commas)

  50. 2.11 The bool Data Type • Boolean variables are set to either true or false

More Related