|
 |
 |
[an error occurred while processing this directive]
- 2. Using the functionality of the Resistance class, you can take advantage of inheritance in C++ to create two new classes. The Voltage class accepts the voltage (V) and resistance (R) and then uses them to calculate the current (I). Note the first line definitions of the Voltage class and the Current class. They both refer to Resistance as virtual.
class Voltage : virtual public Resistance
class Current : virtual public Resistance
This gives rise to the inheritance model shown in Figure 5.26.
Figure 5.26 The virtual inheritance model used by Voltage and Current.
- 3. Here is the program; give it a try.
// Inheritance
#include <iostream.h>
class Resistance
{
protected:
int R;
public:
Resistance(int r) {R = r;}
virtual void Rout(void) {cout << R << endl;}
};
class Voltage : virtual public Resistance
{
protected:
double V;
double I;
public:
Voltage(double v, int r):Resistance(r)
{V = v;}
virtual void CalcI(void)
{I = V/R;}
virtual void Iout(void)
{cout << I << endl;}
};
class Current : virtual public Resistance
{
protected:
double V;
double I;
public:
Current(double i, int r):Resistance(r)
{I = i;}
virtual void Vout(void)
{cout << V << endl;}
virtual void CalcV(void)
{V = I*R;}
};
main()
{
// Given the voltage and resistance
// We can calculate the current
Voltage V1(10,27000);
V1.CalcI();
V1.Rout();
V1.Iout();
cout << endl;
// Given the current and the resistance
// We can calculate the voltage
Current I1(0.2,68000);
I1.CalcV();
I1.Rout();
I1.Vout();
cout << endl;
return(0);
}
- 4. You can modify the preceding program to produce a nonvirtual inheritance model by simply removing the keyword virtual from the declarations of Voltage and Current.
class Voltage : public Resistance
class Current : public Resistance
This modification gives rise to the nonvirtual inheritance model shown in Figure 5.27.
Figure 5.27 The nonvirtual inheritance model used by Voltage and Current.
- 5. Here is the nonvirtual version of the program. Again, at this level it works and appears to be identical to its virtual counterpart.
// Nonvirtual Inheritance
#include <iostream.h>
class Resistance
{
protected:
int R;
public:
Resistance(int r)
{R = r;}
virtual void Rout(void)
{cout << R << endl;}
};
class Voltage : public Resistance
{
protected:
double V;
double I;
public:
Voltage(double v, int r):Resistance(r)
{V = v;}
virtual void CalcI(void)
{I = V/R;}
virtual void Iout(void)
{cout << I << endl;}
};
class Current : public Resistance
{
protected:
double V;
double I;
public:
Current(double i, int r):Resistance(r)
{I = i;}
virtual void Vout(void)
{cout << V << endl;}
virtual void CalcV(void)
{V = I*R;}
};
main()
{
// Given the voltage and resistance
// We can calculate the current
Voltage V1(10,27000);
V1.CalcI();
V1.Rout();
V1.Iout();
cout << endl;
// Given the current and the resistance
// We can calculate the voltage
Current I1(0.2,68000);
I1.CalcV();
I1.Rout();
I1.Vout();
cout << endl;
return(0);
}
- 6. Having created classes to deal with resistance, voltage, and current, it makes sense to unite them into a single OhmsLaw class because that is their common root. You will do this using a multiple inheritance model that draws together all the functionality developed so far. Figure 5.28 shows that model.
Figure 5.28 The virtual multiple inheritance model of the OhmsLaw class.
- 7. The following program pulls together all the operations for Ohms Law in order to calculate and display any combination of voltage, resistance, and current. The important point is that you have used multiple inheritance and virtual classes to collect those actions.
|