Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Objective of this cours is to develop effective computer programming skills in solving complex problems and to learn adequate and operational software organization in developing real life engineering solutions using powerful object oriented paradigm of the language. It includes: Include, Class, Private, Float, Public, Void, Friend, Operator, Resistor, Return

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

netu
netu 🇮🇳

4.5

(4)

55 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1C:\DocumentsandSettings\usman.younis\MyDocuments...\Projects\op_overload\op_overload\op_overload.cpp
#include<iostream>
usingnamespacestd;
classResistor
{
private:
floatR;
float
V
;
floatI;
public:
Resistor(){}
Resistor(floatr,floati):R(r),I(i)
{
V=I*R;
}
voidsetI(floati)
{
I=i;
V=I*R;
}
voidsetR(floatr)
{
R=r;
V=I*R;
}
floatgetR(){returnR;}
floatgetI(){returnI;}
floatget
V
(){return
V
;}
friendResistoroperator+(Resistor&r1,Resistor&r2);
};
Resistoroperator+(Resistor&r1,Resistor&r2)
{
floatI=r1.getI()+r2.getI();
floatR=r1.getR()+r2.getR();
returnResistor(R,I);
}
classSeries
_
Circuit
{
private:
ResistorRcomponent;
public:
Series
_
Circuit():Rcomponent(0.0F,0.0F){}
Series
_
Circuit(floatr,floati):Rcomponent(r,i){}
voidshow
_
circuit()
{
cout<<"ThefinalcircuitResistanceis:"<<
Rcomponent.getR()<<endl;
cout<<"Voltageacrossthecircuitis:"<<
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; }