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

C++ Code for Calculating Total Resistance in Series and Parallel Circuits, Exercises of Object Oriented Programming

This c++ code defines two classes, series and parallel, which inherit from the abstract class resistor. The classes calculate the total resistance for series and parallel circuits respectively. The user inputs the number of resistances and their values, and the code determines the type of circuit to calculate. The main function initializes the circuit type, inputs the number of resistances and their values, and outputs the total resistance.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

netu
netu 🇮🇳

4.5

(4)

55 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Code for Calculating Total Resistance in Series and Parallel Circuits and more Exercises Object Oriented Programming in PDF only on Docsity! 1c:\documents and settings\usman.younis\my ...2010\Projects\polymorph2\polymorph2\polymorph2.cpp 1  # include  < iostream > 2 3  using namespace std ; 4 5 6  // Abstract Class Resistor 7  class Resistor 8  { 9  protected : 10    int NumR ; 11    float *  R ; 12 13  public : 14    Resistor ( int n ) 15    { 16      NumR  =  n ; 17      R  =  new float [ n ] ; 18    } 19 20    void set _ resistance ( float a []) 21    { 22      for ( int i  =  0 ;  i  <  NumR ;  i ++) 23        R [ i ] =  a [ i ] ; 24    } 25 26    virtual  ~ Resistor () 27    { 28      delete []  R ; 29    } 30 31    virtual float total _ resistance () =  0 ; 32  }; 33  // Resistor Ends 34 35  class Series :  public Resistor 36  { 37  public : 38    Series ( int a ,  float b []):  Resistor ( a ) 39    { 40      set _ resistance ( b ) ; 41    } 42 43    float total _ resistance () 44    { 45      float Total  =  0 . 0 F ; 46      for ( int i  =  0 ;  i  <  NumR ;  i ++) 47        Total  +=  R [ i ] ; 48 49      return Total ; 50    } 51 52    ~ Series ()  {} 53  }; 54 55  class Parallel :  public Resistor 56  { 57  public : 58    Parallel ( int a ,  float b []):  Resistor ( a ) 59    { 60      set _ resistance ( b ) ; 61    } 62 63    float total _ resistance () 64    { 65      float Total  =  0 . 0 F ; 66      float Hold  =  0 . 0 F ; docsity.com 2c:\documents and settings\usman.younis\my ...2010\Projects\polymorph2\polymorph2\polymorph2.cpp 67 68      for ( int i  =  0 ;  i  <  NumR ;  i ++) 69        Hold  +=  1 . 0 F  /  R [ i ] ; 70 71      Total  =  1 . 0 F  /  Hold ; 72 73      return Total ; 74    } 75 76    ~ Parallel ()  {} 77  }; 78 79  void main () 80  { 81    Resistor *  ptr ; 82    int choice  =  0 ; 83 84    int Num  =  0 ; 85    float *  Temp ;  86 87    char option ; 88 89    while ( 1 ) 90    { 91      cout << " Which circuit you want to calculate  ( 1 :  Series ,  2 :  Parallel )  \ n " ; 92      cin >> choice ; 93     94      if ( choice  !=  1  &&  choice !=  2 ) 95      { 96        cout << " Not a Valid choice  \ n " ; 97        cin . clear () ; 98        cin . ignore ( INT _ MAX , '\ n ' ) ;  99        continue ; 100      } 101 102      cout << " How many resistances are there in your circuit :  \ n " ; 103      cin >> Num ; 104 105      Temp  =  new float [ Num ] ; 106 107      cout << " Enter the resitance of each resistor :  \ n " ; 108      for ( int i  =  0 ;  i  <  Num ;  i ++) 109      { 110        cout << i + 1 << " : " ; 111        cin >> Temp [ i ] ; 112      } 113 114      switch ( choice ) 115      { 116      case  1 : 117        ptr  =  new Series ( Num ,  Temp ) ; 118        cout << " Total resistance is  : " << ptr ‐ > total _ resistance () << endl ; 119        delete ptr ; 120        break ; 121 122      case  2 : 123        ptr  =  new Parallel ( Num ,  Temp ) ; 124        cout << " Total resistance is  : " << ptr ‐ > total _ resistance () << endl ; 125        delete ptr ; 126        break ; 127      } 128 129      delete []  Temp ; 130 131 132      cout << " Another  ( y / n ) : " ; docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved