Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of C++ Chapter 2 in both books programs from books keycode for lab: 68394 get Program 1 from web test files.

Similar presentations


Presentation on theme: "Overview of C++ Chapter 2 in both books programs from books keycode for lab: 68394 get Program 1 from web test files."— Presentation transcript:

1 Overview of C++ Chapter 2 in both books programs from books keycode for lab: 68394 get Program 1 from web test files

2 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; }

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

5 Program format // # compiler directive // you will nearly always need: #include // for your header files #include "myHeader.h"

6 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

7 Program format // Global constants and variables should be declared here // constants const type CONST_NAME // global variables - use sparingly type globalVar

8 main program int main ( void) { // declarations type varName // body of main program return 0;// 0 => success }// end of main

9 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

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

11 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

12 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

13 Examples Output cout << "Program …" << endl; Input: cout << "enter data: "; cin >> data;

14 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 15 Hello.cpp // FILE: Hello.cpp // DISPLAYS A USER'S NAME #include using namespace std; int main () {

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

17 17 2.4 Executable Statements t Memory status –Before and after t Assignments –Form: result = expression; –sizeInSqyards = metersToYards * sizeInMeters; –sum = sum + item;

18 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

19 Picture Gallery At bottom of web page for your section Each student can put a picture, an email link, some text about themselves, link to another personal web page, or nothing

20 Gallery How To You can email me the information at tcole@cs.boisestate.edu Submit it using submit tcole cs117 pg

21 Variables Variables provide a means to store values that may change as the program runs.

22 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

23 Declarations t format t type varID; t int i; t Comma used to separate identifiers t double var1, var2, var3;

24 24 2.2 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 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 26 Reserved Words and Identifiers t Special symbols –C++ has rules for special symbols –= * ; { } ( ) // > t Appendix B –Examples of reserved words –Special characters

27 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

28 28 2.3 Data Types and Declarations t Predefined data types –int(integers) Positive or negative whole numbers 1000 12199100000 INT_MAX- largest int allowed by compiler –float(real numbers) Positive or negative decimal numbers 10.51.2100.0299.88

29 29 Data Types and Declarations t Predefined data types –bool(boolean) true false –char(Characters) Represent characters

30 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 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 32 Data Types and Declarations t Floating-point types represent real numbers –Integer part –Fractional part t The number 108.1517 breaks down into the following parts –108 - integer part –1517 - fractional part

33 33 Data Types and Declarations t C++ provides three floating-point types –float –double –long double

34 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 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 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

37 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 \

38 Special Characters \nnewline \ttab \"double quote in a text string \'for the single quote character \\to put a \ into a text string


Download ppt "Overview of C++ Chapter 2 in both books programs from books keycode for lab: 68394 get Program 1 from web test files."

Similar presentations


Ads by Google