Classes homework.
1 - Why do we need classes, why can’t we just use structs?
Both structs and classes, technically, can hold data as well as functions. It is considered good practice to use Classes for instances where functions are required as well as data.
2 - What are methods?
Functions stored inside of classes are called methods.
3 - How would you define a class?
class Cars
{
public:
string m_manufacturer;
int m_age;
int m_milesDriven;
};
4 - What is an object?
an object is an instance of a class.
Quiz solution:
#include <iostream>
using namespace std;
class IntPair{
public:
int var1;
int var2;
void set(int x,int y){
var1 = x;
var2 = y;
}
void print(){
cout << "Pair(" << var1 << ", " << var2 << ")" <<endl;
}
};
int main()
{
IntPair p1;
p1.set(1, 1); // set p1 values to (1, 1)
IntPair p2{ 2, 2 }; // initialize p2 values to (2, 2)
p1.print();
p2.print();
return 0;
}