COP3503C - Quiz 2 Study Guide - Classes
scope resolution operator (SRO)
:: symbol which indicates the scope something "belongs" to
What does #pragma once do?
It replaces #ifndef, #endif, etc... - Most major modern compilers support this. - Much simpler, and the preferred way to do things today. (google: preprocessor directive designed to cause the current source file to be included only once in a single compilation)
What is the purpose of a mutator function?
Mutators: the "Set" functions, they change something about the class Ex: somePerson.SetAge(10); someDoor.Unlock(); someAircraft.IncreaseVelocity(2.15f);
What are the different levels of accessibility for class members?
Public - accessible everywhere but use it sparingly (especially for member variables!) cause in general, we want to hide data as much as we can. Private - the default accessibility that only code within class functions (ex: the class itself) can access (Trying to access private data outside of a class results in a compiler error. To avoid undesired changes, as much data as possible should be private. Generally speaking: Use private variables but public functions). Protected - Only applies when using inheritance (covered later in the course so ignore this keyword for now)
What do #include guards so
prevent something in a file from being defined more than once
What is an invoking object?
the object that called the function
What is the "this" keyword?
- a variable that all classes can access - it represents/points to the invoking object (the object that called the function) - It lets you reuse code in a class member function (meaning the same code can operate on diff instances of class variables) - It can resolve ambiguity between member variables and local variables
What are three scopes that exist in C++? Where can I access a variable in each of these scopes?
1. Class scope - Variables (and functions!) that are accessible in a class, or through an instance of that class 2. Namespace scope - Lets us put variables and functions in a "module" of sorts 3. Global scope - A scope that exists outside of (and "above") all others
What is the purpose of an accessor function?
Accessors: the "Get" functions, they retrieve something from the class Ex: someBankAccount.GetBalance();
How are classes broken down into separate files? (i.e., what parts of a class go in which files?)
Class files are typically split into two separate files: 1. Header file (.h or .hpp) - where the initial definition of the class itself is 2. Source file (.cpp) - Where class member functions are defined
What is encapsulation, and why do we do it?
Encapsulation: Group data and functionality together (into a single unit) Object-oriented programming utilizes classes to encapsulate data and functionality.
Why do we write classes?
We write classes to create objects (which represent the data for aspects of a program and may also contain functions to operate on that data) in code.
Can I include a .cpp in another .cpp file? Should I include a .cpp in another .cpp file? Why?
Yes. The language doesn't prevent you from doing it. No. It goes against common style and is likely to cause errors. (CPP files are for definitions, and definitions must be unique. If you copy/paste definitions, you are redefining things).
What accessibility does a class have by default? What about a struct?
default class accessibility: private default struct accessibility: public (otherwise a struct is exactly like a class in C++)
What is the narrowest scope?
statement scope - used for temporary variables - they are the variables declared inside if statements and loops