Final II Flashcards
An object consists of 3 things:
- Name
- Member Data
- Member Functions
An objects name is:
The variable name we give it.
Member data is:
The data that describes an object; Attributes, or the state of the object.
Member functions are:
Behavior aspects of an object (functions related to the object itself)
What is an object?
An encapsulation of data along with functions that act upon that data.
What is a class?
A blueprint for building objects; A user defined type.
A class description consists of:
A declaration and a definition. Usually split into separate files.
An object is a single instance of:
A class. You can create many objects from the same type of class.
What does DDU stand for?
Declare, Define, Use.
Declare (DDU)
Declaration of class (usually) in a header file; Gives an interface.
A variable declaration gives:
The type
A function declaration:
Tells how to use it, without bothering with how it works.
A class declaration:
Shows what an object will look like and what its available functions are, implementation details are not needed.
Define (DDU)
Definition of class members in implementation file.
T/F
The definition of a class doesn’t need to be seen by the user of the class (interface)?
True
A function definition:
The code that makes a function work (the function body)
A class definition:
Consists of definitions of its members.
Use (DDU)
Usage of class by building objects, through its interface (declaration).
The user of a function is:
A programmer, who makes calls to the function (without needing to know implementation details)
The user of a class is:
A programmer, who uses it by creating objects and calling the available functions for those objects.
3 Protection (access) levels in a class:
1) Public
2) Private
3) Protected
Public:
Can be accessed from inside or outside of the object.
Private:
Can only be used by the object itself.
Protected:
Protection label for use in base/derived classes; Members declared as protected can be accesses by the class itself and by derived classes only (not anywhere else)
The interface of an object is essentially comprised of:
The public section of the class.
The user of an objects is:
Some other portion of code (other classes, functions, main program)
Providing functions in the public area that handle all necessary actions on an object helps to:
Make the interface as simple as possible.
T/F
There are set rules on what gets made public and what gets made private?
False
Standard practice for member data of a class:
Make it private, to protect the data.
Functions that do not need to be part of the interface can be made:
Private
4 reasons for data hiding:
1) Makes interface simpler for user.
2) Principal of least privilege (need-to-know).
3) More secure. Less chance for misuse (accidental or malicious).
4) Class implementation easy to change without affecting other modules that use it.
Class declaration format:
class <classname></classname>
{
public:
(public data & functions)
private:
(private data & functions)
};
2 principles of good class design:
1) Decide upon semantic meaning of this type of object, along with what combinations of data values constitute valid meaning,
2) Guarantee that an object is always in a valid semantic state, regardless of what features user calls upon.
2 examples to guarantee valid semantic state of an object:
1) In a Circle class, don’t allow for a negative radius.
2) In a Temperature class, don’t allow a temperature to be below absolute zero.
2 ways to ensure an object is always in a valid semantic state:
1) In the public section, only put the members that the user needs to call upon directly.
2) In the private section, put things that the user does not need to use directly (this typically includes most member data)
By hiding member data in the private section:
The builder can control how the data items are updated, with validation/error-checking in the functions.
2 Primary categories of member functions:
1) Mutators
2) Accessors
Mutators:
Member functions that can change the state of an object.
Accessors:
Member functions that will not change the state of an object
Accessors should always be declared with:
const
Setter:
A common form of a mutator; A function that takes data in through parameters in order to set them into private variables on the inside.
Getter:
A common form of an accessor; A function that returns some internal data to the caller, usually by value (as a copy) or in some other read-only way.
Constructor:
A special member function of a class whose purpose is typically to initialize the member data of an object
Why have a constructor:
To ensure initialization that is not left up to the user; To set an object to a valid semantic state immediately upon creation.
3 properties of a Constructor
1) Has the same name as the class, no return type.
2) Automatically invoked upon creating an object.
3) Can have parameters but doesn’t have to;
Default constructor:
A constructor with no parameters.
T/F
A constructor is a function and you can define it to do anything you want.
True
Example call to default constructor:
Circle c1;
Example call to constructor with parameters:
Circle c1(51.4);
T/F
Circle c1(); calls the default constructor for the Circle class?
False, this declares a function named c3 that returns a Circle object.
How to make a constructor with optional parameters:
Use a default value on the parameter you want to make optional.
Example of a constructor with an optional parameter:
Fraction (int n, int d = 1);
Destructor:
A special function whose typical job is to do any clean-up tasks (usually involving memory allocation) that are needed before an object is deallocated.
3 properties of destructors:
1) Looks like the default constructor, but with a ~ in front.
2) Cannot have parameters, so there can only be one per class.
3) Called automatically, right before an object is deallocated by the system (usually when it goes out of scope)
T/F
Like the constructor, a destructor is a function and can do other things, if desired?
True.
Example declaration of a destructor:
~Fraction();
::
Scope resolution operator.
What is the scope resolution operator used for?
For specifying which class a member belongs to when we define it separately from its declaration inside the class.
To call a member function of an existing object use:
The dot operator (.)
-Syntax: f1.Show();
A class module normally consists of these two things:
1) A header file
2) An implementation file
Header file:
Contains the declaration of the class (without implementation details).
Usual form: filename.h
Implementation file:
Contains the implementations (definitions) of the class headers.
Usual form: filename.cpp
T/F
Filenames have to be the same as the class name:
False
Well-chosen filenames can:
Help identify the contents or purpose of a file.
2 major stages of compiling a program:
1) Compile stage
2) Linking stage
Compile stage consists of these 5 things
1) Checking syntax for correctness.
2) Variables and function calls checked to insure that correct declarations were made (“declare before use”).
3) Matching function calls to prototypes (doesn’t match calls to definitions yet).
4) Type checking on variable usage.
5) Translation into object code
Linking stage consists of these 2 things:
1) Linking object code files together, (usually) into an executable program.
2) Matches function calls to their definitions, checks to make sure it has only one definition for every function that is called).
T/F
The end result of the linking stage is usually an executable program?
True (but could be other target type like a DLL).
2 reasons for separate compiling and linking stages:
1) Changes to a file require only that file to be re-compiled, along with re-linking.
2) Libraries are often distributed in a pre-compiled format, so trying to #include a .cpp file would not be feasible.
CS account (g++) command for invoking compile stage only:
g++ -c filename.cpp
2 general rules for compilation and debugging:
1) Don’t #include .cpp files.
2) #include header files to satisfy “declare before use”.
What does it mean for a library to be distributed in a pre-compiled format?
The programmer only has header file and object code, not full implementation code)
3 Types of errors when compiling:
1) Compilation errors
2) Linker errors
3) Run-time errors
Compilation errors:
Usually syntax errors, undeclared variables and functions, improper function calls.
Linker errors:
Usually involve undefined functions or multiply-defined functions or symbols.
2 varieties of run-time errors:
1) Fatal: cause a program to crash during execution.
2) Non-fatal (logical): don’t crash the program, but produce erroneous results.
T/F
Compilation and linker errors result in a failed compilation of the program, while run-time errors occur while the program is running (after successful compilation)?
True
T/F
Compiler errors usually provides a filename and line number indicating where it ran into trouble, for each reported error?
True
4 tips for debugging compiler errors:
1) Always start at the top of the list of errors. Fix first error, then recompile and see what is left.
2) If a list of errors is too long, compile and debug one file at a time.
3) When searching for an error, start with the indicated line number, but also look in the vicinity (usually previous lines) for the possible error.
4) Compile portions of programs as you go, don’t wait until the program is fully written to do the first compile.
T/F
Linker errors are also reported by the compiler, but do not usually contain line numbers.
True, because the linker works on object code, not on the original source code.
3 tips for debugging linker errors:
1) Learn what kinds of problems cause linker errors (usually problems with agreement between definitions and calls).
2) Usually specify some kind of symbol (used by the compiler), which often resembles a function or variable name. This is usually a good clue.
3) Learn about good compilation techniques and pre-processor directives that help avoid linker errors.
T/F
Run-time errors must be tested while running a fully-compiled program?
True
2 tips for fixing run-time errors:
1) To catch fatal errors, try to wedge the mistake between extra printout statements to locate the cause.
2) To catch logic errors, place extra printout statements in code while testing (to be removed in final version). Especially, print out values of internal variables to locate computation problems.
Friend function:
Non-member functions that are given access to a class’ private section.
friend:
Keyword that allows a class to grant full access to an outside entity.
Full access means access to all the the class’ members, including private section.
Where are friend functions declared?
Inside the class definition block.
Is a friend function public or private?
Neither, by definition it is not a member of the class.
Does it matter where in the class definition block a friend function is placed?
No, because it is not a member of the class.
Example declaration of friend function:
friend bool Equals(Fraction x, Fraction y);
T/F
The keyword friend goes on the function definition in the implementation file?
False
T/F
The keyword friend goes on the function definition in the implementation file?
False