Chapter 02
Chapter 02
OBJECT-ORIENTED DESIGN
1
4
OVERRIDING INHERITED FUNCTIONS
▪ You can override an inherited method by redefining it in the child class.
Example:
class Animal {// parent class
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};
void function_1 ( int x, int y ){ void function_2 ( double x, double y ){ void function_3 ( float x, float y ){
// code goes here… /* The same code goes here, but /* The same code goes here, but
} for “double” instead of “int” */ for “float” instead of “int” */
} }
Solution: Instead of writing the above three versions, just write a single template:
You can have more than one typename: template <typename T, typename V, …>
T and V can be used as if they were datatypes.
Let’s see an example… 10
FUNCTION TEMPLATE
void min_1(int x, int y){ void min_2(double x, double y){ void min_3(float x, float y){
if (x<y) cout << x; if (x<y) cout << x; if (x<y) cout << x;
else cout << y; else cout << y; else cout << y;
} } }
Now, calling the function is straight forward, for example, you may write:
int i = 5; int j = 7; minimum(i,j); // this will print 5
double x = 3.67; double y = 9.7881; minimum(x,y); // this will print 3.67
float a = 1.5; float b = 2.5; minimum(a,b); // this will print 1.5 11
CLASS TEMPLATE
▪ Similar to a function template declaration, it starts with
the keyword template followed by < > for the types
Syntax:
template <typename T, typename V, ...>
class MyClass
{ private:
V var; /* You can treat V like
any regular type */
public:
T functionName(V arg);
/* You can treat T and V like
any regular type */
};
13
CODE DEMONSTRATION
14
15
EXCEPTIONS
EXCEPTIONS
• When executing C++ code, different errors can occur, e.g., coding errors made by
the programmer, or user errors such as providing a wrong input
• To specify what happens when certain errors are made, use “exceptions”. For this,
you need 3 keywords: try, throw, and catch.
• Here is a syntax example (we’ll explain in more detail in the next slides):
try {
// some code can go here...
if( error_happens )
throw( exception_object );
// some code can go here...
}
catch ( exception_object ) {
// write here what you want to happen when error occurs
}
• We need to create an object that will be “thrown” when there is an error! 16
EXCEPTIONS
• First, we need to define a class for each type of error. Then, we will be
able to throw an object of that class if an error occurs!
• Suppose we define a generic class called MathException that we will use
whenever there is a math-related error. A possible definition could be:
class MathException {
private:
string errMsg; // The error message that is stored in the object
public:
/* A constructor. Remember, “: errMsg(err)” is an initializer list
that sets: errMsg = err; */
MathException(const string& err) : errMsg(err) { }
try {
if( z == 0 ){
throw( MathException( “Can’t divide by zero!” ) ); /* Notice how, inside the “throw”
statement, we created an object by calling the constructor of “MathException” */
}else
x = y/z;
}
catch ( MathException& obj ) {
cout << obj.getError(); /* This line will print the error message stored in the
object called “obj” */
}
18
CODE
DEMONSTRATION -4
19
EXCEPTIONS
• A function may specify the exceptions it throws. Here is an example:
void calculator() throw(ZeroDivide, NegativeRoot) {
// function body goes here ...
}
• You will see many examples in the book where a class has a function (i.e., a method)
that specifies the exception it throws, without worrying about the details of what
will happen when that exception is caught!
• This is mainly meant to emphasize that you should look out for, and “catch”, any
errors that may happen, but the book never provides additional details about the
exception object itself, since this is not important for understanding data structures!