C++ Primer - Chapter 7 (Classes) Flashcards
What are the fundamental ideas behind classes?
Abstraction and encapsulation.
What does “abstraction” mean?
A programming (and design) technique that relies on the separation of interface from implementation.
What does “encapsulation” mean?
Encapsulation means hiding the code and data into a single unit (e.g. class or method) to protect the inner working of an object from the outside world.
Fill in the blank: A class that uses data abstraction and encapsulation defines a \_\_\_\_\_\_\_\_\_\_\_ type.
A class that uses data abstraction and encapsulation defines an abstract data type.
True or false? Functions defined inside the class are implicitly inline.
True.
What type does “this” have? (When used inside a class object.)
A const pointer.
What implicit parameter is passed to a member function when the function is called from an object?
A const pointer to “this”.
The “this” pointer is implicitly passed to a member function, so it cannot be made a pointer to const. How do we ensure that a member function does not alter the object?
By adding a “const” after the parameter list of the function.
True or false? A const object, and references or pointers to const objects, may call nonconst member functions.
False. A const object, and references or pointers to const objects, may only call const member functions.
What is the term given to function of a class which contains a const after its parameter list?
A const member function.
Why can the body of a member function use member data that is declared after the member function is?
Because the compiler processes member declarations before processing the body of a member function (if there is one).
How does a compiler process a class?
First, the compiler processes member declarations of the class, after which the member function bodies, if any, are processed.
What operator must be used to define a member function outside a class?
The scope operator, “::”.
True or false? IO objects used to read or write can be passed as parameters of a function as references to const.
False, reading or writing to a stream changes that stream so it can only be passed by reference (it cannot be copied either).
What does the condition in the if-statement below do? istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } if (read(read(cin, data1), data2)) { /*...*/ }
It checks that both data1 and data2 have been read in correctly.
What member functions are responsible for initialising the member data of a class object?
Constructors.
What return type does a constructor have?
Trick question; a constructor does not have a return type.
Where is the error in the following code? class SillyHuman { public: SillyHuman() const {} private: int variableA=10; int variableB=variableA; };
class SillyHuman { public: SillyHuman() const {} // This is an error; a constructor can never be const. private: int variableA=10; int variableB=variableA; };
What is the problem with the following code? class SillyHuman { public: SillyHuman() {variableA=10;} private: const int variableA; };
class SillyHuman { public: SillyHuman() {variableA=10;} // Const member data can only be initialised using the constructor initialiser list. private: const int variableA; };
True or false? It is an error to create a class without a constructor.
False. The compiler can generate a synthesised constructor (but it is a bad idea to rely on a synthesised constructor).
If the definition of a class object contains no initialiser, how are the members of that object initialised?
Through the default constructor.
The default constructor is one that takes no argument.
Suppose a class named "Sales_data" is defined. What is the issue with the following default initialisation of the object "total"? Sales_data total();
Sales_data total(); // The compiler will mistake "total" as the name of a function that takes no arguments and returns an object of type Sales_data.
What is the name given to the compiler-generated constructor? (If no constructor is provided.)
The synthesised constructor.
How does the synthesised constructor initialise each member data of the class?
It first checks if there is an in-class initialisation (C++11) of the member data; if there is not, it will default initialise the variable.
How many arguments must a default constructor take?
None.
Why is it a bad idea to rely on the compiler to generate a synthesised constructor?
Because member data may become uninitialised. This can occur if the variable does not have a default constructor or it is a built-in or compound type
(Note: remember that objects of built-in type or compound type defined inside a block had undefined value when they are initialised.)
Is there an error in the following code? If so, where? class SillyHuman { int variableA; }; int main() { SillyHuman human; }
// The code below contains no errors. The compiler can create a default initialiser to initialise the value of variableA. class SillyHuman { int variableA; }; int main() { SillyHuman human; }
Is there an error in the following code? If so, where? class SillyHuman { public: SillyHuman(int a) : variableA(a) {} private: int variableA; }; int main() { SillyHuman human; }
class SillyHuman { public: SillyHuman(int a) : variableA(a) {} private: int variableA; }; int main() { SillyHuman human; // This is an error; the class SillyHuman already contains a constructor, and the compiler assumes that if the user needs to control the initialisation of an object in one case, it will need to in all cases. }
How can a constructor that takes no arguments be chosen to be the default constructor? (C++11)
By adding “=default” after the parameter list (C++11).
Where does the constructor initialiser list go?
After the constructor parameter list; it is preceded by a colon.
If member data is not initialised in the constructor initialiser list, how will it be initialised?
When a member is omitted from the constructor initialiser list, it is implicitly initialised using the same process as is used by the synthesised default constructor (i.e. it will use the in-class initialiser (C++11), if there is one, and if not, it will be default initialised).
How can encapsulation be enforced in a class?
By the inclusion of access specifiers (e.g. “public” and “private”).
If a constructor initialiser list is empty, what value will member data have inside the constructor body?
Member data will be default initialised before the constructor body is executed.
What is the sole difference between a struct and class?
By default, all members of a struct are “public” and all members of a class are “private”.
How can a class access the members of another class?
By being made a friend of that class (i.e. the one being accessed).
True or false? A friend declaration of a member function is an “official” declaration of that function.
False. A friend declaration only specifies access; it is not a general declaration of the function.
Do compilers enforce the rule that friend functions must be declared outside the class before they can be used?
No. Well, most compilers do not.
Is there a problem with the following code? If so, why? class Screen { private: pos cursor = 0; pos height = 0, width = 0; std::string contents; public: typedef std::string::size_type pos; };
class Screen { private: pos cursor = 0; pos height = 0, width = 0; std::string contents; public: typedef std::string::size_type pos; // Here is the error; unlike other member data, members that define types must be declared before they are used. };
True or false? It is an error to provide an inline specifier before both the declaration and definition of a function (if they are separate).
False. It is legal to specify that a function is inline in both its declaration and definition.
Note: as member functions defined inside the class body are implicitly inline, it can make it easier to read if the inline specifier is only used before the function definition.
How can we ensure that a member of a class can be modified, even inside a const member function?
By declaring that member to be mutable.
True or false? A mutable member data can sometimes be const.
No, a mutable member data is never const, even if it is a member of a const object.
(C++11:) What is the syntax for in-class initialisation?
The member data must either be followed by an “=” and the defined value or inside a braced list.
(C++11:) True or false? Member data of class type cannot be initialised using in-class initialisation.
False.
What is the return type of a member function that returns “*this”?
The return type should be a reference to const.
What is the term given for the declaration of a class before it is defined?
A “forward declaration”.
What is the type of a class after it has been declared and before it has been defined?
An “incomplete type”.
In what ways can an incomplete type be used?
In a limited number of ways: by defining pointers or references to such types or declaring functions (not defining) that use an incomplete type in their parameter list or return type.
True or false? It is possible to use code that creates objects of an incomplete type.
False.