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.