Docsity

Op Overload-Object Oriented Programming-Lecture Handout, Exercises of Object Oriented Programming

Toggle sidebar
bg1
1C:\DocumentsandSettings\usman.younis\MyDocuments...\Projects\op_overload\op_overload\op_overload.cpp
#include<iostream>
usingnamespacestd;
classResistor
{
private:
 floatR;
 float
V
;
 floatI;
public:
 Resistor(){}
 Resistor(floatr,floati):R(r),I(i)
 {
V=I*R;
 }
 voidsetI(floati)
 {
I=i;
V=I*R;
 }
 voidsetR(floatr)
 {
R=r;
V=I*R;
 }
 floatgetR(){returnR;}
 floatgetI(){returnI;}
 floatget
V
(){return
V
;}
 friendResistoroperator+(Resistor&r1,Resistor&r2);
};
Resistoroperator+(Resistor&r1,Resistor&r2)
{
 floatI=r1.getI()+r2.getI();
 floatR=r1.getR()+r2.getR();
 returnResistor(R,I);
}
classSeries
_
Circuit
{
private:
 ResistorRcomponent;
public:
 Series
_
Circuit():Rcomponent(0.0F,0.0F){}
 Series
_
Circuit(floatr,floati):Rcomponent(r,i){}
 voidshow
_
circuit()
 {
cout<<"ThefinalcircuitResistanceis:"<<
Rcomponent.getR()<<endl;
cout<<"Voltageacrossthecircuitis:"<<
Rcomponent.get
V
()<<endl;
docsity.com
pf3
pf4

Partial preview of the text

Download Op Overload-Object Oriented Programming-Lecture Handout and more Exercises Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class Resistor { private: float R; float V; float I; public: Resistor () {} Resistor(float r, float i): R(r), I(i) { V = IR; } void setI(float i) { I = i; V = IR; } void setR(float r) { R = r; V = I*R; } float getR() { return R; } float getI() { return I; } float getV() { return V; } friend Resistor operator + (Resistor& r 1 , Resistor& r 2 ); }; Resistor operator + (Resistor& r 1 , Resistor& r 2 ) { float I = r 1 .getI() + r 2 .getI(); float R = r 1 .getR() + r 2 .getR(); return Resistor(R, I); } class Series_Circuit { private: Resistor Rcomponent; public: Series_Circuit(): Rcomponent( 0. 0 F, 0. 0 F) { } Series_Circuit(float r, float i):Rcomponent(r, i) {} void show_circuit() { cout<<"The final circuit Resistance is : "<< Rcomponent.getR()<<endl; cout<<"Voltage across the circuit is : "<< Rcomponent.getV()<<endl;

cout<<"The series current is : "<< Rcomponent.getI()<<endl; } friend void operator += (Series_Circuit& sc, Resistor& res); friend void operator ‐= (Series_Circuit& sc, Resistor& res); }; void operator += (Series_Circuit& sc, Resistor& res) { float current = 0. 0 F; current = sc.Rcomponent.getV() / (sc.Rcomponent.getR() + res.getR()); sc.Rcomponent.setR(sc.Rcomponent.getR() + res.getR()); sc.Rcomponent.setI(current); } void operator ‐= (Series_Circuit& sc, Resistor& res) { float current = 0. 0 F; current = sc.Rcomponent.getV() / (sc.Rcomponent.getR() ‐ res.getR()); sc.Rcomponent.setR(sc.Rcomponent.getR() ‐ res.getR()); sc.Rcomponent.setI(current); } void main() { char option = 'y'; float resistance = 0. 0 f; float current = 0. 0 f; //Initial setting cout<<"Enter the Initial Resistance (in ohms) : "; cin>>resistance; cout<<"Enter the initial Current (in amperes) : "; cin>>current; Series_Circuit circuit(resistance, current); while( 1 ) { cout<<"Add another (y/n) : "; cin>>option; if(option == 'n') break; cout<<"Enter the Resistance (in ohms) : "; cin>>resistance; Resistor Temp(resistance, 0. 0 F); circuit += Temp; }

c:\documents and settings\usman.younis\my documents... 2010\Projects\op_overload2\op_overload2\opover.cpp 1 #include using namespace std; class complex_number { private: float real_part; float imag_part; public: complex_number(): real_part( 0 ), imag_part( 0 ) { } friend void operator << (ostream& out, complex_number& N); friend void operator >> (istream& in, complex_number& N); }; void operator << (ostream& out, complex_number& N) { out<<"Complex number is : "<<endl <<N.real_part<<" + i"<<N.imag_part<<endl; } void operator >> (istream& in, complex_number& N) { cout<<"Enter a complex number (real part, imainary part) :"<<endl; in>>N.real_part>>N.imag_part; } void main() { complex_number cNumber; cin>>cNumber; cout<<cNumber; }