CSC 1060 Week 03 Data Types and Variables PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
This document covers the basics of data types and variables in C++. It includes explanations of fundamental data types, variable declaration, and various string operations.
Full Transcript
CSC 1060 DATA TYPES AND VARIABLES OBJECTIVES AGENDA: WEEK 03 Understand and differentiate 1. Data Types & Variables between fundamental data 2. Declaration of a Variable types in C++ 3. Naming Convention for Variables...
CSC 1060 DATA TYPES AND VARIABLES OBJECTIVES AGENDA: WEEK 03 Understand and differentiate 1. Data Types & Variables between fundamental data 2. Declaration of a Variable types in C++ 3. Naming Convention for Variables Declare and initialize variables 4. Coding Standards for Variables Explain the concept and 5. Operators importance of data types 6. Assignment Correctly declare variables in 7. Solve problem with Code C++ 8. Type conversion Apply appropriate naming 9. and std::string conventions 10. String common operators and functions -Understand the importance of 11.TODO & Resources for Help naming conventions WHAT ARE DATA TYPES AND VARIABLES Watch the video and although it brings up Java data types, the way variables are declared as noted in the video is the same, but in C++ we have the following fundamental data types: int float double char bool String is not a fundamental type but is part of C++ since C++ was standardized and is the data type: std::string #include C++ DECLARATION OF A VARIABLE (JULIES' AUDIO) 1. Data type indicates how much storage and what operations can be performed using the variable 2. Name, identifies the variable in the program and signifies it's meaning – starts with lowercase letter! 3. Value, assigned into memory dataType name = value; CIRCLE C++ VARIABLES TO C++ RULES & OUR STANDARDS first-place ranked1st 1stPlace FirstPlace winner firstPlace first_place place COMPLETE: C++ DECLARING VARIABLES _____ _______ = 0; // number of letters in word _____ _______ = 0; // first name initial _____ _______ = 0; // balance in bank account _____ _______ = 0; // overall grade percent ASSIGNMENT STATEMENTS (CPLUSPLUS) leftVariable = rightValue; Assigning a value to be stored in the variable The data type of the left variable and right value SHOULD match to ensure type safety. The left variable MUST be declared before it can be used and must be a mutable (CANNOT be a constant) The right value can be an arithmetic expression, formula, or another variable of same type typically. OPERATORS (CPLUSPLUS) Arithmetic PEMMDAS: () * % / + - Order of Operations: Please Excuse My Most Dear Aunt Sally NO (E)xponent operator in C++ % Modulus: remainder from division (left over) / Floating Point Division 5/2.0 or 5.0/2 or 5.0/2.0 / Integer division 5/2 SOLVE PROBLEM #1 WITH CODE A single ticket to the fair costs $8. A family pass costs $25 more than half of that. How much does a family pass cost? TYPE CONVERSION (CPLUSPLUS) Implicit Type Conversion Automatically performed when a value is copied to a compatible type int float double Explicit Type Casting Many conversions, specifically those that imply a different interpretation of the value and/or lose memory space, require an explicit conversion, known in C++ as type-casting. double float int C++ STD::STRING (JULIE'S AUDIO) Strings are a sequence of characters and are standardized in C++ NO C-style strings: char[] or char* are allowed in our class! A std::string's character sequence is stored in contiguous memory is referenced through each character's index. One byte of memory per character std::string school = "Red Rocks"; Index (0-based) 0 1 2 3 4 5 6 7 8 'R' 'e' 'd' '' 'R' 'o' 'c' 'k' 's' What type of characters can be stored in a string? C++ STD::STRING OBJECT (VARIABLE) Strings are objects that represent sequences of characters. Include library for std::string #include Create a string object that is an empty string "" std::string name; Create a string object that is initialized to a string literal std::string firstName = "Julie"; // OR std::string firstName("Julie"); COMMON STD::STRING OPERATIONS Given the following declaration: std::string school; Assignment Operator = school = "Red Rocks"; Concatenation Operator + (adding strings together) school = school + " Community College"; Subscript Operator [index] (getter and setter) char letter = school; COMMON STD::STRING MEMBER FUNCTIONS Given the following declaration: std::string school = "Red Rocks"; Number of characters: size() or length() int numChars = school.length(); // 9 Character at a particular index: at(index) - similar to subscript[index] char letter = school.at(1); // 'e' Search within string: find(char) int foundIndex = school.find('d'); // 2 Generate a substring: substr(pos, len) std::string color = school.substr(0,3); // "Red" COMMON STD::STRING FUNCTIONS Type casting is NOT allowed to convert between std::string and any of the simple data types either implicitly or explicitly! Functions are provided in library to perform these tasks. Convert int, float, double to std::string: std::to_string(var) std::string str = std::to_string(10); // 10 becomes "10" Convert std::string to int, float, double: std::stoi(str), std::stof(str), std::stod(str) int num = std::stoi("10"); // "10" becomes 10 SOLVE PROBLEM #2 WITH CODE Given Red Rocks, reverse the name to Rocks Red REVIEW EXERCISES Complete the 2.12 Multiple Choice Exercises, Questions 1 through 10. EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 2 Content Module in D2L to 100% WHAT'S COMING UP NEXT... Week 3 Pre-Work STARTS FRIDAY 1/26 8AM! QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment (both on-campus and remote via Zoom) o Drop-In Times Available (on-campus) Email: [email protected] On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help