Andy Wang Object Oriented Programming in C++ COP 3330 Test 1 Review Andy Wang Object Oriented Programming in C++ COP 3330
Exam Format Multiple choice True/false Determine whether programming statements are legal Declare and define member functions Coverage: up to aggregation and composition
Class Basics Class Object A user defined type A blueprint for building objects Object An instance of a class With a name, member data, and member functions
DDU Design Model Declare (class declaration in the .h file) Define (class members defined in the .cpp file) Use (usage of class by building objects)
class Declaration vs. Definition Similar in many aspects Return types, member function names, number of parameters and types, keyword const Exceptions Keyword friend in declaration only A member functions’ parameter names can differ in its declaration and definition Need scope resolution operator :: in definitions of member functions Default parameter values are set in declaration The initialization list is only used in definitions
To Call Member Functions of an Object Use the dot operator
Public vs. Private Can everything be public? Who can access the private member data? Member methods Objects of the same class
Constructors Have no return type Have the same name as the class Cannot be called directly as a member function Called automatically Default constructors Have no parameters Used when no other constructors exist
Conversion Constructor A constructor that can be called with one parameter Even with additional optional parameters Converts the parameter type to the object type Unless with the keyword explicit, conversion constructors are called automatically
Compilation Stages Compile stage Link stage Checks syntax and types Matches member function calls with their declarations in the header files Matches function calls to prototypes Reports undeclared variables Translates to .o files Link stage Matches function calls with their definitions Checks for missing or duplicate definitions Usually results in an executable
g++ Commands g++ -c frac.cpp g++ -o frac frac.o main.o Compiles frac.cpp to frac.o g++ -o frac frac.o main.o Links frac.o and main.o to build a frac binary program
Misc Don’t include .cpp files #include header files to satisfy “declare before use” Debugging Compilation errors (syntax with file and line numbers…) Linker errors (undefined or multiply defined functions) Runtime errors: fatal (crashes) and logical (bad results)
friend function Neither public nor private Not a member function Definition has no scope resolution operator Can access the private member data via object.private_member_data
Keyword const Can only be used for member functions of a class Not for friend functions Commonly used for accessor and display functions Can disallow changes when passed by reference Function(const int &a); Can disallow member data of the calling object to be modified during a call Function() const;
L-value vs. R-value L-value = r-value x = 10 vs. 10 = x? L-value can appear on the left-hand side of an assignment Only an L-value can be passed by reference (without const) R-value can appear on the right-hand side of an assignment
More on const const objects const member data static const member data Can only call const member functions Or, it won’t compile const member data Cannot be initialized in class declaration Need to use an initialization list static const member data Can be initialized in class declaration Cannot be changed afterwards A single instance shared by all objects of the same class
Destructors Have no return type Have same name as the class, with ~ in front Only one destructor per class, provided by default Does not take parameters Called automatically when an object goes out of scope
Operator Overloading Cannot change a number of properties Precedence a + b*c Associativity (a + b) + c = a + (b + c) Number of operands Cannot create new operators Can only overload existing ones Cannot change the operator meaning on the built-in types Can mix and match types (Fraction object + int object)
friend vs. Member Functions for Overloading friend functions Each takes two parameters for binary operators, one parameter for unary operators Needed for overloading the following operators +, -, *, /, ==, !=, >=, <=, >, <, <<, >>, etc. Otherwise, the first parameter can be a built-in type, which may not have the conversion constructor for the newly defined type Member functions Each takes one parameter for binary operators, no parameter for unary operators
Exceptions Postfix increment and decrement operators (++, --) Need a dummy parameter of type int
Aggregation Embed an object of one class type as member data of another class type Composition Embedded objects would typically not exist independent of the container object “Has a” relationship Need to use the initialization list to initialize the embedded objects