Classes in C++ - Reading Assignment

#101
1. Why do we need classes, why can't we just use structs?

Structs are typically used to hold data only, although structs may contain functions. Classes provide a way to hold data and functions that work with the data as well. Data only structs need the functions to be defined separately. Classes will also dealocate memory after being destroyed, which increases performance, you may not assume this using a struct.

2. What are methods?

Methods or member functions are functions inside a class that use the data inside the class.

3. How would you define a class?

First declare the class

class DateClass
{
public:
    int m_year;
    int m_month;
    int m_day;
};

then declare the variables of the class using the known method:
DateClass today { 2020, 10, 14 };

4. What is an object?

An object is a variable of a class type, also called an instance

#102

1. Why do we need classes, why can’t we just use structs?
Classes allow for functions to be included. Structs can only hold data.
2. What are methods?
Functions which are a member of a class.
3. How would you define a class?
class TestClass {
code goes here…
};
4. What is an object?
An object is an instance of a class.

#103

1. Why do we need classes, why can’t we just use structs? Use the struct keyword for data-only structures. They can contain functions, but they do not implicitly pass the struct object that is inside the struct. You need to explicitly pass the object to the function (ie: class usage of member function: today.print(); vs struct function usage: print(today):wink: In the struct use of the function, you need to pass the struct to the function, otherwise it will not know what to do. Versus the class use of the member function, it’s dot notation implicitly passes the class object into the function. Use the class keyword for objects that have both data and functions
2. What are methods? Methods are functions that are within classes or structs .
3. How would you define a class? You can define a class the same way as a struct, the convention is to name it with initial caps and classes can have private and public member variables.
4. What is an object? A variable of a class type is also called an object

#104
  1. Structs on their own are not that useful as they only contain data but don’t have a way to alter that data. Classes allow programmers to take data as inputs (e.g structs, arrays, primitive data types) and manipulate the state of the data in order to produce some behaviour.

  2. Methods are member functions within a class which can take input and manipulate it order to produce some specific behaviour or functionality

  3. class MyClass {
    // member variables

// member functions
void myMethod() {
// do something to the variables
}
};

  1. An object is an instance of a class. For example, a programmer could create a class called Person. They could then create instances of that Person class which are the objects and can represent things in the real world e.g. John, Sarah e.t.c
#105
  1. Because structs are data-only whereas classes can embed functions
  2. Methods are functions that belong to a class or at least that relate to a class
  3. A class is a model, like a struct for describing properties but with tools (methods) in addition
  4. An object is an instantiation of a class which means this is a concrete realization of a class. A class is an abstraction, a set of properties and methods whereas an object is a concrete materialization of this abstraction, it’s an exemple of applied set of properties and methods.
#106
  1. Why do we need classes, why can’t we just use structs?
    Classes hold member variables and member functions and structs hold only variables.

  2. What are methods?
    Member functions defined inside of a class.

  3. How would you define a class?
    Class Date {
    Public:
    Int m_year;
    Int m_month;
    Int m_day;

    Void printDate() {
    Cout << m_year << “/” << “/” << m_day;
    }
    }

  4. What is an object?
    A variable of a class is an object.