Docsity

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

Toggle sidebar
bg1
1c:\documentsandsettings\usman.younis\my...2010\Projects\polymorph2\polymorph2\polymorph2.cpp
1#include<iostream>
2
3usingnamespacestd;
4
5
6//AbstractClassResistor
7classResistor
8{
9protected:
10 intNumR;
11 float*R;
12
13 public:
14 Resistor(intn)
15 {
16 NumR=n;
17 R=newfloat[n];
18 }
19
20 voidset
_
resistance(floata[])
21 {
22 for(inti=0;i<NumR;i++)
23 R[i]=a[i];
24 }
25
26 virtual~Resistor()
27 {
28 delete[]R;
29 }
30
31 virtualfloattotal
_
resistance()=0;
32 };
33 //ResistorEnds
34
35 classSeries:publicResistor
36 {
37 public:
38 Series(inta,floatb[]):Resistor(a)
39 {
40 set
_
resistance(b);
41 }
42
43 floattotal
_
resistance()
44 {
45 floatTotal=0.0F;
46 for(inti=0;i<NumR;i++)
47 Total+=R[i];
48
49 returnTotal;
50 }
51
52 ~Series(){}
53 };
54
55 classParallel:publicResistor
56 {
57 public:
58 Parallel(inta,floatb[]):Resistor(a)
59 {
60 set
_
resistance(b);
61 }
62
63 floattotal
_
resistance()
64 {
65 floatTotal=0.0F;
66 floatHold=0.0F;
docsity.com
pf3

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!

1 #include 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;

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) : ";