Show Menu
Cheatography

C++ CS 1410 Exam 1 Cheat Sheet by

Prepping for exam 1 of college CS 1410 for C++ programming

Termin­ology

Cin
is the same as input
Cout
is same as print
<< and >>
point to where the inform­ation is going to.
difference between the ++ being before the variable or after
++x adds 1 to the x variable before doing the math statement, x++ adds 1 after the math statement.
setpre­cis­ion(3)
cout << setpre­cis­ion(3) << 1.2345­6<< endl; 3 total numbers, 2 after the decimal =1.23
setpre­cision command with fixed notation
cout << fixed << setpre­cis­ion(3) << 1.23456 << endl; 4 total numbers, 3 after the decimal = 1.235
Math Libraries
ceiling goes up to next int and floor goes to the int below ceil(3.5) = 4 floor(­3.5)=3 Abs(-3.5) = 3.5
setw
sets the field witdth to be used on the display
function
a way to modularize code and make it reusable. They help us manage, reuse, and organize our code. must include return type, name, parameters and {}. int AddFiv­e(int a) {return x+5}}
parameter passing by value
aka pass by copy, modifies a copy of the variable so that original stays the same. int addfiv­e(int a) { return a = a +5}; int b = 5; addfiv­e(b); shows 5
parameter passing by reference
modifies the actual variable being passed in. void addfiv­e(i­nt& a) { a= a+5;} int b = 5; addfiv­e(b); shows 10.

.h/.cpp

.h aka header file
allows us to link our program to library code #include "­mai­n.h­"
.cpp
source code files - methods for the defini­tions listed in .h file are here
classes
Student class {} within .h file, have member variable and member functions. default visibility of private
class scope
private: only available within that function, public: available throughout the code and protected
Constr­uctors
a member function of a class that is executed whenever we create new objects of that class, it is named after the class and gives our member variables values. student();
default constr­uctor
creates an empty object, numeric values to 0, character and string values to blank "­", and sets objects to empty. can be written in line or initia­lized list. Student() : id_(0), name_(­"­"), email_­(""){}
General Constr­uctor
creates an object with user chosen values given to the member variables. can be in line or a list but not both. Studen­t(int id, std::s­tring name, std::s­tring email) : id_(id), name_(­nam­e),­ema­il_­(email) {}
Setters
gets details from contru­ctors either general or default and add them to the variables list. void SetId(int id);
Getters
a way for the user of the class to see the values without having direct access to them. int GetId() const {return id_;}
structs
similar to class but can group several related variables in one place.
 

Loops

If statement
executes when the condition is true. double up on ifs and code executes for all true statem­ents. if(force <10){}
else if
executes the first true statement. then skips the rest. else if(force < 25){}
for loops
(start, end, change) for(int i = 2; i < 12, i++) {} i-- for counting down
while loops
iterates while a condition is true. while (num <= 10){}
do/while loops
same as while but If the condition is false to begin with it will go through at least once. It can also cut down on code. do() while();
try/catch
try to make this block of code work, if it doesnt use the catch statement to show an error message
throw
creating an error message within a definition or within main. Any throw will work as long as catch has (…) next to it.

Overloaded Operators: classes

Overloaded Function
same name different parame­ters, need these to be able to do basic math/c­omp­arisons on objects student1 vs int 5
Member Functions - binary
Box operat­or+(box const &r­hs)­const adding a box variable to a rhs
Member Functions - binary - definition
Box Box::o­per­ato­r+(Box const &rhs) const {}.
Member functions unary
only the implicit argument is required.
Friend functions
not members of the class. given access to the private member variables. used specif­ically when you want to be able to use an object on the rhs rather than the lhs. friend Box operat­or+(Box const &lhs, Box const &rhs);
friend function as insertion operator
friend std::o­str­eam­& operat­or<­<(s­td:­:os­tre­am& out,Item &i­tem); DEFINI­TION: ostrea­m& operat­or<­<(o­stream &o­ut,Item &item) { out << "­Name: " << item.name_ << " Descri­ption: " << item.d­esc­rip­tion_ << " Price: " << item.p­rice_; }

scope

global
can be accessed from anywhere in the program, not placed in a function
local
also known as block scope. between []
function
between {} of a function
namespace
defined in the namespace.

overloaded << operator

writing a function

 

std::s­trings

defining a string
string s1("­Man­"); or string s2 = "­Bea­st"; or string s3;
combining strings
use the + operator. s3 += "­" and "­" + s2
Getline
reads up to an enter aka new line getlin­e(cin, name);
cin
reads up to a space cin >> name;
fixes for getline when coming after a cin
add a cin.ig­nore(); before the getline statement
.size() and .length()
do the same thing, finds the length of the string
Concat­enating with numbers
use to_string. string full = str + to_str­ing­(va­lue);
comparing strings
same idea as values A is smaller in askii than B, Lower case values are larger than upper case in the askii table.
.find()
returns the position of the search item.s­pecify a start position of search str.fi­ng(­"­iss); can also look for the option after the first time pos = str.fi­nd(­"­iss­"); str.fi­nd(­"­iss­", pos +1)
string­::npos
no position aka not found
substring
returns a portion of the string and specifies the end. int space = str.fi­nd(­" "); string school = str.su­bstr(0, space);
.at vs [] for iterating over strings
.at is read only where as [] is read and write, .at will terminate if you are looking for something out of range [] will display random junk
cstring vs std::s­tring
c-string pros: fixed length and prmitive data type. cons: watch for out of bounds. std::s­trings more intuitive to use and checks for out of bounds.

string­stream

#include <ss­tre­am>
adds strings, objects and values into a stream, parse streams and strings into values
<<
used to add to the string­stream. .str() function converts the stream to a string. string­stream sout; sout << "add words";
parsing
string str = "one two three four"; string­stream sout; sout << str; string temp; while(sout >> temp) {cout << temp << endl;}

class member functions example

example main, cpp and h files

parts of a function

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.