cpp Inheritance
Published on: 08 February 2025
Resources
Aim: Construct a program that demonstrates implementation of Inheritance - Single and Multilevel
Practice Exercise 9: Write a program to demonstrate the concept of single inheritance by calculating the kinetic energy of fluid flow.
Theory:
Theory of Kinetic Energy in Fluid Flow
Kinetic energy in fluid mechanics refers to the energy possessed by a moving fluid due to its velocity. It is given by the equation:
$$ KE = \frac{1}{2} m v^2 $$
where $m$ is the mass of the fluid and $v$ is its velocity. Since mass flow rate ($\dot{m}$) is defined as:
$$ \dot{m} = \rho Q $$
where $\rho$ is fluid density and $Q$ is the volumetric flow rate, the kinetic energy per unit time can be expressed as:
$$ KE = \frac{1}{2} \rho Q v^2 $$
Algorithm:
- Start
- Input fluid density ($\rho$), flow rate ($Q$), and velocity ($v$)
- Compute mass flow rate:
$$ \dot{m} = \rho Q $$
- Compute kinetic energy:
$$ KE = \frac{1}{2} \rho Q v^2 $$
- Output the kinetic energy
- End
Code:
#include <iostream>
using namespace std;
class Fluid {
public:
double density; // Fluid density (kg/m³)
double flowRate; // Flow rate (m³/s)
// Parameterized Constructor
Fluid(double d, double q) {
density = d;
flowRate = q;
}
void showType() {
cout << "Fluid System:" << endl;
}
// Calculation function for mass flow rate
double calculateMassFlowRate() {
return density * flowRate;
}
// Calculation function for momentum (takes velocity as input)
void calculateMomentum(double velocity) {
double massFlowRate = calculateMassFlowRate();
double momentum = massFlowRate * velocity;
cout << "Momentum: " << momentum << " kg·m/s" << endl;
}
};
class KineticEnergy : public Fluid {
public:
double velocity; // Fluid velocity (m/s)
// Parameterized Constructor
KineticEnergy(double d, double q, double v) : Fluid(d, q) {
velocity = v;
}
// Calculation function for kinetic energy
void calculateKE() {
double KE = 0.5 * density * flowRate * velocity * velocity;
cout << "Kinetic Energy: " << KE << " Joules" << endl;
}
};
int main() {
// Creating an object using the parameterized constructor
KineticEnergy fluidSystem(1000, 0.05, 3); // density, flow rate, velocity
fluidSystem.showType();
// Calling calculation functions from Fluid class
cout << "Mass Flow Rate: " << fluidSystem.calculateMassFlowRate() << " kg/s" << endl;
fluidSystem.calculateMomentum(fluidSystem.velocity); // Call momentum calculation
// Calling calculation function from derived class
fluidSystem.calculateKE();
return 0;
}
Practice Exercise 10: Write a program to demonstrate multilevel inheritance by calculating the kinetic and potential energy of fluid flow.
Theory:
A fluid's kinetic energy and potential energy are calculated. Kinetic energy depends on fluid velocity, while potential energy depends on the fluid's height above a reference point.
Formulas:
- Kinetic Energy:
$$ KE = \frac{1}{2} \rho Q V^2 $$
- Potential Energy:
$$ PE = \rho Q g h $$
Algorithm:
- Define the base class
Fluid
with propertiesdensity
andflowRate
. - Define the intermediate class
KineticEnergy
to calculate kinetic energy. - Define the derived class
PotentialEnergy
to calculate potential energy. - Instantiate the derived classes, set fluid properties, and compute kinetic and potential energy.
Code:
#include <iostream>
using namespace std;
class Fluid {
public:
double density; // Fluid density (kg/m³)
double flowRate; // Flow rate (m³/s)
void setFluidProperties(double d, double q) {
density = d;
flowRate = q;
}
void showType() {
cout << "Fluid System:
";
}
};
class KineticEnergy : public Fluid {
public:
double velocity; // Fluid velocity (m/s)
void setVelocity(double v) {
velocity = v;
}
double calculateKE() {
return 0.5 * density * flowRate * velocity * velocity;
}
};
class PotentialEnergy : public KineticEnergy {
public:
double height; // Height (m)
void setHeight(double h) {
height = h;
}
void calculatePE() {
double PE = density * flowRate * 9.81 * height;
cout << "Potential Energy: " << PE << " Joules
";
}
};
int main() {
PotentialEnergy fluidSystem;
fluidSystem.setFluidProperties(1000, 0.05); // Example values: density, flow rate
fluidSystem.setVelocity(3); // Example velocity
fluidSystem.setHeight(10); // Example height
fluidSystem.showType();
cout << "Kinetic Energy: " << fluidSystem.calculateKE() << " Joules
";
fluidSystem.calculatePE();
return 0;
}
Aim: Construct a program that demonstrates implementation of Inheritance - Multiple, Hierarchical and Hybrid.
Practice Exercise 11: Write a program to demonstrate multiple inheritance by calculating the Reynolds number of fluid flow.
Theory:
The Reynolds number is a dimensionless quantity that determines the flow regime of fluid (laminar, transitional, or turbulent). It is calculated using the fluid's density, velocity, pipe diameter, and viscosity.
Formula:
$$ Re = \frac{\rho V D}{\mu} $$
Algorithm:
- Define the base class
Fluid
with propertiesdensity
andviscosity
. - Define the class
Velocity
to calculate flow velocity using the flow rate and pipe diameter. - Create a derived class
ReynoldsNumber
that inherits from bothFluid
andVelocity
. - Calculate the Reynolds number using the formula:
$$ Re = \frac{\rho V D}{\mu} $$
- Display the flow regime based on the Reynolds number.
Code:
#include <iostream>
using namespace std;
class Fluid {
public:
double density; // Fluid density (kg/m³)
double viscosity; // Fluid viscosity (Pa.s)
void setFluidProperties(double d, double v) {
density = d;
viscosity = v;
}
};
class Velocity {
public:
double flowRate; // Flow rate (m³/s)
double diameter; // Pipe diameter (m)
void setVelocityProperties(double q, double d) {
flowRate = q;
diameter = d;
}
double calculateVelocity() {
return flowRate / (3.1416 * diameter * diameter / 4);
}
};
class ReynoldsNumber : public Fluid, public Velocity {
public:
void calculateReynolds() {
double velocity = calculateVelocity();
double Re = (density * velocity * diameter) / viscosity;
cout << "Reynolds Number: " << Re << endl;
if (Re < 2000)
cout << "Flow is Laminar.
";
else if (Re > 4000)
cout << "Flow is Turbulent.
";
else
cout << "Flow is Transitional.
";
}
};
int main() {
ReynoldsNumber fluidSystem;
fluidSystem.setFluidProperties(1000, 0.001); // Example values: density, viscosity
fluidSystem.setVelocityProperties(0.05, 0.1); // Example flow rate, diameter
fluidSystem.calculateReynolds();
return 0;
}
Practice Exercise 12: Write a program to demonstrate hierarchical inheritance by calculating the kinetic and potential energy of the fluid flow.
Theory:
Kinetic and potential energy depend on the fluid's properties such as velocity and height. Multiple derived classes can inherit from the same base class.
Formulas:
- Kinetic Energy:
$$ KE = \frac{1}{2} \rho Q V^2 $$
- Potential Energy:
$$ PE = \rho Q g h $$
Algorithm:
- Define the base class
Fluid
with propertiesdensity
andflowRate
. - Define the class
KineticEnergy
to calculate kinetic energy. - Define the class
PotentialEnergy
to calculate potential energy. - Instantiate both classes, set fluid properties, and compute kinetic and potential energies separately.
Code:
#include <iostream>
using namespace std;
class Fluid {
public:
double density; // Fluid density (kg/m³)
double flowRate; // Flow rate (m³/s)
void setFluidProperties(double d, double q) {
density = d;
flowRate = q;
}
void showType() {
cout << "Fluid System:
";
}
};
class KineticEnergy : public Fluid {
public:
double velocity; // Fluid velocity (m/s)
void setVelocity(double v) {
velocity = v;
}
double calculateKE() {
return 0.5 * density * flowRate * velocity * velocity;
}
};
class PotentialEnergy : public Fluid {
public:
double height; // Height (m)
void setHeight(double h) {
height = h;
}
void calculatePE() {
double PE = density * flowRate * 9.81 * height;
cout << "Potential Energy: " << PE << " Joules
";
}
};
int main() {
KineticEnergy ke;
PotentialEnergy pe;
ke.setFluidProperties(1000, 0.05); // Example values: density, flow rate
ke.setVelocity(3); // Example velocity
pe.setFluidProperties(1000, 0.05); // Example values: density, flow rate
pe.setHeight(10); // Example height
ke.showType();
cout << "Kinetic Energy: " << ke.calculateKE() << " Joules
";
pe.calculatePE();
return 0;
}
Practice Exercise 13: Write a program to demonstrate hybrid inheritance by calculating the Reynolds number considering multiple fluid properties and velocity characteristics.
Theory:
Hybrid inheritance combines both multiple and multilevel inheritance concepts. In this case, the Reynolds number is calculated using fluid and velocity properties, where fluid and velocity are inherited separately.
Formula:
$$ Re = \frac{\rho V D}{\mu} $$
Algorithm:
- Define the base class
Fluid
with propertiesdensity
andviscosity
. - Define the class
Velocity
to calculate flow velocity using flow rate and pipe diameter. - Create a derived class
ReynoldsNumber
that inherits from bothFluid
andVelocity
. - Calculate Reynolds number:
$$ Re = \frac{\rho V D}{\mu} $$
- Display the flow regime based on the Reynolds number.
Code:
#include <iostream>
using namespace std;
class Fluid {
public:
double density; // Fluid density (kg/m³)
double viscosity; // Fluid viscosity (Pa.s)
void setFluidProperties(double d, double v) {
density = d;
viscosity = v;
}
};
class Velocity {
public:
double flowRate; // Flow rate (m³/s)
double diameter; // Pipe diameter (m)
void setVelocityProperties(double q, double d) {
flowRate = q;
diameter = d;
}
double calculateVelocity() {
return flowRate / (3.1416 * diameter * diameter / 4);
}
};
class ReynoldsNumber : public Fluid, public Velocity {
public:
void calculateReynolds() {
double velocity = calculateVelocity();
double Re = (density * velocity * diameter) / viscosity;
cout << "Reynolds Number: " << Re << endl;
if (Re < 2000)
cout << "Flow is Laminar.
";
else if (Re > 4000)
cout << "Flow is Turbulent.
";
else
cout << "Flow is Transitional.
";
}
};
int main() {
ReynoldsNumber fluidSystem;
fluidSystem.setFluidProperties(1000, 0.001); // Example values: density, viscosity
fluidSystem.setVelocityProperties(0.05, 0.1); // Example flow rate, diameter
fluidSystem.calculateReynolds();
return 0;
}
References
There may be some AI Generated content in this article used for demonstration purposes.