Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

Similar presentations


Presentation on theme: "C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:"— Presentation transcript:

1 C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

2 C++ Programming, Namiq Sultan2 2.1 The Parts of a C++ Program C++ programs have parts and components that serve specific purposes.

3 C++ Programming, Namiq Sultan3 Program 2-1 //A simple C++ program #include using namespace std; int main () { cout<< “Programming is great fun!”; return 0; } Program Output: Programming is great fun!

4 C++ Programming, Namiq Sultan4 Table 2-1 Special Characters

5 C++ Programming, Namiq Sultan5 2.2The 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. l Its job is to output information using the standard output device

6 C++ Programming, Namiq Sultan6 Program 2-2 // A simple C++ program #include using namespace std; int main () { cout << “Programming is “ << “great fun!”; return 0; } Output: Programming is great fun!

7 C++ Programming, Namiq Sultan7 Program 2-3 // A simple C++ program #include using namespace std; int main () { cout<< “Programming is “; cout << “ great fun!”; return 0; } Output: Programming is great fun!

8 C++ Programming, Namiq Sultan8 Program 2-4 // An unruly printing program #include using namespace std; int main() { cout << "The following items were top sellers"; cout << "during the month of June:"; cout << "Computer games"; cout << "Coffee"; cout << "Aspirin"; return 0; } Program Output The following items were top sellersduring the month of June:Computer gamesCoffeeAspirin

9 C++ Programming, Namiq Sultan9 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 C++ Programming, Namiq Sultan10 Program 2-5 // A well-adjusted printing program #include using namespace std; int main() { 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; return 0; }

11 C++ Programming, Namiq Sultan11 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

12 C++ Programming, Namiq Sultan12 Program 2-6 // Another well-adjusted printing program #include using namespace std; int main() { cout << "The following items were top sellers" << endl; cout << "during the month of June:" << endl; cout << "Computer games" << endl << "Coffee"; cout << endl << "Aspirin" << endl; return 0; }

13 C++ Programming, Namiq Sultan13 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

14 C++ Programming, Namiq Sultan14 Program 2-7 // Yet another well-adjusted printing program #include using namespace std; int main() { cout << "The following items were top sellers\n"; cout << "during the month of June:\n"; cout << "Computer games\nCoffee"; cout << "\nAspirin\n"; return 0; }

15 C++ Programming, Namiq Sultan15 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

16 C++ Programming, Namiq Sultan16 Table 2-2

17 C++ Programming, Namiq Sultan17 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 C++ Programming, Namiq Sultan18 2.4 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 C++ Programming, Namiq Sultan19 Program 2-8 #include using namespace std; int main() { int value; value = 5; cout << “The value is “ << value << endl; return 0; } Program Output: The value is 5

20 C++ Programming, Namiq Sultan20 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 C++ Programming, Namiq Sultan21 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 C++ Programming, Namiq Sultan22 2.5 Identifiers Must not be a keyword 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

23 C++ Programming, Namiq Sultan23 Table 2-3 The C++ Keywords Asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid Type name union unsigned using virtual void volatile wchar_t while

24 C++ Programming, Namiq Sultan24 Table 2-4 Some Variable Names Variable NameLegal or Illegal? dayOfWeekLegal 3dGraphIllegal. Cannot begin with a digit. _employee_numLegal june1997Legal Mixture#3Illegal. Cannot use # symbol.

25 C++ Programming, Namiq Sultan25 2.6Integer 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.

26 C++ Programming, Namiq Sultan26 Program 2-12 // This program shows three variables declared on the same line. #include using namespace std; int main() { 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"; return 0; }

27 C++ Programming, Namiq Sultan27 Program Output The Grande Hotel has 15 floors with 300 rooms and 30 suites.

28 C++ Programming, Namiq Sultan28 2.7 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

29 C++ Programming, Namiq Sultan29 Program 2-14 // This program uses character constants #include using namespace std; int main() { char letter; letter = 'A'; cout << letter << endl; letter = 'B'; cout << letter << endl; return 0; }

30 C++ Programming, Namiq Sultan30 Program Output ABAB

31 C++ Programming, Namiq Sultan31 Strings Strings are consecutive sequences of characters and can occupy several bytes of memory. 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.

32 C++ Programming, Namiq Sultan32 Let’s look at an example of how a string is stored in memory. "Sebastian" would be stored. ‘A’ is stored as “A” is stored as Sebats\0ian 65 0

33 C++ Programming, Namiq Sultan33 Review key points regarding characters, and strings: Printable characters are internally represented by numeric codes. Most computers use ASCII codes for this purpose. Characters occupy a single byte of memory. Strings are consecutive sequences of characters and can occupy several bytes of memory. Strings always have a null terminator at the end. This marks the end of the 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.

34 C++ Programming, Namiq Sultan34 2.8 Floating Point Data Types Floating point data types are used to declare variables that can hold real numbers

35 C++ Programming, Namiq Sultan35 // This program uses floating point data types #include using namespace std; int main() { float distance; float mass; distance = 1.495979; mass = 1.989; cout << "The Distance is " << distance << " kilometers \n"; cout << "The mass is " << mass << " kilograms.\n"; return 0; }

36 C++ Programming, Namiq Sultan36 Program Output The Sun is 1.4959 kilometers The mass is 1.989 kilograms.

37 C++ Programming, Namiq Sultan37 2.9 The bool Data Type Boolean variables are set to either true or false

38 C++ Programming, Namiq Sultan38 Program 2-17 #include using namespace std; int main () { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; return 0; }

39 C++ Programming, Namiq Sultan39 Program Output 1 0 Internally, true is represented as the number 1 and false is represented by the number 0.

40 C++ Programming, Namiq Sultan40 2.11 Variable Assignment and Initialization An assignment operation assigns, or copies, a value into a variable. When a value is assigned to a variable as part of the variable’s declaration, it is called an initialization.

41 C++ Programming, Namiq Sultan41 Program 2-19 #include using namespace std; int main () { int month = 2, days = 28; cout << “Month “ << month << “ has “ << days << “ days.\n”; return 0; } Program output: Month 2 has 28 days.

42 C++ Programming, Namiq Sultan42 2.13 Arithmetic Operators There are many operators for manipulating numeric values and performing arithmetic operations. Generally, there are 3 types of operators: unary, binary, and ternary. l Unary operators operate on one operand l Binary operators require two operands l Ternary operators need three operands ( ?: in ch 4)

43 C++ Programming, Namiq Sultan43 Table 2-8

44 C++ Programming, Namiq Sultan44 Program 2-21 // This program calculates hourly wages. The variables are used as follows: // regWages: holds the calculated regular wages. // basePay: holds the base pay rate. // regHours: holds the number of hours worked less overtime. //otWages: holds the calculated overtime wages. // otPay: holds the payrate for overtime hours. // otHours: holds the number of overtime hours worked. //totalWages: holds the total wages. #include using namespace std; int main() { float regWages, basePay = 18.25, regHours = 40.0; float otWages, otPay = 27.78, otHours = 10; float totalWages;

45 C++ Programming, Namiq Sultan45 Program 2-21 regWages = basePay * regHours; otWages = otPay * otHours; totalWages = regWages + otWages; cout << "Wages for this week are $" << totalWages << endl; return 0; }

46 C++ Programming, Namiq Sultan46 2.14Comments Comments are notes of explanation that document lines or sections of a program. Comments are part of a program, but the compiler ignores them. They are intended for people who may be reading the source code. Commenting the C++ Way // Commenting the C Way /* */

47 C++ Programming, Namiq Sultan47 Program 2-22 // PROGRAM: PAYROLL.CPP // Written by Herbert Dorfmann // This program calculates company payroll #include using namespace std; int main(void) { float payRate;// holds the hourly pay rate float hours;// holds the hours worked int empNum;// holds the employee number (The remainder of this program is left out.)

48 C++ Programming, Namiq Sultan48 Program 2-24 /* PROGRAM: PAYROLL.CPP Written by Herbert Dorfmann This program calculates company payroll */ #include using namespace std; int main(void) { float payRate;/* payRate holds hourly pay rate */ float hours;/* hours holds hours worked */ int empNum;/* empNum holds employee number */ (The remainder of this program is left out.)

49 C++ Programming, Namiq Sultan49 2.15 Programming Style Program style refers to the way a programmer uses identifiers, spaces, tabs, blank lines, and punctuation characters to visually arrange a program’s source code. l Generally, C++ ignores white space. l Indent inside a set of braces. l Include a blank line after variable declarations.

50 C++ Programming, Namiq Sultan50 Program 2-26 #include using namespace std; int main( ){float shares=220.0; float avgPrice=14.67; cout <<"There were "<<shares<<" shares sold at $"<<avgPrice<<" per share.\n";return 0;} Program Output There were 220 shares sold at $14.67 per share.

51 C++ Programming, Namiq Sultan51 Program 2-27 // This example is much more readable than Program 2-26. #include using namespace std; int main(void) { float shares = 220.0; float avgPrice = 14.67; cout << "There were " << shares << " shares sold at $"; cout << avgPrice << " per share.\n"; return 0; } Program Output There were 220.0 shares sold at $14.67 per share.


Download ppt "C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:"

Similar presentations


Ads by Google