introduction to c version 1 1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to C++ Version 1.1 PowerPoint Presentation
Download Presentation
Introduction to C++ Version 1.1

Loading in 2 Seconds...

play fullscreen
1 / 52

Introduction to C++ Version 1.1 - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

Introduction to C++ Version 1.1. Topics. C++ Structure Primitive Data Types I/O Casting Strings Control Flow. Objective. At the end of this lesson students should be able to: Read and follow instructions, Write simple C++ programs using the standard I/O library,

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

Introduction to C++ Version 1.1


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
topics
Topics

C++ Structure

Primitive Data Types

I/O

Casting

Strings

Control Flow

slide3

Objective

  • At the end of this lesson students should
  • be able to:
  • Read and follow instructions,
  • Write simple C++ programs using the standard I/O library,
  • the String class,
  • and primitive data types.
slide4

Review: A Simple C# Program

using System;

static class Program

{

// a constant

const int SIZE = 5;

static void Main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

}

}

slide5

Let’s Convert it to C++

using System;

class Program

{

// a constant

const int SIZE = 5;

static void Main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

}

}

slide6

using System; //replaced by #include<iostream>

static class Program

{

// a constant

const int SIZE = 5;

static void Main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

}

}

slide7

Let’s Convert it to C++

#include <iostream

C++ is not a pure object oriented

language, so all code does not need

to be enclosed inside of a class.

using System;

class Program

{

// a constant

const int SIZE = 5;

static void Main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

}

}

slide8

Let’s Convert it to C++

#include <iostream>

using System;

// a constant

const int SIZE = 5;

static void Main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

}

Main does not have to be static,

in C++ it normally returns an int,

and it is all lower case

int main( )

return 0;

slide9

Let’s Convert it to C++

#include <iostream>

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

return 0;

}

The syntax of basic declarations,

arithmetic, and control statements

are the same as in C#

slide10

Let’s Convert it to C++

#include <iostream>

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.WriteLine("The average value is {0}", average);

Console.ReadLine();

return 0;

}

C++ I/O is much different from C#

(we’ll discuss the details later)

cout << “The average value is “ << average << endl;

slide11

Let’s Convert it to C++

#include <iostream>

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

Console.ReadLine();

return 0;

}

To keep the console window open,

we’ll use a system call

cout << “The average value is “ << average << endl;

system(“PAUSE”);

slide12

Let’s Convert it to C++

#include <iostream>

using namespace std;

using System;

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

system(“PAUSE”);

return 0;

}

Namespaces are declared differently

in C++. Everything we will use is in

the standard (std) namespace.

cout << “The average value is “ << average << endl;

slide13

Let’s Convert it to C++

#include <iostream>

Finally, we need to add a new

pre-processor directives, that includes

header files that are required for

our program to compile correctly.

The iostream header file is required

when doing console I/O.

using namespace std;

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

system(“PAUSE”);

return 0;

}

cout << “The average value is “ << average << endl;

slide14

Here’s the Final C++ Program

#include <iostream>

using namespace std;

// a constant

const int SIZE = 5;

int main( )

{

// a local variable

double average =32.5;

// arithmetic

double newValue = average * SIZE;

system(“PAUSE”);

return 0;

}

cout << “The average value is “ << average << endl;

slide15

A Good C++ Code Skeleton

#include <iostream>

using namespace std;

// declare global constants here

int main( )

{

// declare local variable variables here

// C++ statements

cout << “Press any key to continue … “;

system(“PAUSE”);

return 0;

}

emailing a program
Emailing a Program
  • Go the project folder
    • Documents/Visial Studio/Projects
    • Demo_101
  • Right click->Send to Compressed (Zipped) Folder
  • Demo_101.zip
  • Rename to Demo_101.zipd
  • Email to 1410dennis@gmail.com
slide17

Primitive Data Types

C++ has fewer primitive data types than does C#,

but the primary ones that we will use are exactly

the same … int, double, bool, and char.

There is a major difference between C++ data types

and C# data types. In C++, the size of a data type

is determined by the underlying hardware, it is not

defined by the language.

slide18

Assignment and Arithmetic work

just as they do in C#.

slide19

C++ Input and Output

In place of C#s Console class, we need two

C++ Objects to do console input and output

slide20
cin

cinis an object of the istreamclass. To use cin, you must

#include iostream in your program.

This object represents the standard input stream. The

cin object is created automatically for you.

keyboard buffer

keyboard buffer

program

cin

slide21
cout

coutis an object of the ostream class. This object

represents the standard output stream. It

is also created automatically for you.

display buffer

output buffer

cout

program

the stream insertion operator
The Stream Insertion Operator, <<

a binary operator (it takes two operands)

the left hand operand must be an output stream

the right hand operand

  • is converted into text
  • the text data is then copied into the stream
slide23

the integer 5

a

0000 0000 0000 0101

the character 5

0000 0000 0011 0101

display buffer

cout

int a = 5;

cout << a;

5

slide24

multiple pieces of data are output by

cascading the << operator …

cout << “The answer is “ << a;

slide25

If you want data to appear on a new line, you must

explicitly add the newline character to the output

stream. There is noWriteLine operation.

Special characters are added to the stream

using the escape character \

\t

\n

etc …

cout << “The answer is “ << a << ‘\n’;

slide26

the endlstream manipulator can be added to the output

stream. It does two things:

It adds a new line to the stream.

It forces the buffer to be output

cout << “Hello” << endl;

formatting numbers
Formatting Numbers

Output formatting in C++ is quite different from

C#’s output formatting.

slide28

To display a double or a float in standard decimal notation

The default formatting, is the

“general” format. In this case

precision defines the number of

digits in total to be displayed

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

setf( ) is a function in the cout object. It is responsible for

setting formatting flags. We will study these in much more

detail in a later section. In this case, we are setting the

ios::fixed flag. This makes the output appear as a normal

decimal number instead of in scientific notation.

the ios::showpoint flag guarantees that a decimal point

will be displayed in the output.

the cout.precision( ) function determines how many digits

will be displayed after the decimal point.

example
Example

double price = 78.5;

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precison(2);

cout << “The price is $” << price << endl;

the stream extraction operator
The stream extraction operator, >>

Also a binary operator

The left operand must be an input stream

Any initial white space is skipped, then the stream is read up to the next white space character ( tab, space, new-line )

If necessary, the text just read is converted to match the type of the right operand. An error occurs if the conversion cannot be done.

The data is stored in the right operand

slide31

a

0000 0000 0000 0101

int a;

cin >> a;

the character 5

0000 0000 0011 0101

keyboard buffer

cin

5 72 hello

reading stops when white space is encountered

slide32

In C#, we always read a string from the

Console, and then used a Parse method to

convert the string to the desired data type.

In C++, this conversion happens automatically.

However, no exception occurs if the conversion

cannot be done.

slide33

Failed Input

Consider the following:

A program contains the statements

int number = 0;

cin >> number

What happens if the user types the letter “t”?

slide34

The stream extraction operator is not able to

convert the letter “t” into a proper integer value.

Three important things happen, without warning:

No input occurs, so the variable number

contains whatever value it had previously.

2. The letter “t” remains in the input buffer, so

a subsequent read operation will try to read it by error.

3. The object cin sets itself to a “failed” state, and

subsequent input operations will all fail.

slide35

Handling Failed Input

As noted, if input fails, no data is input.

We can detect when this happens by testing the state of

the input stream object.

if (cin.fail( ) )

{

cout << “Invalid input occured”;

. . .

}

slide36

There is a well known idiom in C++ that makes

handling failed input much easier. The expression

cin >> number;

has a value, which is the value of the object cin itself.

if the value of cin is “good”, we can go ahead and

process the data. The statement to do this looks like

if (cin >> number)

{

// process the input

}

slide37

Recall that once the stream object fails, all subsequent

read operations will fail. How do we fix that?

The stream objects have a member function named

clear( ), that resets the failed state back to good.

cin.clear( );

slide38

Using the Stream State to Control a Loop

This code will process user input until a non-integer value is typed:

cout << “\nEnter an integer value (or ‘q’ to quit): “;

while (cin >> number)

{

// process the data

}

cin.clear( ); // clear the fail state

string dummyValue; // get the ‘q’ out of the buffer

cin >> dummyValue;

. . .

slide40

You can control the size of an input field with

the setw( n ) stream manipulator.

cin >> setw(5) >> title;

But … keep in mind that this can leave data in the buffer.

cin get
cin.get

The get function of the istream class works similar

to the stream extraction operator. With no parameter,

it gets one character from the input stream.

cin.get( );

cin ignore
cin.ignore( )

This function reads in a character and ignores

it. The character read in is discarded.

cin.ignore( );

cin ignore1
cin.ignore( )

This version of the function reads in n characters

and ignores them.

cin.ignore(n);

This version of the function reads in n characters

or until it encounters the delimiter character,

and ignores the characters read.

cin.ignore(n, ‘\n’);

slide44

Casting

In C++, you can use the same style of cast

as you did in C#, but the preferred (NOT!) way to

cast in C++ is to write, for example

int number = static_cast<int> myDoubleValue;

slide45

Strings

C++ has a string class what is similar to C#’s string class.

slide46

Declaring a String

(you must #include <string>)

string myName;

string myName = Prof. deBry”;

reading a line into a string
Reading a line into a string

This function is similar to cin.get( )except that

it reads an entire line of data, including spaces,

and the data is stored in a string object. This is

the preferred way of reading in a line of data.

It is equivalent to C#’s ReadLine method.

getline(cin, stringName);

cin ignore again
cin.ignore( ) … again

Why is ignore useful? Try the following code…

int numItems;

string description;

cout << “\nenter the number of items and description: “;

cin >> numItems;

getline(cin, description);

When prompted, enter the data on two lines.

slide49

C++ Control Flow

With a few minor exceptions, the statements that control

flow through a C++ program look and work the same as

they do in C#

slide50

Switch

In C#, each case must contain a break statement. It

is illegal to drop through from one case to the next.

slide51

This is valid code in C++

switch (num)

{

case 1:

a = a + 5;

b = b + 3;

case 2:

a = a + 6;

b = b + 4;

case 3:

a = a – 7;

b = b – 1;

}

if num =1, then

this code executes

if num = 2, then

this code executes

if num = 3, only

this code executes