Section 4 Flashcards

1
Q

How would you make a function that returns something?

make a function take an integer and return that number added to 5

How would you store this in a variable?

A

int myFunction(int x) {
return 5 + x;
}

int main() {
cout &laquo_space;myFunction(3);
return 0;
}

TO STORE IN A VARIABLE

int main() {
int z = myFunction(5, 3);
cout &laquo_space;z;
return 0;
}

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

Pass a reference variable to a function.

Make a variable that says “Hello”
pass it through a function to say Hello world

A

void modifyStr(string &str) {
str += “ World!”;
}

int main() {
string greeting = “Hello”;
modifyStr(greeting);
cout &laquo_space;greeting;
return 0;

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

How would you pass an array to a function.

The function should take an array of 5 numbers and throw them in a for loop to show each number

A

void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
cout &laquo_space;myNumbers[i] &laquo_space;“\n”;
}
}

int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}

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

What does it mean to overload a function.

Give an example

A

Give two functions the same name that perform different operations

int plusFunc(int x, int y) {
return x + y;
}

double plusFunc(double x, double y) {
return x + y;
}

int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout &laquo_space;“Int: “ &laquo_space;myNum1 &laquo_space;“\n”;
cout &laquo_space;“Double: “ &laquo_space;myNum2;
return 0;
}

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

What is recursion?

A

Making a function call itself

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

Make a recursive function that you can call that prints out “You take a step!” 100 times

A

walk(100);

void walk(int steps) {
if(steps > 0) {
cout &laquo_space;“You take a step!\n”
walk(steps - 1)
}
}

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

What’s the difference between procedural programming and object oriented programming?

A

Procedural - writing procedures or functions that perform operations on the data

OOP - Creating objects that contain data and functions

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

What is DRY

A

Don’t Repeat Yourself!

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

What is a class comprised of?

A

Attributes (variables)
Methods (functions)
Objects that are instances of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
Q

Create a class with a method that prints something to the screen.

Create an object for it and call the method

A

class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout &laquo_space;“Hello World!”;
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

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

Show how you would create a Class with a single method and then define it outside of the class

A

class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class
void MyClass::myMethod() {
cout &laquo_space;“Hello World!”;
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

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

Create a method outside of the class that has a parameter called maxSpeed

A

include <iostream></iostream>

using namespace std;

class Car {
public:
int speed(int maxSpeed);
};

int Car::speed(int maxSpeed) {
return maxSpeed;
}

int main() {
Car myObj; // Create an object of Car
cout &laquo_space;myObj.speed(200); // Call the method with an argument
return 0;
}

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

What is a constructor?

A

This is similar to the __init__ method of a class in python

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

Create a constructor for a car class.
Let it take in brand model and year as parameters.
Create the attributes for the class afterword

Create the objects and give the appropriate arguments

Print the attributes

A

class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};

int main() {
// Create Car objects and call the constructor with different values
Car carObj1(“BMW”, “X5”, 1999);
Car carObj2(“Ford”, “Mustang”, 1969);

// Print values
cout &laquo_space;carObj1.brand &laquo_space;” “ &laquo_space;carObj1.model &laquo_space;” “ &laquo_space;carObj1.year &laquo_space;“\n”;
cout &laquo_space;carObj2.brand &laquo_space;” “ &laquo_space;carObj2.model &laquo_space;” “ &laquo_space;carObj2.year &laquo_space;“\n”;
return 0;
}

int main() {
Car chevy(“Kayak”, “Chevy”, 1990)
}

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

Create a constructor for a car class.
Let it take in brand model and year as parameters.

Give it the actual definition of the those attributes outside of the class.

Create the attributes for the class afterword

Create the objects and give the appropriate arguments

Print the attributes

A

class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};

// Constructor definition outside the class
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
}

int main() {
// Create Car objects and call the constructor with different values
Car carObj1(“BMW”, “X5”, 1999);
Car carObj2(“Ford”, “Mustang”, 1969);

// Print values
cout &laquo_space;carObj1.brand &laquo_space;” “ &laquo_space;carObj1.model &laquo_space;” “ &laquo_space;carObj1.year &laquo_space;“\n”;
cout &laquo_space;carObj2.brand &laquo_space;” “ &laquo_space;carObj2.model &laquo_space;” “ &laquo_space;carObj2.year &laquo_space;“\n”;
return 0;
}

17
Q

What is an access specifier

What are the three access specifiers?

A

These define how members (attributes and methods) of a class can be accessed.

So “public” means that they can be accessed and modified from outside of the code.

public

private - can’t be accessed or viewed outside of the class

protected - can’t be access from outside the class, but can be accessed in inherited classes.

18
Q

Which access specifier is it good practice to try and use every time and why?

A

private. This is also the default if you don’t specify and access specifier

This is so idiots like you and someone else don’t mess up the code.

19
Q

What is encapsulation?

A

To make sure that sensitive data is hidden from users.

like making access specifiers private.

20
Q

What methods can you use to get private methods and variables?

A

get and set methods

21
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

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

22
Q

Explain a scenario where a setter and getter are necessary

A

If you class has a deposit attribute as private

You could have the user deposit money through one setter and withdraw with another setter method.

The withdraw would keep you from taking out more than is avaiable showing that keeping the attribute private is a good idea.

23
Q

What are the two types of inheritance?

A

Derived class (child) - class that inherits from a class

Base Class (Parent) - class being inherited from.

24
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;
}

25
Q

MultiLevel inherritance.