Classes
CSE 232 - Dr. Josh Nahum
Reading:
Section 2.3
Table of contents
00
02
01
enum & union
Return Type
Constructors
00
enum & union
—Me
“enums and unions are powerful types, but not essential enough to be taught in CSE 232.”
01
Return Type
Alternative Return Types
Danger! Functions should not return references or pointers to variables that are local to the function. These local variables will fall out of scope and be deleted when the function call ends.
Most function return ordinary types (non-const copies of objects).
Functions can return pointers, references and const objects, if needed.
Live Coding Example
02
Constructors
Constructors
Constructors never have a return type
Body
Constructor
No Return Type
Optionally a list can be provided to initialize data members
The body of the constructor can to other initialization work (if any)
Member initializer list
A constructor is a member function with the same name as the class
Default Constructor
Could also be written like this with a member initialization list:
class Vector {
public:
Vector() :elem{nullptr}, sz{0} {}
// ...
};
A default constructor is a constructor that can be called with no arguments.
Example:
class Vector {
public:
Vector() {
elem = nullptr;
sz = 0;
}
// ...
};