Menu

(Solved) : 1 Objectives Wish Basic Electric Calculations Based Ohm S Law U R U Voltage Volts R Resist Q34191314 . . .

1. Objectives
We wish to do some basic electric calculations based on Ohm’s law,U = R∙I, where U is the
voltage in volts, R is the resistance in ohms and I is the currentintensity in ampere. We wish to do
this job while using notions in OOP.

2. Project description
Exercise 1 : Declaration and Overloading of operators

Create the class Volt. Its constructor should take as parametera double which by default should be zero.
For now, we wish to:
– Get this value by the accessor val(),
– print this value in a stream by using the operator<<.
– Read this value in a stream by using the operator >>.
Create a file Volt.h containing the declarations of the class Voltand the file volt.cpp
containing the implementations. When overloading the operator<<, think of displaying the unit (in
this case ‘V’ to say volts) after the value.

Hint: We need to overload the operators << and >>.Therefore, we need to use ostream and istream
for the output display and input reading << and >>respectively. E.g. for ostream, we can do as follows:

friend ostream &operator << (ostream &out, Volt&v) ;
in Volt.h
ostream &
operator << (ostream &out, Volt &v)
{
out << v.value << ” V”;
return out;
}
In Volt.cpp

Exercise 2 : In main
Hoping you test your code regularly, create a file proj2b.cpp thatwill contain the main. In this main, ask
the voltage to the user and display the value he inserted.

Exercise 3 : Implementing Ohm and Ampere
Repeat the work done in exercise 1 and 2 with the classes Ohm.h,Ohm.cpp and Ampere.h,
Ampere.cpp. Make sure in their respective display you adjust whatis relevant e.g. the units in the
Ohm class should be ohm for ohms and that of the Ampere classshould be A for amperes.

Exercise 4 : Multiplication of Objects
We try to multiply an Ampere and a Volt. What error is displayed bythe compiler?
  

Exercise 5 : Homogeneity
We wish to multiply objects such that the resulting unit has aphysical significance e.g. we can multiply
an Ampere with an Ohm and we expect the result to be an object oftype Volt.
a) Overload the operator ∗ in Ohm.h and Ohm.cpp to get the expectedresult described above.
b) Add what is necessary in the main such that, when we ask thecurrent in amperes and a resistance in
ohms to the user, we should display the resulting voltage involt.
c) What happens when we do an operation like: v=i ∗r; (where i isin amperes and r in ohms)? Why?
d) Correct the problem that occurred in c) above.
e) We now wish to use the fact that I = U/R and that R = U/I. Dothe necessary adjustments such that
we can do the following in the main:

Ohm r;   Volt v; Ampere i;

cout << “Resistance ? “;

cin >> r;

cout << “Current Intensity ? “;

cin >> i;

v = r * i;

cout << “Voltage = ” << v << endl;

cout << “Voltage ? “;

cin >> v;

cout << ” Current Intensity ? “;

cin >> i;

r = v / i;

cout << “Resistance = ” << r << endl;

cout << “Resistance ? “;

cin >> r;

cout << “Voltage ? “;

cin >> v;

i = v / r;

cout << “Intensit´ e = ” << i << endl;

Expert Answer


Answer to 1 Objectives Wish Basic Electric Calculations Based Ohm S Law U R U Voltage Volts R Resist Q34191314 . . .

OR