Lecture 3 --- Classes
- Classes in C++; Types and defining new types; Example: A Date class;
- Class declaration: member variables and member functions; Using the class member functions;
- Member function implementation; Class scope; Classes vs. structs; Designing classes;
- Defining a custom sort of class instances.
3.1 Types and Defining New Types
- Every C++ object has a type (e.g., integers, doubles, strings, and vectors, etc., including custom types); and a class is a custom data type, also known as a user-defined data type. – The type tells us what the data means and what operations can be performed on the data.
3.2 C++ Classes
-
A C++ class group together variables and functions that operate on those variables. We call these variables member variables, and we call these functions member functions.
-
A C++ class consists of – a collection of member variables, usually private, and
– a collection of member functions, usually public, which operate on these variables. -
public member functions can be accessed directly from outside the class,
-
private member functions and member variables can only be accessed indirectly from outside the class, through public member functions.
3.3 Class scope notation
In the Date class example as you saw in lab 2,
- Date:: indicates that what follows is within the scope of the class.
- Within class scope, the member functions and member variables are accessible without the name of the object.
3.4 Constructors
These are special functions that initialize the values of the member variables. The constructor automatically get called when an object of the class is created. You have already used constructors for string and vector objects. As you have seen in the lab, constructors in C++ have the following characteristics:
- Constructors have the same name as the class they belong to.
- They do not have a return type, not even void.
- A class can have multiple constructors, and this is known as constructor overloading.