Difficult One Flashcards

(28 cards)

1
Q

You’ve entered bad information using cin
How do you clear the input buffer to discard invalid input

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is everything you can use with fstream?

A

ifstream - input
i=eyes
read

ofstream - output
o=mouth
write

fstream - everything
f=f*ck it, you do what you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can you add to your lib.h file to make sure it’s only read once?

A

pragma once <- place this at the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a variable in lib.h and define it somewhere else

A

extern uint32_t command;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you append to a file?

A

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 &laquo_space;text;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you delete everything, even the lines from a file?

A

std::ofstream file(“todo.txt”, std::ofstream::trunc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Enter a multiline string for cin

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Show a simple class with two attributes

create an object (instance of that class) and give values to the attributes

A

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 &laquo_space;myObj.myNum &laquo_space;“\n”;
cout &laquo_space;myObj.myString;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a template

A

<template>

T add(T a, T b) {
return a + b;
}

template is like auto
</template>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a function that takes in an array reference

A

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 &laquo_space;arr[i] &laquo_space;” “;
}
cout &laquo_space;endl;
}

STANDARD ARRAY
void printArray(const array<int, 5>& arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Read a file line by line and send it to a deque

A

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)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Clear the screen

A

include <stdlib.h></stdlib.h>

system(“cls”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

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 &laquo_space;myObj.getSalary();
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

class Vehicle {
public:
string brand = “Ford”;
void honk() {
cout &laquo_space;“Tuut, tuut! \n” ;
}
};

// Derived class
class Car: public Vehicle {
public:
string model = “Mustang”;
};

int main() {
Car myCar;
myCar.honk();
cout &laquo_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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a namespace for a function and use it

A

namespace ONE
{
void myFunc()
{
cout &laquo_space;“this”;
}
}

ONE::myFunc()

You use this to avoid naming conflicts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If someone inputs a char instead of an int, how do you create a statement that doesn’t let it go through

A

if(std::cin.fail())

17
Q

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

A

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;
18
Q

When to use pointers vs references

19
Q

What is memory made up of?

A

ADDRESS 0x1000
VALUE 0x4
4 lives at the location at hex 1000
This memory is allocated to the STACK

20
Q

What does a pointer do in terms of memory?

A

ADDRESS 0x1004
VALUE 0x1000

Now 0x1004 points to 0x1000

21
Q

Create a pointer and explain it

A

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

22
Q

Why use a pointer

copy something with one

A

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.

23
Q

When creating a class reference, what do you do

A

voide func (Character&* character)

24
Q

Convert a string to an int

Convert an integer to a strng

A

uint32_t a = stoi(string_var_here)

std::string str = std::to_string(num)

25
Make your program require you to press enter
Protect yoself from buffer overflows, yo std::cin.ignore(std::numeric_limits::max(), '\n'); std::cin.get();
26
When is extern needed?
This is for global variables and does not apply to classes or functions. You would declare local variables in classes and functions inside the functions and classes without extern.
27
What does endl do?
Adds a new line and flushes the buffer Ex: When a program outputs "hello" the text "hello" goes into the buffer first. The buffer collects a bunch of output to send all at once which is more efficient than sending character by characer. When the buffer is flushed, everything waiting in the buffer is immediately sent out to the file, cli, etc. The buffer normally flushes when full or at program end. Ex: You'd use this when the program crashes and you want to output logs before it's done Prompting the user for input and wanting the prompt to appear right away. Debugging
28
Create a constructor for a class
Constructors always run the code inside Variables defined here will be free to use by all members class Printer { string name; public: Printer(string name) { _name = name } } This is a constructor that contains an argument and a child class that inherits #include #include #include #include #include #include class Employee { uint32_t salary; public: Employee(uint32_t amount) { salary = amount; } }; class Nathan:public Employee { public: Nathan(uint32_t amount) : Employee(amount) {}; void nathanSays() { std::cout << "In the Nathan class"; } }; int main() { Employee nathan(43000U); Nathan cahoe(45000U); return 0; }