Section 4 Flashcards
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?
int myFunction(int x) {
return 5 + x;
}
int main() {
cout «_space;myFunction(3);
return 0;
}
TO STORE IN A VARIABLE
int main() {
int z = myFunction(5, 3);
cout «_space;z;
return 0;
}
Pass a reference variable to a function.
Make a variable that says “Hello”
pass it through a function to say Hello world
void modifyStr(string &str) {
str += “ World!”;
}
int main() {
string greeting = “Hello”;
modifyStr(greeting);
cout «_space;greeting;
return 0;
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
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
cout «_space;myNumbers[i] «_space;“\n”;
}
}
int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
What does it mean to overload a function.
Give an example
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 «_space;“Int: “ «_space;myNum1 «_space;“\n”;
cout «_space;“Double: “ «_space;myNum2;
return 0;
}
What is recursion?
Making a function call itself
Make a recursive function that you can call that prints out “You take a step!” 100 times
walk(100);
void walk(int steps) {
if(steps > 0) {
cout «_space;“You take a step!\n”
walk(steps - 1)
}
}
What’s the difference between procedural programming and object oriented programming?
Procedural - writing procedures or functions that perform operations on the data
OOP - Creating objects that contain data and functions
What is DRY
Don’t Repeat Yourself!
What is a class comprised of?
Attributes (variables)
Methods (functions)
Objects that are instances of the class
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 class with a method that prints something to the screen.
Create an object for it and call the method
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout «_space;“Hello World!”;
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Show how you would create a Class with a single method and then define it outside of the class
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};
// Method/function definition outside the class
void MyClass::myMethod() {
cout «_space;“Hello World!”;
}
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Create a method outside of the class that has a parameter called maxSpeed
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 «_space;myObj.speed(200); // Call the method with an argument
return 0;
}
What is a constructor?
This is similar to the __init__ method of a class in python
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
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 «_space;carObj1.brand «_space;” “ «_space;carObj1.model «_space;” “ «_space;carObj1.year «_space;“\n”;
cout «_space;carObj2.brand «_space;” “ «_space;carObj2.model «_space;” “ «_space;carObj2.year «_space;“\n”;
return 0;
}
int main() {
Car chevy(“Kayak”, “Chevy”, 1990)
}
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
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 «_space;carObj1.brand «_space;” “ «_space;carObj1.model «_space;” “ «_space;carObj1.year «_space;“\n”;
cout «_space;carObj2.brand «_space;” “ «_space;carObj2.model «_space;” “ «_space;carObj2.year «_space;“\n”;
return 0;
}
What is an access specifier
What are the three access specifiers?
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.
Which access specifier is it good practice to try and use every time and why?
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.
What is encapsulation?
To make sure that sensitive data is hidden from users.
like making access specifiers private.
What methods can you use to get private methods and variables?
get and set methods
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
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;
}
Explain a scenario where a setter and getter are necessary
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.
What are the two types of inheritance?
Derived class (child) - class that inherits from a class
Base Class (Parent) - class being inherited from.
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;
}
MultiLevel inherritance.
pass