Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files
2 Hello.cpp char letter1, letter2; string lastName; // Enter letters and print message. cout << "Enter 2 initials and last name: "; cin >> letter1 >> letter2 >> lastName; cout << "Hello " << letter1 << ". " << letter2 << ". " << lastName << "! "; cout << "We hope you enjoy studying C++." << endl; return 0; }
Program format /* begins a c-style comment * (ignored by compiler) * which spans all lines occurring * before the concluding */ // comments which extend to // the end of the line
4 2.1 C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line t /* Text text */ use if comments on multi lines t Don’t embed comments within /* */ comments
Program format // # compiler directive // you will nearly always need: #include // for your header files #include "myHeader.h"
6 Compiler Directives t #command –Compiler directive –Processed at compilation time –include, define, … t #include –Instructs compiler on what you want in the program –Adds library files to program –Used with –Also " " for user defined
Program format // Global constants and variables should be declared here // constants const type CONST_NAME // global variables - use sparingly type globalVar
main program int main ( void) { // declarations type varName // body of main program return 0;// 0 => success }// end of main
Basic Syntax { } can be used to group statements into blocks () can be used in arithmetic expressions to control order of evaluation [] are used for array indices
Basic syntax The words that are part of the language are reserved words or keywords. Identifiers are words you make up A statement corresponds to a sentence; it ends with a semicolon. It may span multiple lines.
Identifiers Consist of letters (a…z, A…Z), digits (0 …9), underscore (_) cannot start with a digit must not be the same as keywords case sensitive
Input and Output C++ uses streams for input and output sequential - data comes out in same order as it goes in character based. >> extraction operator for input << insertion operator for output
Examples Output cout << "Program …" << endl; Input: cout << "enter data: "; cin >> data;
14 iostreams t Stream data type –Object that is a stream of characters –Defined in iostream –Entered on the keyboard(cin) –Displayed on monitor(cout)
15 Hello.cpp // FILE: Hello.cpp // DISPLAYS A USER'S NAME #include using namespace std; int main () {
16 Hello.cpp Program output Enter first two initials and last name and press return: EBKoffman Hello E. B. Koffman! We hope you enjoy studying C++.
Executable Statements t Memory status –Before and after t Assignments –Form: result = expression; –sizeInSqyards = metersToYards * sizeInMeters; –sum = sum + item;
1/25/02 Homework 1 submit tcole cs117 h1 Programs done independently you may discuss general ideas but don't share your code if someone else explains how to do something, acknowledge their contribution in your readme
Picture Gallery At bottom of web page for your section Each student can put a picture, an link, some text about themselves, link to another personal web page, or nothing
Gallery How To You can me the information at Submit it using submit tcole cs117 pg
Variables Variables provide a means to store values that may change as the program runs.
22 Declarations t Set aside memory for storing program data t Use an identifier to refer to a particular piece of data t Each identifier needed must be declared t type associated with identifier determines how much memory is needed t Can declare more than one variable at a time –Comma used to separate identifiers
Declarations t format t type varID; t int i; t Comma used to separate identifiers t double var1, var2, var3;
Reserved Words and Identifiers t Reserved words have special meanings –Can NOT be used for other purposes (const, float and void are some examples) t Identifiers (variables) –Used to store data by the program (user defined) –Valid identifiers - letter, letter1, _letter –Invalid identifiers - 1letter, const, hell o
25 Identifiers t Identifiers should be –Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) –Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word
26 Reserved Words and Identifiers t Special symbols –C++ has rules for special symbols –= * ; { } ( ) // > t Appendix B –Examples of reserved words –Special characters
27 Upper and Lower Case t C++ case sensitive –Compiler differentiates upper & lower case –Identifiers can be either –Be careful though (cost != Cost) t Blank spaces –Use space to make program readable –Use care in placing spaces
Data Types and Declarations t Predefined data types –int(integers) Positive or negative whole numbers INT_MAX- largest int allowed by compiler –float(real numbers) Positive or negative decimal numbers
29 Data Types and Declarations t Predefined data types –bool(boolean) true false –char(Characters) Represent characters
30 Data Types and Declarations t The basic integer type is int –The size of an int depends on the machine and the compiler On pc’s it is normally 16 or 32 bits t Other integers types –short: typically uses less bits –long: typically uses more bits
31 Data Types and Declarations t Different types allow programmers to use resources more efficiently t Standard arithmetic and relational operations are available for these types
32 Data Types and Declarations t Floating-point types represent real numbers –Integer part –Fractional part t The number breaks down into the following parts –108 - integer part – fractional part
33 Data Types and Declarations t C++ provides three floating-point types –float –double –long double
34 Data Types and Declarations t Predefined data types –char(characters) Individual character value (letter or number) Character literal enclosed in single quotes ‘A’ –bool(true / false) t Ordinal types –intboolchar –Values can be listed
35 Data Types and Declarations Character type char is related to the integer types t Characters are encoded using a scheme where an integer represents a particular character
36 Data Types and Declarations t ASCII is the dominant encoding scheme –Examples ' ' encoded as 32 '+' encoded as 43 'A' encoded as 65 'Z' encoded as 90 ’a' encoded as 97 ’z' encoded as 122
Special Characters Some characters can't be entered into the program directly unprintable characters such as tab, newline, new page characters that have a special meaning such as " and ' Represented by two-character sequence that begins \
Special Characters \nnewline \ttab \"double quote in a text string \'for the single quote character \\to put a \ into a text string