New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Chapter 2: Intro to C++

by: LadyLJ

Chapter 2: Intro to C++ CS 1336


Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

2.1 Parts of a C++ Program 2.2 cout 2.3 #include 2.4 Variables and Literals 2.5 Identifiers 2.6 Integer Data Types 2.7 char 2.8 string 2.9 Floating-point Data Types 2.10 bool 2.11 Determi...
Programming Fundamentals
Charles O. Shield
Class Notes
programming, cs1336, c++, Computer, software engineering
25 ?




Popular in Programming Fundamentals

Popular in Department

This 16 page Class Notes was uploaded by LadyLJ on Monday October 3, 2016. The Class Notes belongs to CS 1336 at University of Texas at Dallas taught by Charles O. Shield in Fall 2016. Since its upload, it has received 10 views.


Reviews for Chapter 2: Intro to C++


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/03/16
Chapter 2: Introduction to C++ 2.1 Parts of the C++ Program // sample c++ program Comment #include <iostream> Preprocessor Directive using namespace std; the namespace in use int main() beginning of the function main { beginning the block for main cout << “Hello world!”; output statement/string literal return 0; sends 0 to the operator/signals end to the function } end of block for main *main() can be thought of as a header and the included block of code { } can be the body. If you think of it like this it will help later when we have functions inside of functions. Function- a named subroutine, a collection of one or more programming statements that can be referred to (and executed) by name. **EVERY C++ program must have a main() function **EVERY function must have an opening brace ‘ { ‘ AND a closing brace ‘ } ’ Special Characters Cha The English What it does r Name // Double fwd Starts a comment Slash # Pound Sign Starts a preprocessor directive < > Open/Close Encloses a filename in an #include statement Brackets ( ) Open/Close Used after naming a function; sometimes will Parentheses contain (void) or an equation (x=y) or it can be blank () – depends on the function { } Open/Close Encloses the Body of a function Brace “ “ Open/Close Carries the string literals which will be printed out to Quotation the user when you run the program Marks ; Semicolon Ends the program statement; anything after, and on the same line, will cause an error message in your code 2.2 The cout Object cout << “txt”; Whatever is in the parentheses after the cout statement is what the user sees. This is very important as it allows you to communicate to the user. cout is classified as a STREAM OBJECT, this means it works with streams of data. In the case above the user would just see txt . they would not see the quotes. It is possible to more than one item at the same time: cout << “We will” << “ Rock you!”; or cout << “We will”; cout << “ Rock you!”; produce the same thing: We will Rock you! If you separate the statement you must include the space in between the last word of the first quotes and the first word of the next quotes, if you don’t your sentence will just run together. Stream Manipulators and Escape Sequences change the cout statement to do a certain action. We will focus on two for now. endl \n cout << “txt” << endl; cout <”txt \n”; Both statements do the same thing, end the line. when you put these at the end of your statements it like pressing ‘enter’ in between each paragraph. In this example the output is: txt txt Going In Depth: endl  Do not put quotations around it or it will just be included in the output string. The last letter is a lowercase L NOT a 1. The endl is an example of a Stream manipulator \n  usually included in the quotation marks. \n is an example of an Escape Sequence. Other Escape Sequences Ch Name Summary of What it Does ar \n Newline Moves the cursor to the next line; like pressing ‘enter’ \t Horizontal Moves the cursor over to the next tab spot; like indenting a Tab paper \a Alarm Causes an audible beep \b Backspace Causes the cursor the move one space to the left; like you left arrow over \r Return Causes the cursor the move to the beginning of the current line; like ‘home’ \\ Backslash Prints a backslash \ \’ Single Prints a single quote ‘ Quote \” Double Prints a double quote “ Quotes The bottom three characters must be printed using the escape sequences or they will be read as part of the code instead of being displayed. 2.3 #include this preprocessor inserts the contents of another file into the program. Some Examples are: #include <iostream> This one will be used always. #include <iomanip> #include <string> *do not place a semicolon at the end of this statement. 2.4 Variables and Literals Variables – a named storage location in memory. It has a name and a data type that can be held. Before any variable can be used it must be declared. <data type> <variable name>; int num3; Literals – a value that is written directly into the code; essentially it is a variable that has been defined. <variable name> = <number>; num3 = 20 *more on data types in 2.6 & 2.9 2.5 Identifiers Identifier – a programmer-defined name for some part of the program: variables, functions etc. *Key words cannot be used as identifiers Variable names need to follow a set on rules just like anything else.  need to be purposeful and mean something to you, don’t use x and y  two basic formats: o icecreamFlavor first word is lowercase and the second word is capital o icecream_flavor both words are lowercase and separated by an underscore  The first character must be an alphabetic character or and underscore; it cannot be a number  After the first character you can use any alpha-numeric combination along with underscores, but nothing else may be used.  Variable names are case sensitive. Variables can be defined on the separate lines: int length; int width; double area; on the same line int length, width; double area; *variables of different types must be defined on different lines. 2.6 Integer Data Types data Type Byte Range Size short int 2 bytes -32,768 to +32,767 unsigned short int 2 bytes 0 to +65,535 int 4 bytes -2,147,483,648 to +2,147,483,648 unsigned int 4 bytes 0 to 4,294,967,295 long int 4 bytes -2,147,483,648 to +2,147,483,648 unsigned long int 4 bytes 0 to 4,294,967,295 long long int 8 bytes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 unsigned long 8 bytes 0 to 18,446,744,073,709,551,615 long int *int works for most values. signed v. unsigned All integers are signed by default. n-1 n-1 Signed integers range from -2 to +2 -1 where n is the number of bits in the number. Unsigned integers range from 0 to 2 -1 where n is the number of bits in the number. **if you get confused between which value holds the negative just think “Unsigned means you don’t see a sign in front” so in only holds positive numbers (and 0); “Signed means you DO see a sign, a negative sign!” 2.7 Char char <variable name> = ‘character’ holds one value of any character on the keyboard; single letters and 0-9 along with punctuation  must be surrounded by single quotes The computer assigns these char variables as numbers. These numbers come from ASCII tables; I cannot include one here because of copyright laws, but I encourage you to look some up and possibly print them out to get a better idea of the concept. String literals are stored as a series of char values and terminated by a NULL ‘\0’ “Hello” Is stored as the follows: H e l l o \0 Note: The quotation marks are not stored and the NULL \0 is added automatically. Char values are stored slightly differently: ‘A’ Is stored as the follows: A 2.8 string Special data type that you must include at the beginning of your code: #include <string> A string is basically a variable for a phrase. if you want it to be easily changed or accessed you would use this instead of typing every time you need is, especially if it is a long phrase. It is talked about super in depth in the book but I feel you only need the basic gist of this topic. Ex. #include <iostream> #include <stream> using namespace std; int main() { string favTV; favTV = CSI: Miami; cout << “My favorite show is “ << favTV << “!” << endl; return 0; } 2.9 Floating Point Data Types In order of largest to smallest long double double float Integers v. Floating Points Integers do not have decimal places while Floating points do. If you assign a decimal value to the int data type, the decimal disappears and is unusable. Most compilers will give you an error and tell you to change the data type. Why Significant Figures matter float only carry 6 sig figs and everything after those 6 figures is garbage (as in inaccurate). double carries 14 sig figs; so it will hold larger values more accurately. Holding doubles as floats or long doubles double is the default storage but you can change that by adding an F or L to force the number to be saved as floats and long doubles, respectively. 2.10 bool Condition – an expression that evaluates to either true or false bool holds these values by true = 1 and false = 0 but all non-zeros are taken to be true 2.10.2 Pointers int intPtr = 3000 int x=5; int * intPtr; intPtr = &x  This give the location of x This code multiplies x and intPtr together then gives the location of x as a stored integer in the system. 2.11 Determining the Size of a Data Type sizeof() asks the system for the byte size of a data type or variable the displays the result. cout << “Size of an int: “ << sizeof(int);  Returns 4 in most systems 2.12 Variable Assignments and Initialization <variable> = <value> -or- <variable> = <equation> fish = 2; -or- fish = 2+2; The operand MUST be on the LEFT; it must be capable of receiving a value. This is called the lvalue. logic dictates that then whatever remains on the right side of the equals sign is then the rvalue. The following will NOT work: 2 = fish; -or- 2+2 = fish; Initializing – To give a variable a value when it is first declared int fish = 2; Defining – when you declare and initialize at the same time Or it can happen in two steps: int fish;  declaration fish = 2;  initializing Defining by auto auto – detects what the variable needs to be; you HAVE to define the value in the same line. auto fish = 2;  int auto water = 16.3 double you can do this for any variable. 2.13 Scope Scope – The part of the program in which the variable can be accessed A variable define in an inner scope cannot be used in an outer scope int main() { cout << num; num=1.3; } This will give you an error because you must define the variable before you use it. 2.14 Arithmetic Operators Three types of operators: Unary: uses 1 operand; 6 Binary: uses 2 operands; -7 + 5 Ternary: uses 3 operands; 3 * 4 – 6 Code follows the order of operations (PEMDAS) Parentheses Exponents Multiplacation/Division/and now Modulus goes left to right if they share an operand Addition/Subtraction goes left to right if they share an operand Integer Division division (/) uses integer division if both operands are integers: 3/2 = 1 you can change this by making one not an integer: 3/2.0 = 1.5 Modulus Operator Completes integer division and gives you the remainder: 9 / 4 = 2 9 % 4 = 1 requires BOTH terms to be integers: 9 / 4.0 = error 2.15 Comments ** Explained fairly well in Chapter 1 notes; Summarized here.  Used as part of the code to better understand it when you come back later  make sure comments each variable when you declare it to further explain what that means o double avgTemp = #### //The calculated average temperature  also make sure to comment closing braces (}) I usually just say //end <function>  comment the top of blocks of calculations and things to summarize and/or title them Multi-line Comments /* this comment is a multi-line comment */ 2.16 Named Constants Constants – values that cannot be changed during the execution of a program const double GRAVITY_RATE = 9.8; Specific naming convention: Use all caps when typing constant variables and underscores to have multi-word variables. Constants must be defined when they are declared. After this point they cannot be changed. #define <identifier> <replacement text> This define a variable that was used earlier in a calculation. //before #define area = radius * PI; #define PI 3.14159 //after #define area = radius * 3.14159; *IMPORTANT: This is a string replacement; meaning no variable or memory space was created when we use #define 2.17 Programming Style  The visual organization of your program o everyone is a little different and that’s okay!  This includes the use of spaces/tabs/blank lines  Important to keep your code clean looking o It effects the readability of you code for graders and also other coders so they improve your project later How to improve the readability of your code:  Align your braces vertically { }  Use a set indentation inside each set of brackets (our class is 3)  Use blank lines to separate blocks of code and better organize yourself  Use multiple line for long statements instead of one big long line  Make sure to use your comments like I specified earlier **Combining all these tactics and the comments will have your code looking really organized. It might be annoying at first, but it will become natural to you after a while and make a better coder. Ifyou have to come back to fix formatting issues then you will know where to look rather than looking through hundreds of line of code to find one off value.


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Amaris Trozzo George Washington University

"I made $350 in just two days after posting my first study guide."

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.