cs110 lec4 week2

Subdecks (2)

Cards (77)

  • The Basics
  • C++ syntax and semantics
    • Basic output
  • C++ program structure
    • Variables and Constants
    • Identifiers
    • Assignment/Declaration
    • Memory allocation
    • Basic overview of datatypes
  • Writing C++ Code
    1. You've seen a few examples of C++ code
    2. Now, we'll start to dig into what the code in the simplest examples actually mean
    3. Lots of specifics when writing code, lots of rules you must follow
  • Let's Begin with Output
    1. How to print something to the screen
    2. 'cout' is the "output buffer"
    3. The insertion operator << is used with cout
    4. We put anything we want to display on the screen to the right of the insertion operator
  • Example Output Code
    • cout << "Hello World";
    • Screen will display: Hello World
  • Output Statements
    Syntax: cout << Expression << Expression . . . ;
  • Output Statements
    • cout << "The answer is " ;
    • cout << 3 * 4 ;
    • cout << "The answer is " << 3 * 4 ;
  • What does this statement do?
    cout << "Hello" << "World";
    Answer: the output is HelloWorld
    ➢ Be aware of “whitespace”
  • Output manipulation
    • cout << endl; (ends the line)
    • cout << "This \"thing\" "; (\" produces a quotation mark)
  • Hello World
    • #include <iostream>
    using namespace std:
    void main()
    {
    cout << "Hello World!" << endl;
    }
  • Basic code boilerplate
    #include <iostream>
    using namespace std;
    void main()
    {
    // code goes here
    }
  • Boilerplate
    • Some things are needed for all programs
    • In most cases these things will be provided for you, on tests etc
    • We should have an idea of why they exist and why they are needed
  • Structure of a C++ program: Headers
    #include <iostream>
    This lets us use bits of code someone else wrote.
    iostream is called a header file
    iostream is part of the standard library that comes with C++
    We will be using two functions from iostream a lot:
    cin — reads input from the terminal
    cout — prints output to the terminal
  • Structure of a C++ program: Namespace
    using namespace std;
    This tells the compiler to look in the standard library for the functions we call
    If we didn't do this, every time we used a function from std, such as "cout", we would have to write it as "std::cout"
  • Structure of a C++ program: main function
    void main()
    {
    // code goes here
    }
    All C++ programs begin at the main function (which is a subroutine)
    { and } begin and end the code for the "main" function
  • Structure of a C++ program: Comments
    // code goes here
    The highlighted text is called a "comment"
    The "//" at the beginning of the line tells the compiler to ignore everything after it until there is a new line.
  • Extending the Simple C++ Program
    • #include <iostream>
    using namespace std;
    void main()
    {
    cout << "Programming is fun!" << endl;
    cout << "Fundamentals First" << endl;
    cout << "Problem Driven" << endl;
    }
  • Computing with Numbers
    • #include <iostream>
    using namespace std;
    void main()
    {
    cout << "(10.5 + 2 * 3) / (45 - 3.5) = ";
    cout << (10.5 + 2 * 3) / (45 - 3.5) << endl;
    }
  • Identifiers
    Identifiers are used to identify C++ data objects (variables, constants, and functions)
    Identifiers have to follow certain rules:
    • They may only include letters, digits, and underscores (_)
    • They must start with a letter or underscore — they cannot start with a digit
    • They cannot be the same as a reserved word
    • They can be any length, but some C++ compilers will place limits on their length. For compatibility, make sure identifiers are no more than 31 characters.
    Identifiers are case-sensitive.
  • Identifiers
    • VALID: age_of_dog, taxRateY2K, PrintHeading, ageOfHorse
    NOT VALID: age#, 2000TaxRate Age-Of-Cat
  • Choosing identifiers: 3 bad ways
  • More About Identifiers
    Some C++ compilers recognize only the first 32 characters of an identifier
    If so, these identifiers are the same: age_Of_This_Old_Rhinoceros_At_My_Zoo, age_Of_This_Old_Rhinoceros_At_My_Safari
    Consider these: Age_Of_This_Old_Rhinoceros_At_My_Zoo, age_Of_This_Old_Rhinoceros_At_My_Zoo
  • Identifier Style: Capitalization
    thisIsVariableStyle (start with lower-case letter, capitalize each word except first)
    ThisIsFunctionStyle (Start with upper-case letter, capitalize each word)
    NAMED_CONSTANT_STYLE (All capital letters, words separated by underscores)
  • Activity: for each identifier, is it…
    Legal?
    Descriptive?
    • char a; (legal, not descriptive)
    float 200taxRate (not legal, not descriptive)
    int age; (legal, descriptive)
    int student#; (not legal, not descriptive)
    int age_of_dog; (legal, descriptive)
    int Age-Of-Cat; (not legal, descriptive)
    float payRate2000; (legal, descriptive)
    string Name; (legal, descriptive)
    const int X=2; (legal, not descriptive)
  • Variables
    A variable is a location in memory which we can refer to by an identifier, and in which a data value that can be changed is stored.
    Declaring a variable means specifying both its name and its data type.
  • Variables vs. Identifiers
    An identifier is the name we give to a variable.
    A variable is the actual location in memory where the data is stored.
  • Declaration, Initialization, & Assignment
    Declaration: Telling the computer, "Hey computer, this thing exists. Please reserve some memory for it, and call it by this identifier."
    Assignment: When you assign a value to a variable you are putting a value into the variable.
    Initialization: When you assign a value to a variable for the first time.
  • Declaration, Initialization, & Assignment
    • int foo; // declaring an integer with identifier foo
    foo = 5; //assigning the value 5 to "foo"
    int foo = 5; // declares and initializes in a single statement
  • What Does a Variable Declaration Do?
    A variable declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location.
  • Data Types
    A Data Type is a specific set of data values along with a set of operations on those values.
    e.g. integer: values = positive and negative whole numbers (within a limit), operations = standard math operations
  • C++ Data Types
    • Integral Types (int, short, long)
    Floating Types (float, double, long double)
    Character Types (char)
  • Variables vs. Constants
    Variables: The value can be changed
    Constants: The value can't be changed, and must be initialized when declared
    const datatype IDENTIFIER = value;
  • Why use constants?
    • Ensures the value won't be changed by human error
    Compilers can optimize and speed up the program if data is stored in constants rather than variables