Difficult One Flashcards
(28 cards)
You’ve entered bad information using cin
How do you clear the input buffer to discard invalid input
import limits
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
The doctor yells CLEAR! and brings IGOR back to life.
IGOR’s hands go NUMB UNDER his gloves so he uses his LIMBS to STREAM his favorite show superSIZE me. He tries playing with his mighty MAX() toy that has 2 figureines to get his feeling back.
What is everything you can use with fstream?
ifstream - input
i=eyes
read
ofstream - output
o=mouth
write
fstream - everything
f=f*ck it, you do what you want
What can you add to your lib.h file to make sure it’s only read once?
pragma once <- place this at the top
How do you create a variable in lib.h and define it somewhere else
extern uint32_t command;
How do you append to a file?
std::ofstream MyFile(“todo.txt”, std::ios::app);
The proFeSsor grabs his TEXTbook, tries to read it, but instead gets on his IPHONE and looks at his favorite APP
REMEMBER JESUS FISHING
std::string text;
std::ofstream MyFile(“test.txt”, std::ios::app);
std::getline(std::cin, text);
MyFile «_space;text;
How do you delete everything, even the lines from a file?
std::ofstream file(“todo.txt”, std::ofstream::trunc)
Enter a multiline string for cin
might need “limits” library here
std::getline(std::cin, to_do);
to_do += ‘\n’
Jesus asks you to get a line so he can fish.
After you give it to him he is so thankful that his puts his hands together, one side holds a rock the other holds your sins. He places your sins in the rock and then tosses it in the water.
Show a simple class with two attributes
create an object (instance of that class) and give values to the attributes
class MyClass {
public:
int myNum;
sting myString;
};
public is and access specifier which specifies the members (The attributes and methods)
int main() {
MyClass myObj;
myObj.myNum = 15;
myObj.myString = “Some text”;
cout «_space;myObj.myNum «_space;“\n”;
cout «_space;myObj.myString;
return 0;
}
Create a template
<template>
T add(T a, T b) {
return a + b;
}
template is like auto
</template>
Create a function that takes in an array reference
If you want to add a template here REMEMBER it can’t use TYPENAME, it has to be a non-type that is used for sizes and counts like the below
template<std::size_t T >
AND if you want to return different things you’d have to have a COUNT/SIZE type AND a regular type like this
template<std::size_t T, typename N>
void printArray(const N(&arr)[5])
RAW
void printArray(int (&arr)[5])
{
for (int i = 0; i < 5; i++)
{ cout «_space;arr[i] «_space;” “;
}
cout «_space;endl;
}
STANDARD ARRAY
void printArray(const array<int, 5>& arr)
Read a file line by line and send it to a deque
std::deque<std::string> items;
std:uint32_t to_do;
std::fstream MyFile(“todo.txt”)
std::string MyText;
while(getline(MyFile, MyText))
{
items.push_back(MyText)
}
Clear the screen
include <stdlib.h></stdlib.h>
system(“cls”)
Create an employee class that has a private attribute “salary” declared but not defined.
Create two public get and set methods.
Set should set salary to whatever the user wants to set it to.
Make the get method give the user back the modified salary attribute
Very simple concept here. You can’t change salary with an object trying to access it directly, but internal public methods can do the work for you. They’re like middle men.
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout «_space;myObj.getSalary();
return 0;
}
Create a base class of Vehicle that has a public attribute of brand = Ford
and a method that print “honk honk”
Create a derived class that inherits from it with a model of Mustang public attribute
Create an object from the derived class and make it honk and then print its brand and model
class Vehicle {
public:
string brand = “Ford”;
void honk() {
cout «_space;“Tuut, tuut! \n” ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = “Mustang”;
};
int main() {
Car myCar;
myCar.honk();
cout «_space;myCar.brand + “ “ + myCar.model;
return 0;
}
public = public and private stuff stays the same here
private = make everything private in your class, you can’t access stuff via the main()
protected - Car can access private methods internally but can’t expose them externally
Create a namespace for a function and use it
namespace ONE
{
void myFunc()
{
cout «_space;“this”;
}
}
ONE::myFunc()
You use this to avoid naming conflicts
If someone inputs a char instead of an int, how do you create a statement that doesn’t let it go through
if(std::cin.fail())
Perform exception handling where you can have 3 different options for bad input including but not limited to:
invalid_argument
out_of_range
anything else
include <bits/stdc++.h>
using namespace std;
int main() {
// Code that might throw an exception try { int choice; cout << "Enter 1 for invalid argument, " << "2 for out of range: "; cin >> choice; if (choice == 1) { throw invalid_argument("Invalid argument"); } else if (choice == 2) { throw out_of_range("Out of range"); } else { throw "Unknown error"; } } // executed when exception is of type invalid_argument catch (invalid_argument e) { cout << "Caught exception: " << e.what() << endl; } // executed when exception is of type out_of_range catch (out_of_range e) { cout << "Caught exception: " << e.what() << endl; } // executed when no matching catch is found catch (...) { cout << "Caught an unknown exception." << endl; } return 0;
When to use pointers vs references
What is memory made up of?
ADDRESS 0x1000
VALUE 0x4
4 lives at the location at hex 1000
This memory is allocated to the STACK
What does a pointer do in terms of memory?
ADDRESS 0x1004
VALUE 0x1000
Now 0x1004 points to 0x1000
Create a pointer and explain it
int x =4;
integers are 4 bits
int *pX = &x;
int = type
* = modifies the type to pointer
pX = variable name
= set in memory
& = the address of
x = the original variable
All this just say the integer pointer pX is set to the address of x
Why use a pointer
copy something with one
Access x by reference rather than value
This avioids making
We can copy a variable
int * pX = &x;
int y = *pX;
REMEMBER * modifies types normally, there’s no type in the second example so it works as a dereference point(Go to the address and grab that value) Otherwise it’s just a copy of the mem address
*pX = the thing pointed to by pX
You can change what’s being referenced with a pointer and not a reference variable.
This means that you save a bit of space since you can just reuse a pointer for something else.
When creating a class reference, what do you do
voide func (Character&* character)
Convert a string to an int
Convert an integer to a strng
uint32_t a = stoi(string_var_here)
std::string str = std::to_string(num)