W2 P3: Classes and Scoped Enumerations Flashcards
Three class types:
class struct union
True or False: a Subject class may include a data member of Subject* type
True
True or False: a Subject class may include a data member of Subject type
False
How to combine a class definition with the definition of one or more class instances:
class-key Identifier {
sub-object declarations member function declarations
} identifier;
ex. class Subject { unsigned number; char desc[41]; } subject, *pSubject;
How to initialize a data member directly from the constructor definition:
Class-name(type x, …) : data-member-name{x},… { // any logic }
What is an anonymous class
A class without a class name
2 ways of defining an anonymous class:
// defining an instance of an // anonymous type (name)
class Subject { int number; struct { // definition - no tag char shortName[7]; char fullName[41]; } identifier; }
or
// declaring a synonym type (Course)
typedef struct { // synonym - no tag
unsigned number;
char desc[41];
} Course;
Prototypes for move constructors and move-assignment operators:
The prototype for a move-constructor takes the form
Class-name(Class-name&&);
The prototype for a move-assignment operator takes the form
Class-name& operator=(Class-name&&);
Call move assignment operator:
class1 = std::move(class2)
What is a class variable?
A class can include data members that hold the same information for all instances of the class and access that information regardless of the number of instances that currently exist. These are called class variables.
The keyword static declares a variable in a class definition to be a class variable.
ex.
unsigned Horse::noHorses = 0; // initializer
class Horse { unsigned age; unsigned no; public: static unsigned noHorses; Horse(unsigned a);
What is a class function?
A class function provides access to private class variables. The keyword static declares a function in a class definition to be a class function.
ex. class Horse { unsigned age; unsigned no; static unsigned noHorses; public: Horse(unsigned a); void display() const; ~Horse(); static unsigned howMany(); };
unsigned Horse::howMany() { return noHorses; }
What is a union?
- Members are public by default
- members of a union share the same address
- can only hold the value of one member at a time
What is an enumeration?
A type that holds a discrete set of symbolic constants.
- simplifies the readability and the upgradeability
ex.
enum Colour {white, red, green, blue};
std::ostream& operator