Presentation is loading. Please wait.

Presentation is loading. Please wait.

Andy Wang Object Oriented Programming in C++ COP 3330

Similar presentations


Presentation on theme: "Andy Wang Object Oriented Programming in C++ COP 3330"— Presentation transcript:

1 Andy Wang Object Oriented Programming in C++ COP 3330
Test 1 Review Andy Wang Object Oriented Programming in C++ COP 3330

2 Exam Format Multiple choice True/false
Determine whether programming statements are legal Declare and define member functions Coverage: up to aggregation and composition

3 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

4 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)

5 class Declaration vs. Definition
Similar in many aspects Return types, member function names, number of parameters and types, keyword const Exceptions Declaration only Keywords friend and explicit Default parameter values Definition only Need scope resolution operator :: in definitions of member functions The initialization list is only used in definitions A member functions’ parameter names can differ in its declaration and definition

6 To Call Member Functions of an Object
Use the dot operator

7 Public vs. Private Can everything be public?
Who can access the private member data? Member methods Objects of the same class Friend functions with the same class via object.member_data

8 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

9 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

10 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

11 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

12 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)

13 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

14 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 MemberFunction() const;

15 L-value vs. R-value L-value = r-value
Assigns the r-value to the l-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

16 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

17 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

18 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)

19 friend vs. Member Functions for Overloading
friend functions Each takes two parameters for binary operators, one parameter for unary operators Often 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

20 Exceptions Postfix increment and decrement operators (++, --)
Need a dummy parameter of type int

21 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


Download ppt "Andy Wang Object Oriented Programming in C++ COP 3330"

Similar presentations


Ads by Google