720 likes | 969 Views
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:
E N D
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: Programming is great fun!
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
Program 2-2 // A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is “ << “great fun!”; } Output: Programming is great fun!
Program 2-3 // A simple C++ program #include <iostream.h> void main (void) { cout<< “Programming is “; cout << “ great fun!”; } Output: Programming is great fun!
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
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
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; }
Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin
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; }
Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin
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"; }
Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin
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
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.
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
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.
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.
Program 2-10 #include <iostream.h> void main (void) { int apples; apples = 20; cout<< “Today we sold “ << apples << “ bushels\n”; cout << “of apples.\n”; }
Program Output Today we sold 20 bushels of apples. Where are the constants in program 2-10?
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
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.
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"; }
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.
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"; }
Program Output The Grande Hotel has 15 floors with 300 rooms and 30 suites.
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
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
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; }
Program Output A B
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; }
Program Output A B
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.
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'; }
Program Output A B
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.
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.
2.10 Floating Point Data Types • Floating point data types are used to declare variables that can hold real numbers
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"; }
Program Output The Sun is 1.4959e+11 kilometers away. The Sun's mass is 1.989e+30 kilograms.
Floating Point Constants • May be expressed in a variety of ways • E notation • decimal notation • (no commas)
2.11 The bool Data Type • Boolean variables are set to either true or false