cs 1400. primitive data types variables and constants declarations assignment input and output...

91
CS 1400 Classes, Objects, and Primitive Data Version 1.1

Upload: hubert-whitehead

Post on 21-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

CS 1400

Classes, Objects,and Primitive Data

Version 1.1

Page 2: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Topics

Primitive data typesVariables and constantsDeclarations

AssignmentInput and output

Classes and objects

Style

Page 3: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Objectives

At the completion of this topic, students should be able to:

Create proper identifiers in a C++ programDescribe the difference between an object and primitive data

Describe the primitive data types in the C++ languageWrite C++ programs that correctly

* use declarations* use assignment statements* use literal data* use cin and cout* format simple floating point data

Describe the object model of programming

Describe the way that data is stored in the computer

Page 4: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

In order to be able to refer to a piece of datain a program, we have to give that piece of data a name. These names are called identifiers.

Page 5: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

IdentifiersThe name that you use to refer to a piece of dataor a function in C++ is called an identifier.

A C++ identifier must begin with either a letteror an underscore character. [a-zA-Z_]

The remaining characters may be letters, digits, orthe underscore character. All other characters areinvalid. [a-zA-Z_0-9]*

Identifiers can be of any length. Be reasonable!

Identifiers are case sensitive.

Page 6: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Some Valid Identifiers

xx1_abcsumdata2oldValue

It is common in C++ to run words togetherLike this. Just capitalize all words after the first.

Page 7: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Some Invalid Identifiers

123&change1_dollarmy-data

Page 8: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Keywords

C++ has a set of built in keywords that are considered tobe part of the language. You cannot use any of these asidentifiers in your program. A complete list can be foundin your book. Examples include

boolbreakcharclassconstdo…

Page 9: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

A C++ program manages two general kinds of information

Primitive Datathe most basic forms of data - numbers and characters int, double, char, bool

Objectsmore complex data – usually composed ofmany pieces of primitive data cout, cin, string, DATA

Page 10: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Primitive Data

Primitive data elements all have a data type

The data type defines the possible set of valuesthat a primitive data element can have, and theoperations that can be performed on the data.

Page 11: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Numeric Data Types

Type Storage Max Value

short 16 bits -32,766 to 32,767int 32 bits - 2,147,483,646 to 2,147,483,647long 32 bits - 2,147,483,646 to 2,147,483,647float 32 bits over 1038

double64 bits over 10308

integertypes

realtypes

The amount of storage allocated to variousdata types is not defined by the C++ language,but is dependent upon the underlying hardware.The following are examples only, and may differon your computer.

there are unsigned version of each of integer types.

Page 12: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Integer NumbersIntegers are whole numbers they have nofractional part.

The most common integer data type is int

Integer operations includeadditionsubtractionmultiplicationdivisionremainderassignment

Page 13: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Examples of Integers

10-532729053011234567890L

Page 14: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Real NumbersReal numbers have fractional parts

Real numbers are often written in scientific format

The most common real data type is double

Operations on real numbers includeadditionsubtractiondivisionmultiplicationassignment

Page 15: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Examples of Real Numbers

10.5-5.02327.9812905301.0040.0000239897F-1.56E-4

Page 16: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Character Data

Different standards exist for encoding characters

The ASCII standard, finalized in 1968, uses 7 bits for each character.In the ASCII standard, 1000001 is interpreted as the character ‘A’.

7 bits only allows for the definition of 128 unique characters. Subsequentstandards (ISO8859 and ISO10646) define much larger, multi-nationalcharacter sets. However, both are supersets of ASCII.

Character data is defined by the keyword char

When interpreted as a character, certain bit patternsrepresent printable characters and control characters.

Page 17: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Control Characters

Control characters are characters that donot print, but cause some action, such as movingto a new line, to occur. In C++ we write control characters as a backslash, followed by a characterthat denotes the action to be taken.

\b backspace\t tab\n new-line\r carriage return\\ back slash

Page 18: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Boolean Data

A piece of Boolean data can only have oneof two values:

true ( none zero )false (0)

Boolean data is defined by the keyword bool

Page 19: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Variables and Constants

A variable is a name for a memory locationthat holds some piece of data. The valuestored in that location may change duringexecution of the program.

A constant is a name for a memory locationthat holds some piece of data, where thevalue of the data will not change duringexecution of the program.

Page 20: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Declarations

In C++, all variables and constants must be declared before they are used in a program.

C++ is what is known as a strongly typed language.This means that we must tell the compiler what thedata type is for every variable. The compiler thenchecks all operations to make sure that they arevalid for the given type of data.

Page 21: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Question . . .

Assume that you are able to peek into the memory of yourcomputer, and you see the bit pattern

00000000 00000000 00000000 01100010

What does this bit pattern mean?

Page 22: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

The correct answer is that you don’t know.Unless you know what type of data you arelooking at, it is impossible to interpret thebits stored in memory without knowing itstype in storage.

Page 23: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Integer RepresentationIn modern digital computers, integer numbers are stored internally in a binary format. The number of bits used tostore an integer depends on the type of processor in the computer, but is typically 32 or 64 bits.

Example: the integer 5, when storedin a typical Intel class machine is

00000000 00000000 00000000 00000101

Page 24: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Floating Point Representation

Numbers that contain decimal points are stored internally in a very different format. The exact

format depends upon the processor used in the computer, but in general it looks like:

sign exponent Mantissa or Coefficient

for example, the number 6,045.03 (0.604503 x 104) would have an exponent of 4 and a

mantissa of .604503

The actual binary representation is beyond thescope of this course.

Page 25: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Character Representation

Characters are stored internally in a codedformat. For example, using the standardASCII code, the character ‘A’ would be stored as 0100 0001. Most modern computerlanguages support a multiple byte charactercode called Unicode.

Page 26: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

The ASCII Code Table

Page 27: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Computer Instructions

Locations in memory can hold both dataand instructions. A special register, calledthe program counter points to the nextinstruction in memory to be executed. The computer fetches the next instruction from memory. The program counter moves to the next instruction. The computer thendecodes the instruction it just fetched.

Page 28: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Machine Language

We call the instructions stored in computer memory machine language instructions. They are defined by the chip manufacturer. For example, the machine instruction

00110011 00011010

might mean something like

take the byte stored in memory location 0024 and put it into register ‘A’.

Page 29: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Summary

Integers straight binary representation

Real Numbers split into sign, exponent and coefficient

Characters coded bytes – ASCII

Instructions coded words – machine language

Page 30: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Declaring a Variable

int someNumber;char firstLetter;bool theAnswer;

double density = 12.45;int hoursWorked = 14;char key = ‘g’;

this statement reservesspace in computer memoryfor an integer. We can then refer to the data in this location using the name “someNumber. ”

C++ does not initialize variables.

this statement reservesspace in computer memory for a character. The bit patternfor ‘g’ is then stored in thatlocation. We can now refer to thedata in this location using the name “key”

Page 31: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Declaring a Variable

int value1, value2, value3; //comma delimited list betterint value1;int value2;int value3;

This statement defines threevariables, all of which are ints.

Page 32: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Declaring a Variable

int value1= 12, value2= 4, value3= 21; betterint value1 = 12;int value2 = 4;int value3 = 21;

This statement defines threevariables, all of which are ints,and initializes them.

Page 33: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Declaring a Constant

const double PI = 3.1416;const int SCALE_VALUE = 14;

The keyword const means that this is a constant.You cannot change the value after it is declared.

We normally use all upper caseletters when writing the name of a constant.

Page 34: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Floating Point Constants

Consider the following declaration:

const float PI = 3.14159F;

the decimal signals that thisis a real number

the F signals that this literalvalue is a float data type andnot a double (the default).

Page 35: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Assignment

The easiest way to change the value of a variableis to use an assignment statement.

temperature = 68.4;

note that all statementsend with a semicolon.

the right hand side of the assignmentstatement may be a literal value, oran expression involving variables, literalvalues, and operators, or even functioncalls.

the expression on the rightside of the operator is evaluatedand the resulting value is storedin the storage location allocatedto the variable “temperature”

rvaluelvalue

Page 36: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Assignment Compatibility

In general, it is invalid to assign a variable of one typeto a variable of another. For example,

int a = 6.52;

The compiler will issue the warning

“conversion from 'double' to 'int', possible loss of data”

This means that the code will compile, but may not produce the results expected.

Page 37: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Assignment Compatibility

Note that you can do this assignment.

float a = 6;

The compiler will force a conversion to 6.0

Page 38: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

The compiler will allow you to do Widening Conversions

float a = 3;

because no information is lost.

The compiler will warn you if you do Narrowing Conversion

int pi = 3.14159;

because information will be lost.

Page 39: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Un-initialized Data

In C++, no initialization is done when a data element isdeclared. The value of that data element will be basedon whatever bits happen to be in that storage locationwhen your program starts execution. It will likely besomething (bad) left over from another running program!

So …… always initialize data when it is declared.

Page 40: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Initializing Data

int numOne = 5, numTwo = 4, numThree = 17;

int numOne (5), numTwo (4), numThree (17);

int numOne = 5;int numTwo = 4;int numThree = 17;

Page 41: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Literal Data

In the statement sum = a + 5;

the value 5 is what is called literal data.

It is good programming practice to use constantsinstead of literal data in your program.

const int MAX = 5;...sum = a + MAX;

Exceptions are 1, -1 and 0

Page 42: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

We use the term “Magic Numbers” to refer toliteral data that is written into an expressionin your program.

double avgTemperature = sumTemperature / 10;

This is a magic number

You do not want magic numbers in your programs.They make programs hard to maintain. You willLose points if I see magic numbers in your programs.

Page 43: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Objects and Classes

Object oriented languages give programmers theability to model real-world objects.

Page 44: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

for example, a car has attributes (data)* it is black* it has a 200 hp engine* it has 2 doors* it was built in 1943* etc

it also has behaviors (functions)* when you turn the key it starts* when you press the brake it stops* when you push the horn it beeps* etc

object-oriented languagesencapsulate the data and thefunctions that operate on thatdata into an object.

Page 45: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

size

color

getS

ize

( )

getColor( )

an object’s functionsmanage specific piecesof data inside the object.

External Function

functions outside ofthe object cannot see or manipulate the object’sdata, which is private.However, they can call public functions inside the object to accessthe data.

object

Page 46: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Classes

Later on, we will spend much more time talkingabout objects and classes. For now, just thinkof a class as a blueprint that the computer useswhen creating objects. When we write an objectoriented program, much of our time is devoted todesigning and writing classes. ostream (class) cout (object) istream (class) cin (object) string (class) stg (object)

Page 47: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Languages that primarily deal with objectsare called object-oriented languages.

Pure object oriented languages, like C# and Java,treat everything as an object.

C++ is sometimes called a hybrid language,because it has elements of a procedurallanguage and an object-oriented language.

Page 48: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

The C Language

The C++ Language

The C language is a pureprocedural language. It wasdeveloped at AT&T’s Bell Labsin the 1970s by Brian Kernihan and Dennis Ritchie. It was firstused for writing and maintainingthe Unix operating system.

Bjarne Stroustrup of Bell Labsdeveloped the C++ languagein the early 1980’s by addingobject oriented capabilities toC (C with classes).

Page 49: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Some Convenient Objects

C++ has built in to it some objects that will make ourprogramming tasks much easier. The first of these wewill introduce are cin and cout

Page 50: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cin

cin an object of the istream class (#include <iostream>).This object represents the standard input stream. The cin object is created automatically for you.

keyboard buffercin program

keyboard buffer

Page 51: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cout an object of the ostream class (#include <iostream>). This object represents the standard output stream. It is also created automatically for you.

output buffercout program

cout

display buffer

Page 52: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

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

The Stream Insertion Operator, <<

Page 53: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

display buffer

int a = 5; a 0000 0000 0000 0101

the integer 5

0000 0000 0011 0101

the character 5

cout

cout << a;

5

Page 54: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

multiple pieces of data are output bycascading the << operator …

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

Page 55: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

special characters are added to the streamusing the escape character \

\t //tab\n //newlineetc …

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

Page 56: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

the endl stream manipulator can be added to the outputstream. It does two things:

It adds a new line to the stream.

It forces the buffer to be output

cout << “Hello” << endl;

Page 57: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Formatting Numbers

When outputting numbers with fractional partswe have to take care to make the output appearjust as we would like it. This does not happenautomatically.

Page 58: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

To display a double or a float in standard decimal notation

cout.setf(ios::fixed);

setf( ) is a function in the cout object. It is responsible forsetting formatting flags. We will study these in much moredetail in a later section. In this case, we are setting theios::fixed flag. This makes the output appear as a normaldecimal number instead of in scientific notation.

cout.setf(ios::showpoint);

the ios::showpoint flag guarantees that a decimal pointwill be displayed in the output.

cout.precision(2);

the cout.precision( ) function determines how many digitswill be displayed after the decimal point.

The default formatting, is the“general” format. In this caseprecision defines the number ofdigits in total to be displayed

Page 59: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Example

double price = 78.5;cout.setf(ios::fixed);cout.setf(ios::showpoint);cout.precison(2);cout << “The price is $” << price << endl;

Page 60: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

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

The stream extraction operator, >>

Page 61: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

keyboard buffercin

int a;cin >> a;

a

0000 0000 0011 0101

the character 5

0000 0000 0000 0101

5 72 hello

reading stops when white space is encountered

Page 62: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

you can also cascade the stream extraction operator:

cin >> a >> b;

Page 63: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

You can control the size of an input field withthe setw( n ) stream manipulator.

cin >> setw(5) >> title;

Page 64: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cin.getThe get function of the istream class works similarto the stream extraction operator. With no parameter,it gets one character from the input stream.

cin.get( );

Page 65: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Reading a line into a string

This function is similar to cin.get( )except thatit reads an entire line of data, including spaces,and the data is stored in a string object. This isthe preferred way of reading in a line of data.

getline(cin, stringName);

Page 66: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cin.ignore( )

This function reads in a character and ignoresit. The character read in is discarded.

cin.ignore( );

Page 67: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cin.ignore( )

This version of the function reads in n charactersand ignores them.

cin.ignore(n);

This version of the function reads in n charactersor until it encounters the delimiter character,and ignores the characters read.

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

Page 68: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

cin.ignore( )

Why is this 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.

Page 69: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

if(cin.rdbuf()->in_avail()!=0) cin.ignore(80,'\n');

Insure that the keyboard buffer is cleared!

Page 70: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Style

When writing programs in any programminglanguage, it is helpful to use a consistentstyle. Good software development organizationswill often dictate that programmers use a specificstyle. This makes it easier for everyone to maintainthe code that is being developed.

In this course, you are expected to follow certainstyle guidelines. They are available on the courseweb site.

Page 71: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Style - IdentifiersUse names that have meaning. Avoid single character, very short, or very long names.

Examples: Meaningful Names Baffling Names amount aisFinished xl

ConstantsAll upper case with words separated by an underscore

Example: SIZE Classes Title case (capitalization of the first letter in each word)

Example: class ImpleCalc{…}

Function names and VariablesLower case for the first word and title case for every word thereafter.

Example: makeDeposit( )

Page 72: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Style - Braces

Even though the C++ language does not always require braces for some statements it is good programming practice to provide them. Use braces liberally to visually delimit the beginning and end of code blocks. Including braces now avoids the possibility of errors creeping into your code when you add additional statements at the last minute.

Place the opening (left) brace { so that it lines up with the left side of class headers, function headers, conditional statements, or repetitive statements. Place the closing (right) brace } in the same column as the opening brace. Always enter braces in opening/closing pairs to avoid forgetting to add one or the other or both. For braces that span more than three to five lines, comment the ending brace to indicate its nature (e.g., //End if ).

Page 73: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Indentation

As you moved from block to block, indentat least three spaces. Indentation makes codemuch more readable.

Page 74: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Example

void reviewCode( ){ if ( meetsGuidelines ) { cout << “Proceed to the next assignment”; } else { cout << “Rework your documentation”; } //End if/else} //End reviewCode( )

Page 75: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Your Own Code Declaration

Every source code file must contain the followingdeclaration. Code that does not contain thisdeclaration will not be graded!

"I declare that the following source code was written solely by me. I understand that copying any source code, in whole or in part, constitutes cheating, and that I will receive a zero on this project if I am found in violation of this policy.

Page 76: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Magic Numbers

A magic number is any numeric literal other than 1, 0, or –1 used in your program. However if 1, 0 and –1 are used to represent something other than the integers 1, 0, or –1 they will be considered magic numbers. Unfortunately, most code you will see in C++ books or programming books in general will include magic numbers because it’s easier to code in the short run. In the long run, six months from today, you will be clueless as to what the number means. Therefore, DON’T USE MAGIC NUMBERS in your assignments.

Page 77: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Where are variables stored?

If a variable is declared inside of the curly bracketsthat define a function, then that variable is said to be local to the function. It is stored on the stack segment.

If a variable is declared outside of the curly brackets,that define a function, then that variable is said to be global and is stored in the data segment.

The code is stored in the code segement.

Page 78: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

#include <iostream>using namespace std;

const float PI = 3.14149;

int main( ){ float radius;

cout << . . .

}

Declared inside ofcurly braces – stack seg

This is a local variable

Declared outside ofcurly braces – global (data seg)

Page 79: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

#include <iostream>using namespace std;

const float PI = 3.14149;

int main( ){ float radius;

cout << . . .

}

Declared outside ofcurly braces – data segment

This is a global variable

Page 80: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Name the basic C++ data types.

Page 81: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Here is some data stored in the memory of thecomputer.

0000 0000 0000 1001

What is it’s value?

Page 82: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Suppose that you needed a Student objectin a course registration program.

What attributes might a Student have?

Page 83: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

What kind of language is C++?

* Object Oriented * Procedural * Hybrid

Page 84: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

In which part of computer storage iseach of the following stored?

* A global variable * A local variable

Page 85: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Name two objects that we have learned about in thislesson. To what class do they belong?

Name the classes that these objects are instantiedfrom. What is string?

Page 86: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Which operator is used to convert data into itscharacter representation and output it to thestandard output device?

Page 87: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Which operator is used to convert data from itscharacter representation and store it to in memoryin its binary representation?

Page 88: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Write a program that prints the message

“Hello, my name is Hal”.

Then the program will prompt the user for his or her full name. It then Will print

“Hello, users full name, how are you?”

Page 89: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Write a program that prints the message

“Hello, my name is Hal”.

Then the program will prompt the user for his or her name.It then Will print

“Hello, user name, how are you?”

Prompt the user to type in their age. Then print

“user name, you are n years old”

Page 90: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Write a program that prints the message

“Hello, my name is Hal”.

Then the program will prompt the user for his or her age.

Then prompt the user to type in their full name. Then print

“ Hello user name, you are n years old”

Page 91: CS 1400. Primitive data types Variables and constants Declarations Assignment Input and output Classes and objects Style

Practice

Write a program that prints the message

“C++ Rocks!”.

Inside a rectangle. Use |, +, and – to draw the rectangle.Your output should look like

+-------------+| C++ Rocks |+-------------+