Professional Documents
Culture Documents
PROGRAMMING
CHAPTER
7&8
BY THEISA
Constructors
Example:
class Rectangle {
public:
int length;
int width;
Rectangle() {
length = 0;
width = 0;
}
};
Destructors
Example:
class Rectangle {
public:
int* arr;
Rectangle() {
arr = new int[10];
}
~Rectangle() {
delete[] arr;
}
};
Function Overriding
Function overriding is a feature that allows a derived class
classes.
Templates
Function Templates
Syntax:
Example:
Syntax:
Example:
public:
// Code
};
Exception Handling
Try-Catch Block
Syntax:
try {
// Code that might throw an exception
}
catch (ExceptionType& exceptionObject) {
// Code to handle the exception
}
Example:
try {
int result = divide(10, 0);
}
catch (const std::runtime_error& error) {
std::cout << "Exception occurred: " << error.what() <<
std::endl;
}
Exception Handling
Throwing Exceptions
Syntax:
Example: