chapter 2 introduction to c
Download
Skip this Video
Download Presentation
Chapter 2 Introduction to C++

Loading in 2 Seconds...

play fullscreen
1 / 51

Chapter 2 Introduction to C++ - PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on

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.1 The Parts of a C++ Program. C++ programs have parts and components that serve specific purposes. Program 2-1.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 2 Introduction to C++' - lexine


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 2 introduction to c
Chapter 2Introduction to C++

Namiq Sultan

University of Duhok

Department of Electrical and Computer Engineerin

Reference: Starting Out with C++, Tony Gaddis, 2nd Ed.

C++ Programming, Namiq Sultan

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

C++ Programming, Namiq Sultan

program 2 1
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!

C++ Programming, Namiq Sultan

table 2 1 special characters
Table 2-1 Special Characters

C++ Programming, Namiq Sultan

2 2 the cout object
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

C++ Programming, Namiq Sultan

program 2 2
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!

C++ Programming, Namiq Sultan

program 2 3
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!

C++ Programming, Namiq Sultan

program 2 4
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 ofJune:Computer gamesCoffeeAspirin

C++ Programming, Namiq Sultan

new lines
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

C++ Programming, Namiq Sultan

program 2 5
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;

}

C++ Programming, Namiq Sultan

program output
Program Output

The following items were top sellers

during the month of June:

Computer games

Coffee

Aspirin

C++ Programming, Namiq Sultan

program 2 6
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;

}

C++ Programming, Namiq Sultan

program output1
Program Output

The following items were top sellers

during the month of June:

Computer games

Coffee

Aspirin

C++ Programming, Namiq Sultan

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

}

C++ Programming, Namiq Sultan

program output2
Program Output

The following items were top sellers

during the month of June:

Computer games

Coffee

Aspirin

C++ Programming, Namiq Sultan

table 2 2
Table 2-2

C++ Programming, Namiq Sultan

2 3 the include directive
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

C++ Programming, Namiq Sultan

2 4 variables and constants
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.

C++ Programming, Namiq Sultan

program 2 8
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

C++ Programming, Namiq Sultan

assignment statements
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.

C++ Programming, Namiq Sultan

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

C++ Programming, Namiq Sultan

2 5 identifiers
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

C++ Programming, Namiq Sultan

table 2 3 the c keywords
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

C++ Programming, Namiq Sultan

table 2 4 some variable names
Table 2-4 Some Variable Names

C++ Programming, Namiq Sultan

2 6 integer data types
2.6 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.

C++ Programming, Namiq Sultan

program 2 12
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;

}

C++ Programming, Namiq Sultan

program output3
Program Output

The Grande Hotel has 15 floors

with 300 rooms and 30 suites.

C++ Programming, Namiq Sultan

2 7 the char data type
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

C++ Programming, Namiq Sultan

program 2 14
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;

}

C++ Programming, Namiq Sultan

program output4
Program Output

A

B

C++ Programming, Namiq Sultan

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

C++ Programming, Namiq Sultan

slide32
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

S

e

b

a

s

t

i

a

n

\0

65

65

0

C++ Programming, Namiq Sultan

review key points regarding characters and strings
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.

C++ Programming, Namiq Sultan

2 8 floating point data types
2.8 Floating Point Data Types
  • Floating point data types are used to declare variables that can hold real numbers

C++ Programming, Namiq Sultan

slide35
// 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 Distanceis " << distance << " kilometers \n";

cout << "The mass is " << mass << " kilograms.\n";

return 0;

}

C++ Programming, Namiq Sultan

program output5
Program Output

The Sun is 1.4959 kilometers

The mass is 1.989 kilograms.

C++ Programming, Namiq Sultan

2 9 the bool data type
2.9 The bool Data Type
  • Boolean variables are set to either true or false

C++ Programming, Namiq Sultan

program 2 17
Program 2-17

#include

using namespace std;

int main ()

{

bool boolValue;

boolValue = true;

cout << boolValue << endl;

boolValue = false;

cout << boolValue << endl;

return 0;

}

C++ Programming, Namiq Sultan

program output6
Program Output

1

0

Internally, true is represented as the number 1 and false is represented by the number 0.

C++ Programming, Namiq Sultan

2 11 variable assignment and initialization
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.

C++ Programming, Namiq Sultan

program 2 19
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.

C++ Programming, Namiq Sultan

2 13 arithmetic operators
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.
    • Unary operators operate on one operand
    • Binary operators require two operands
    • Ternary operators need three operands ( ?: in ch 4)

C++ Programming, Namiq Sultan

table 2 8
Table 2-8

C++ Programming, Namiq Sultan

program 2 21
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;

C++ Programming, Namiq Sultan

44

program 2 211
Program 2-21

regWages = basePay * regHours;

otWages = otPay * otHours;

totalWages = regWages + otWages;

cout << "Wages for this week are $" << totalWages << endl;

return 0;

}

C++ Programming, Namiq Sultan

2 14 comments
2.14 Comments
  • 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

/* */

C++ Programming, Namiq Sultan

program 2 22
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.)

C++ Programming, Namiq Sultan

program 2 24
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.)

C++ Programming, Namiq Sultan

2 15 programming style
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.
    • Generally, C++ ignores white space.
    • Indent inside a set of braces.
    • Include a blank line after variable declarations.

C++ Programming, Namiq Sultan

program 2 26
Program 2-26

#include

using namespace std;

int main(){float shares=220.0; float avgPrice=14.67; cout <<"There were "<

Program Output

There were 220 shares sold at $14.67 per share.

C++ Programming, Namiq Sultan

program 2 27
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.

C++ Programming, Namiq Sultan

ad