03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

An interface can be created for this circuit <strong>to</strong> define the components of the circuit<br />

(r1 and r2), and the method (calcParallel() and calcSeries()) with:<br />

interface NewCircuit<br />

{<br />

double r1 { <strong>set</strong>; get;}<br />

double r2 { <strong>set</strong>; get;}<br />

double calcParallel();<br />

double calcSeries();<br />

}<br />

which defines that the base class has two properties and two methods. The derived<br />

class can then be defined with:<br />

public class Circuit: NewCircuit<br />

{<br />

private double res1, res2;<br />

}<br />

public double r1<br />

{<br />

<strong>set</strong> { res1=value;}<br />

get { return res1; }<br />

}<br />

public double r2<br />

{<br />

<strong>set</strong> { res2=value;}<br />

get { return res2; }<br />

}<br />

public double calcParallel()<br />

{<br />

return((r1*r2)/(r1+r2));<br />

}<br />

public double calcSeries()<br />

{<br />

return(r1+r2);<br />

}<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

and called with:<br />

Circuit cir = new Circuit();<br />

cir.r1=1000;<br />

cir.r2=1000;<br />

double res = cir.calcParallel();<br />

The full program is given in Program 8.1<br />

Agilent .NET Course: More Objects 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!