Welcome to Lesson 3!

Learning Objectives For Today's Lesson


By The End of this Class You Should Know...

  • How to create a new C++ file in Eclipse.
  • The basic building blocks of a C++ program, including:
    • Whitespace
    • Comments
    • Libraries
    • Namespace
    • The Main Function
    • Blocks
    • Proper Indentation
  • How to print to the console
  • How to use endl and \n properly as part of a cout statement
  • What pair programming is and the basic rules you should follow when pair programming.


Announcements

  • Quiz 1 on Thursday
    • Through Today's Lesson
    • To Prepare: Look over review activities, also in class activities and assignments
    • Answer 6 questions in 10 minutes
    • How would the presenter in "Things that Top Performing Students Know..." video recommend you study for the quiz?
  • Log into CodeLab given the access code for OUR class
    • Please do not give the access code to a student in another class
    • CodeLab is where you will be completing the rest of your Friday labs
  • If you are on the waitlist, please come see me during today's break to sign in.
  • Women in Computer Science Club meeting:
    • This Wednesday! 12:30pm to 1:30pm in ATC 202
    • Amazon speaker!
    • All are welcome

Review Activity

With a partner, answer the following questions:


What is the Decimal Equivalent of the Computer Science Class He is Entering?


More Review
  • With a partner, answer the following questions:
    1. What are the five main components of a computer?
    2. What is the difference between hardware and software?
    3. What is an algorithm?
    4. Why does a computer only understand 1s and 0s?
    5. What is a compiler?


Introduction to C++ - A Language That Humans Can Understand

  • C++ is one of the most popular languages in use today. Please see the Tiobe Community Programming Index for its current ranking.
  • C developed by Dennis Ritchie at AT&T Bell Labs in the 1970s.
  • C++ developed by Bjarne Stroustrup B-yar-ne Strov-stroop at AT&T Bell Labs in the 1980s.
    • 1979 - Stroustrup begins work on "C with Classes"
    • 1982 - Name changed to C++
    • 1983 - First used by AT&T
    • 1985 - First commercial use
  • Let's try writing our first program together! (Done using Eclipse)
    • Open Up Eclipse
    • Follow my instructions and watch what I am doing up front to create our first C++ program.

Basic C++ Skills

Setting Up Your Environment

  • Let's take a moment to create a new folder in your personal documents folder where you can save all of your in-class coding exercises. Name it CIS22A (with no spaces).
  • Next, watch up front or follow the tutorial to create a new C++ file.
  • Note the tutorials that can help you get Eclipse installed on your laptop.
  • Let's build a program step by step and see what happens.

Building Blocks of a C++ Program

Comments:

  • Comments are ignored by the compiler. Instead, they are notes to anyone reading the code. 
  • Comments are used to make your program more understandable to someone who is looking at it.
  • There are two types of comments in C++:
    • Multi-line comments - comments that are more than one line long.
      • Multi-line comments must be placed between /* */ as in lines 1-6 above.
      • Another example: 
/**
This is a multi-
line comment
*/
  • Inline comments - comments that are only 1 line long.
    • Inline comments follow a double forward slash //
    • There is an inline comment on line 12 of the program above. Do you see it?
    • Another example:

//This is an inline comment. It cannot be more than one line long.

  • Block Comments are multiline comments that begin your program. It is good style to always include a comment at the start of every program with your name and some additional information.
    • For example:
/**
* Jennifer Parrish
* CIS 22A
*/
  • Note that Eclipse will gray out any comments in your code. You know that a line of code is a comment if it is green. 
  • Put a comment at the top of your program, like the one above. Notice how the words become green.

Whitespace

  • Whitespace is ignored by the compiler.
  • Whitespace includes extra lines, extra spaces and extra tabs in your program.
  • It is important to use a lot of white space to make your code readable.
  • Add a couple of lines of white space to your program by pressing the enter key twice.


Libraries:

  • In programming terms, a library is a collection of prewritten code we can use in your programs
  • This saves us the effort of writing our own code for commonly-used functions
  • C++ has a number of standard libraries
  • These libraries place their code in what is called the std namespace
  • To use a library, we say #include<nameoflibraryhere>
  • #include <iostream>
  • The iostream library needs to be included every time we want to do input or output (most programs!).
  • It allows us to use the word cout for sending data to a terminal screen
  • Other libraries exist for math, strings and more
  • Note that some compilers are picky about spaces in include directives
    • Do not put spaces before or after the # sign
    • Do not put spaces inside the angle brackets
  • Add the iostream library to your program as in the example above.

Namespace:

  • Namespace: a set of name definitions where all the names are unique
  • As part of programming, we assign names to things
    • We will discuss these "things" in more detail as the course progresses
  • We can only use a name once within a namespace
  • All standard libraries put their definition in the namespace: std
  • To use these standard names, we need the directive:
  • using namespace std;
  • Thus, most of our programs will begin with two statements:
    #include <iostream>
    using namespace std;
  • Add the standard namespace to your program as in the code above. 


The Main Function

  • C++ programs are structured into subprograms called functions.
  • Function = a named block of code that executes a series of statements.
  • Every C++ program has one or more functions, including a main function defined like this:
    int main() {
        // program statements go here
return 0;
}
  • Programs begin executing at the first line of the main() function.
  • The int means main returns a value of type int -- cover later.
  • For now, simply mimic the first line of main as shown above. Add the main function to your program, a line or two below the namespace directive.


Blocks

  • A block is a section of code grouped together
  • C++ is known as a block-structured language
  • This means that most source code is grouped within pairs of matching { }
  • Left brace { begins body of every function
  • Right brace } ends body of every function
  • All functions have associated blocks
  • However, as we will see later in the course, we can use blocks other places as well
  • Make Sure Your Curly Brace Has a Matching Curly Braces { }.


Programming Style: Indentation Inside Braces

  • We should always indent our statements inside braces
  • This makes the structure of our code easier to follow as our programs grow more complex
  • When we get to the end of a function, we remove the level of indentation
  • Verify that your statements are indented within the curly braces.

Your Program Should Now Look Something Like This:

/**
* Jennifer Parrish
* CIS 22A
*/

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!";
    return 0;
}


  • Now Run the Program and Verify that You Get No Error Messages. Press the Build and Run Button. 

Your First Words in the C++ Language

  • cout means console out - the command to tell the computer to print a message to the screen.
  • The message to be printed goes between double quotation marks.
  • The arrows point away from the message, indicating that it is output.
  • Each complete cout statement is followed by a semi-colon.
cout << "It's alive!\n";
  • Add a cout statement to your program with any message that you want.
  • Press the Build and Run Button again.
  • Verify that your message appears on the console screen.
  • Now close the console screen.

Remember: Programming is for detail-oriented people!


/**
* Jennifer Parrish
* CIS 22A
*/

#include <iostream>
using namespace std;

int main() {
    cout << "It's alive!" << endl;
    return 0;
}


Group Exercise: Let's Experiment!

  • Try adding whitespace (blank lines, extra spaces, extra tabs) in various places in your program. Each time you change the program, press the build and run button. Where does whitespace matter and where doesn't it matter?
  • Return your program to its original format.
  • Try commenting out your cout statement by placing // in front of it. What happens? What happens when you press the build and run button?
  • Now, remove the comment and press build and run again to ensure your program is working correctly.
  • Take off the semi-colon ;  from the end of your cout statement.
  • Press build and run. What happens? Note the message the compiler gives to you.
  • Replace the missing semi-colon ; and verify your program is working again.
  • Now add a second cout statement.
  • Press the run and build button.
  • What happens? Is that the effect that you wanted.

Endl and \n

  • There are two ways to signal the end of a line in C++
  • \n goes inside the quotation marks of the message.
  • The n stands for newline.
  • Pronounced "Back slash n."
  • cout << "message goes here\n";
cout << "Greetings, earthlings!\n";
cout << "Hi back, alien life forms.";
  • You can also signal the end of a line using the command endl.
  • endl stands for end of line
  • cout << "message goes here" << endl;
cout << "Greetings, earthlings!" << endl;
cout << "Hi back, alien life forms.";

  • Add a second cout statement to your program, using either \n or endl.
  • Press the build and run button to verify you got the result that you wanted.


Backslash vs Forward Slash - How to Remember the Difference:


backslash forward slash stick figures leaning backwards and forwards.

Pair Programming

What is Pair Programming?

  • Pair programming is a style of programming in which two people work together on one computer at the same time:
    • Exactly two people: not one nor three or more
    • Exactly one computer: not two or more
  • One person types the code and the other reviews each line of code as it is entered
  • The person using the mouse and keyboard is called the driver
  • The person reviewing is called the navigator
  • In addition to reviewing, the navigator:
    • Analyzes the design and code to prevent errors
    • Looks up reference materials like program syntax
  • Each person "drives" about half the time:
    • Physically get up and move positions when switching roles
  • At most 25% of your time is spent working alone:
    • Any work done alone is reviewed by the other person
  • The objective is to work together and to learn from each other
  • You cannot divide the work into two pieces with each partner working on a separate piece
  • If you are not both engaged in the process, you will not learn the material

Today, we will begin practicing our pair programming skills during our in-class exercises.

Why Pair Program?

  • Students who pair program report:
    • Higher confidence in a program solution
    • More satisfaction with programming
  • Instructors report higher completion and passing rates

Video Explaining Pair Programming

More Information


Activity 3.1Your C++ Screenplay (10pts)

  • This exercise will be your first chance to try out pair programming! 
  • Find a partner. Remember that you can work with no more than one other person.
  • Open up a new C++ project in Eclipse called screenplay. Save it in your 22A folder.
  • In the file screenplay.cpp, write your names at the very top of the file by placing them inside of a multi-line comment, along with your section information.
  • The objective of your program is to create a dialogue between two different characters.
  • Select your genre of choice for your screen play. For example:
    • Western
    • Romance
    • Science Fiction
  • Give names to your two characters.
  • Write a minimum of 6 lines of dialogue between the two characters, following the conventions of your selected genre. 
  • Note: You should have 6 separate cout statements and 6 endl or \n .
  • Write each line of dialogue, by typing the character's name followed by a colon.
  • The dialogue should appear on the console when you press the run and build button.
  • Remember that each person should drive 50% of the time.
  • When finished, upload screenplay.cpp to Canvas. 
  • Each person needs to upload the file.
  • The output of your program should look similar to the one below (but with 6 lines, rather than 2).

            Martha: Fred, I need to confess something to you. I have been cheating on you with your evil twin, Ted.

       Fred: Actually, Ted isn't the evil one, Martha. I am! Bwahahaha!

  • Please avoid using any foul language, sex, excessive violence or other offensive material in your screen play, or you will receive no credit for this activity.

CodeLab

  • Log onto CodeLab and work through the first few exercises to get a start on Lab 2.
  • Make sure you know how to logon and how to work through the first few exercises.

Review: 

  • With a Partner, answer the questions from our learning objectives.

Upcoming Assignments:

  • Study for Quiz 1 on Thursday
  • Assignment 3 due on Thursday
  • Lab 2 due Friday at midnight

~ See You Thursday! ~