Show Menu

C++ 101 Midterm 3 Cheat Sheet by

istream ostream

friend ostrea­m& operat­or<­<(o­str­eam­& out, room& r) { out << “Width:” << r.width << “, Length:” << r.length; return out; }
friend istrea­m& operat­or>­>(i­str­eam­& in, room& r) { cout << “Enter width: “ << endl; in >> r.width; cout << “Enter length: “ < endl; in >> r.length; return in; }

Strings vs C-strings (ch8)

#include <st­rin­g>
#include <cs­tri­ng>
Reading in:
>> stops reading at the first space
string input;
getline(cin, input);
char input[100]
cin.getline(input, 100);
string s1;
empty c-string: char s[20] = "­";
returning c-strings: return type of pointer.
char* funct(­){...r­etu­rn...}
for both: make sure that if you return something in a function if it's a variable defined in the function that is was defined dynami­cally or is static.
s.len­gth() or s.size()
strle­n(s) - s is a c-string
cannot use .length() or .size()
s2 = s1;
strcp­y(s2, s1) - copy s1 to s2
s1 == s2;
strcm­p(s1, s2) == 0; - tests for equality
s1 += s3
strca­t(c­har* s1, const char* s2);
string s to double stod(s);
char* s to double: strtod(s, nullptr);
c-strings have a null termin­ation character '/0'. So to define a c-string to hold 20 it's char input[­21];
same thing as >
c++ doesn't check indexes to validate if something is within bounds
int n = 3; cout << (char)(n + '0') << endl; prints 3.
char c = 'D'; cout << (c - 'A') << endl; prints 3.
char name[100]; char* get_name() { . . . return ______­___­______; } return name


Defa­ult: foo() no arguments
foo f1; or foo* f2 = new foo;
Conv­ers­ion: foo(int i) one argument to be turned into the class object
foo f1(3); or foo* f2 = new foo(3);
Gene­ral: foo(int x, int y) anything with more than one
Copy: foo(f­oo& f) pointer argument
Move: foo(f­oo&& f) double pointer

Constr­uctor and Initia­lizer List Examples

Choose the best C++ class named person that has: A private member field for the person’s first name; A private member field for the person’s last name; A private member field for the person’s age; and a public constr­uctor:
class Person{ string first; string last; int age;
public: person­(string f, string l, int a) : first(f), last(l), age(a) {} };
UML: Student
-name: string
+Student(n: string, g: double)
class Student
string name;
double gpa;
Student(string n, double g) : name(n), gpa(g) {} };
Write a single constr­uctor that works as default, conver­sion, and general: UML fraction
+fraction(n: int, d:int)
(default values: n = 0, d = 1)
fract­ion(int n = 0, int d = 1) : numera­tor(n), denomi­nat­or(d) {}
UML: Foo
+running: bool (set running to true)
+Foo(a_count: int)
-my_helper(arg: int) : char
class Foo
int count;
char my_hel­per(int arg)
bool running = true;
Foo(int a_count) : count(­a_c­ount) {} };


Only technical difference between structures and classes:
features in classes are private by default, features in structures are public by default
Constr­uctors name is
the same as the name of the class


+: public
-: private
#: protected
Auto: Stack
Dynamic: Heap
defining functions outside of a class: retur­n-type class:­:fu­nct­ion­-na­me(­arg­ume­nts) with a prototype included in class file.

Member­/Friend Summary

Implicit Args
Explicit Args
Unary Member:
Unary Friend:
Binary Mbr:
Binary Friend:

mbr/ nonmbr in and out a class

Mbr defined in the class
foo operat­or+(foo f){...}
Mbr defined out the class
foo foo::o­per­ato­r+(foo f){...}
Nonmbr defined in the class
friend foo operat­or+(foo f1, foo f2) {...}
Nonmbr defined out the class
foo operat­or+(foo f1, foo f2) {...}


main function definition to allow a program to access command line arguments:
main(int argc, char* argv[])
A program is named "­my_­pro­gra­m" and is executed from the command line as my_program file1 file2 file3 file4 If the program is written in C++ and the arguments are passed in to main, what is the value of argc and what is stored in argv[2]?
argc = 5, argv[2] = file2

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          EQ tips Cheat Sheet
          C# & Unity MonoBehaviour Cheat Sheet
          Numeric Formats Cheat Sheet