23
CMSC 202 Lesson 2 C++ Primer

CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Embed Size (px)

Citation preview

Page 1: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

CMSC 202

Lesson 2

C++ Primer

Page 2: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Warmup

Create an array called ‘data’ Define a constant called DATA_SIZE with

value 127 Write a loop to fill the array with integers

from the user

Page 3: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Intro to C++

Minor differences from C Major additions to C Today

Differences Semester

Additions

Page 4: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Output

cout Output stream Print to screen

<< Output stream separator Between every object in stream

endl Output stream end of line character

Example:cout << "Hello World!" << endl;

Page 5: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Input

cin Input stream Standard input (keyboard)

>> Input stream separator Between every object in stream

Exampleint age;cout << "What is your age?" << endl;cin >> age;

Page 6: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

In Action!#include <iostream> using namespace std; int main( ) {

// greet the user cout << "Hello reader.\n" << "Welcome to C++.\n";

// prompt user and get response int numberOfLanguages; cout << "How many programming languages have you used? "; cin >> numberOfLanguages;

// determine appropriate response if (numberOfLanguages < 1 )

cout << "Read the preface. You may prefer" << endl << "a more elementary book by the same author" << endl;

else cout << "Enjoy the book." << endl;

return 0; }

Page 7: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Formatting Decimals

Magic code cout.setf( ios::fixed ); cout.setf( ios::showpoint ); cout.precision( 2 );

Fix & show the decimal with 2 digits of precision Collection of flags, set until you UN-set them!

Example:double temperature = 81.7;cout.setf( ios::fixed );cout.setf( ios::showpoint );cout.precision( 2 );cout << temperature << endl; // 81.70

Page 8: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Casting

Old C-style castingdouble DegreesCelsius( int degreesFahernheit ) {

return (double)5 / 9 * ( degreesFahrenheit - 32 ); }

New C++-style castingdouble DegreesCelsius( int degreesFahernheit ) {

return static_cast<double>( 5 ) / 9 * ( degreesFahrenheit - 32 );

} Why?

Compiler makes a check – so it’s safer

Page 9: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Constants

Old C-style #defines#define PI 3.14159;

New C++-style constant variablesconst double PI = 3.14159;

Why?Type-checking!

Page 10: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Enumerations

enum Allows you to define new, limited types Maps to integers (so comparisons are allowed)

Syntax:enum TYPE_NAME { VAL1, VAL2, …, VALN };

Example:enum DAYS { SUNDAY, MONDAY, TUESDAY,

WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

DAYS today = TUESDAY;

Page 11: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Practice!

Work with your neighbor Rewrite the warmup to use the following C++

constructs: cout cin const

Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with integers from the user

Page 12: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Review: C-style Strings

Declare a new string:char string1[4] = "abc";

char string2[ ] = "abc";

char *stringp = "abc";

// What about this?

char string3[ ] = {'a', 'b', 'c'};

What’s the difference?

Page 13: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Review: C-style Strings

Library Functionsstrcpy -- copy a C-style string strcat -- concatenate two C-style strings strlen -- return the length of a C-style string strcmp -- compare two C-style strings

Page 14: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

C++-style Strings

Library header file#include <string>

Create an empty stringstring stringName;

Create a string with initial valuestring carMake( “Nissan” );string carModel = “Pulsar”;

Create a copy of a stringstring newCarMake( carMake );

Page 15: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

C++-style Strings

Copy value into existing stringfirstString = secondString;

Access individual character (like arrays!)firstString[7] = ‘x’;

Compare two strings (==, <, >, !=, …)if (firstString == “ham”)

Concatenationstring name =

firstName + “ “ + lastName;

Page 16: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

C++-style Strings

Length of stringif (fileName.size() > 8)if (fileName.length() > 8)

Is string empty?if (fileName.empty())

Treat C++-string as C-string (HINT: important for file streams!)string s1 = "bob"; char s2[] = "This is a C-style string";

// this code copies s1 onto s2 using strcpy() strcpy (s2, s1.c_str());

Page 17: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

C++-style Strings

Substring Syntax:

stringName.substr( index, length); Example

string name = “Your Name”;

string your = name.substr(0, 4);

cout << your << endl; // “Your”

Page 18: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Input/Output with C++ Strings

OutputSame as with other variables…cout << stringName << endl;

Examplestring firstName = “Dana”;string lastName = “Wortman”;cout << firstName << “ “

<< lastName;

Page 19: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Input/Output with C++ Strings

Input Same as with other variables One issue – strings are broken by whitespace

cin >> stringName; Example:

string name;cout << “Please enter your name” << endl;

// User types in: Dana Wortmancin >> name;cout << name << endl; // “Dana”

// “Wortman” is waiting

// to be read…

Page 20: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Input/Output with C++ Strings

Input – Whole lines! Syntax:

getline(inputStream, stringName); Getline reads until an end-of-line character is seen (return!)

Example:string name;cout << “Please enter your name” << endl;

// User types in: Dana Wortmangetline(cin, name);cout << name << endl; // “Dana Wortman”

Page 21: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Strings In Action!string Reverse (string s) {

string result(s); // copy of original string int start = 0; // left-most character int end = s.length(); // right-most character

// swap chars until end and start // meet in the middle while (start < end) {

--end; char c = result[end]; result[end] = result[start]; result[start] = c; ++start;

} return result;

}

R e v e r s e !

! e s r e v e R

Page 22: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Practice!

Prompt for 3 words from the user Read in 3 separate words Concatenate them together into 1 string in

REVERSE ORDER Print that string

Page 23: CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with

Challenge!

Part 1: Define a constant representing the minimum “Gateway” GPA (3.0) Request the user’s GPA for CMSC 201 Print it using 3 decimal points of precision

Part 2: Define a constant representing the computer science major code (CMSC) Request the user’s 4-letter Major code Print it

Part 3 (use your constants!): If the user is a CMSC major and their GPA is above the cuttof

Print a congratulatory message If they are a CMSC major and their GPA is too low

Tell them to retake 201 If they are any other major

Tell them to change majors to CMSC – because it’s the best!