class className: memberAccessSpecifier baseClassName
{
memberList
};
the memberAccessSpecifier is either public, protected, or private (default)
private members of the base can't be directly accessed by members of the derived class
public members of a base class can be inherited either as public or private members by the derived class
the derived class can include data/functions
DEFINITIONS
INHERITANCE
an object oriented design concept which relates many classes together to allow specific classes using members of general classes
a kind of hierarchy relation between different classes of different levels
OVERRIDING
occurs when public functions of the base class are redefined in a derived class (subclass)
the redefined function in the subclass must have the same name, number, and types of parameters (formal parameter list)
Overriding Sample
class Circle
{
double selfRadius;
void print();
};
void Circle::print(){
cout << "Radius: " << selfRadius << "\n";
}
class Cylinder: public Circle
{
double selfHeight;
void print(); //overriding the print function of the class circle
};
void Cylinder::print(){
cout << "\nCall of print from class Cylinder.\n";
//call of print of Circle class
Circle::print();
}
OVERLOADING
occurs when both functions in the base class & in the derived class have the same name, but different formal parameter list (overloading is allowed in C++)
Overloading Sample
class Circle
{
void get(double&);
};
void Circle::get(double& r){
r = radius
}
class Cylinder: public Circle{
void get(double&, double&); //OVERLOADING
};
void Cylinder::get(double& r, double& h){
Circle::get(r);
h = height;
}
COMPOSITION
another way to relate two or more classes - similar to inheritance
used for objects that have a "has-a" relationship to each other
C++ allows object composition by using classes as member variables in other classes
POLYMORPHISM
this is an OOD principle
can be discussed via overloading or via templates
allows the programmer defining operators/functions w/ same name, but different formal parameter list
see slide 14 of chapter 2
TEMPLATES
enables the programmer to write generic codes for related functions & classes
enable the user writing a single code segment for a set of related functions (function template) or related classes (class template)
Template Syntax
template <class Type>
function definition;
Class Template Syntax
template <class Type>
class declaration;
INHERITANCE IMPLEMENTATION: CIRCLE
UML
SOURCE CODE
HEADER FILE: circle.h
/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: circle.h
# Dependency: circleImplementation.h
################################################*/
const double PI = 3.141592653589793238463;
class Circle
{
private:
double selfRadius;
public:
Circle(double radius);
double circumference();
double area();
};
HEADER FILE: circleImplementation.h
/*#######################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: circleImplementation.h
# Dependency: circle.h
#######################################*/
#include "circle.h" //this links the circle.h to implementation.h
Circle::Circle(double radius){
selfRadius = radius;
}
double Circle::circumference(){
return 2 * PI * selfRadius;
}
double Circle::area(){
return PI * selfRadius * selfRadius;
}
//inheritance; inherit the circle class
class Cylinder: public Circle //cylinder is the child class & circle is the base class
{
private:
double selfHeight;
public:
Cylinder(double radius, double height); //the 1st double is for radius which is in circle
//the 2nd double is for height which is in cylinder
double volume();
};
Cylinder::Cylinder(double radius, double height):Circle(radius){ //cylinder constructor w/ circle constructor
selfHeight = height;
}
double Cylinder::volume(){
return area() * selfHeight;
}