1. Why do we need classes, why canât we just use structs?
Classes are very much like data-only structs, except that classes provide much more power and flexibility
2. What are methods?
In addition to holding data, classes can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods).
3. How would you define a class?
eg
class YearClass
{
public:
int m_day;
int m_week;
int m_year;
};
4. What is an object?
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
quiz
1a
#include
using namespace std;
class IntPair
{
public:
int m_one;
int m_two;
void set(int one, int two)
{
m_one = one;
m_two = two;
}
void print()
{
// create format Pair (x,x)
cout << "Pair(" << m_one << " , " << m_two << ")"<<endl;
}
};
int main()
{
IntPair p1;
p1.set(1, 1);
IntPair p2{ 2, 2 };
p1.print();
p2.print();
return 0;
}
1b
Because this object includes both member data and member functions.