C++ Exercise

Hi everyone, I am in the fourth year of high school and I'm working on a program in C ++, which mixes the electrical circuits of physics with C ++. the delivery involves the creation of a program in C ++, in which the resistance and the capacitor must be inserted in two separate classes with their own attributes, methods and constructors. also in the main it is indicated to insert 3 resistors and 2 capacitors. I leave you both the original delivery and my work. "Create a program that allows the insertion of two bipoles (Resistance and Capacitor) through two non-derived classes by implementing the manufacturers the attributes and the get and set methods for each attribute. Create a main that allows you to insert 3 resistors and 2 capacitors"
at the moment I'm only working with the class of capacitors, to concentrate on the code of it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath> 
using namespace std;
class capacitors{
	public:
		double Formula();
		void GetCapacitors();
		void SetCapacitors();
		double Parallel();
		double Series();
	private:
		double AccumulatedCharge;
		double PotentialDifference; 
};
double capacitors::Formula(){
	return (abs(capacitors::AccumulatedCharge)/abs(capacitors::PotentialDifference));
}
void capacitors::SetCapacitors(){
	double charge, difference;
	cout<<"\nQ:";cin>>charge;
	do { cout<<"\nV: ";cin>>difference;
	} while(difference==0);
	AccumulatedCharge=charge;
	PotentialDifference=difference;
}
void capacitors::GetCapacitors(){
	cout <<"\nQ= "<< abs(AccumulatedCharge);
	cout <<"\nV= "<< abs(PotentialDifference);
	cout <<"\n----------------";
}

main(){
    for(int i=0; i<3;i++){
    	capacitors C;
    		cout<<"\nInsert Value:";
    		C.SetCapacitors();
    		cout<<"\nThe following values have been entered:";
    		C.GetCapacitors();
    		cout<<"\nThe capacity of the capacitors is equal to: "<<C.Formula()<<"\b F.";
   }
}

for the insertion of the 3 capacitors, through a FOR loop I can only get them displayed, but in case I want to make each value of the capacitors interact, for example the capacity of the capacitor 1 + capacitor2 + capacitor3, as I do, since they are values entered at the moment and printed?
Last edited on
Can you rephrase your question?
I don't get the purpose of your "Capacitors" class.
Is your capacitors class supposed to represent a single capacitor or multiple capacitors? It sounds like the latter. But the rest of your code doesn't really allow this.

I think you're getting bogged down by the verbosity of your class functionality.

If you want to tell the user to enter a capacitors, say, in parallel, then you need to enter 1 voltage, and then N charges.
If you want to compute the total capacitance, then you need to sum the charges, then divide by the voltage.
Last edited on
to tell you the truth I'm really confused 🥺, What I wanted to do was a class that represents a single capacitor and then through an Array, insert 2 of them. And then do the same with the resistance class.

What would you have done for me?
I have a lot of ideas in my head, but they get confused between them🤦🏻‍♂️
Last edited on
Okay, something like,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>

class Capacitor {
  public: // simplified for brevity
    double charge; 
    double voltage;
};

int main()
{
    using namespace std;

    Capacitor caps[3]; // array of 3 capacitors
    cout << "Enter voltage across the 3 capacitors in parallel: ";
    double voltage;
    cin >> voltage;

    for (int i = 0; i < 3; i++)
    {
        // ask user to enter attributes of capacitor
        cout << "Enter charge of capacitor " << i << ": ";
        cin >> caps[i].charge;
        caps[i].voltage = voltage; // already known
    }

    double total_charge = 0;
    for (int i = 0; i < 3; i++)
    {
        total_charge += caps[i].charge;
    }

    cout << "C = " << total_charge / voltage;
}

Enter voltage across the 3 capacitors in parallel: 120
Enter charge of capacitor 0: 30
Enter charge of capacitor 1: 30
Enter charge of capacitor 2: 30
C = 0.75 F
Last edited on
in the for loop
// e.g.

what means e.g?
in this case how i can implement set and get method in the for loop?
I dunno, if you want to add getter/setter boilerplate, feel free to do so.
Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Capacitor {
  public:
    double getVoltage()
    {
        return voltage;
    }
    void setVoltage(double voltage)
    {
        this->voltage = voltage;
    }

    double getCharge()
    {
        return charge;
    }
    void setCharge(double charge)
    {
        this->charge = charge;
    }

    double getCapacitance()
    {
        // Q = CV
        return charge / voltage;
    }
  private:
    double voltage;
    double charge;
};


I also edited my previous post with a bit more detail.
Last edited on
please can you explain me what you have done here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Capacitor {
  public:
    double getVoltage()
    {
        return voltage;
    }
    void setVoltage(double voltage)
    {
        this->voltage = voltage;
    }

    double getCharge()
    {
        return charge;
    }
    void setCharge(double charge)
    {
        this->charge = charge;
    }

    double getCapacitance()
    {
        // Q = CV
        return charge / voltage;
    }
  private:
    double voltage;
    double charge;
};
It's just setters/getters for things you might want from a capacitor (voltage, charge, calculating the capacitance).

I suggest looking at a tutorial on classes if this doesn't make sense.
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/classes/
thank you very much, now I read and then I try to do it again, if I have new problems I will rewrite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;
class capacitator{
	public:
		double GetCharge(){
				return charge;
		};
		void SetCharge(){
			cout<<"Charge: "; cin>>charge;
		};
		double GetVoltage(){
			return voltage;
		};
		void SetVoltage(){
			cout<<"Voltage: "; cin>>voltage;
		};
		double GetCapacity(){
			return charge/voltage;
		};
		double charge;
		double voltage;
};

main(){
double voltage;
	capacitator c[2];
    		cout<<"\nEnter voltage:";    	
    		cin>>voltage;
		    for(int i=0; i<3;i++){
				cout<<"\nEnter charge of capacitator"<<i<<": ";
      		  	cin>>c[i].charge;
      		 	c[i].voltage=voltage;
    double TotalCharge=0;
    	for (int i=0; i<3; i++){
       	 	TotalCharge += c[i].GetCharge();
   		}

    cout<<"\nC="<<TotalCharge/voltage;
   }

}


I managed to set it this way here, correct?
Last edited on
Perché hai iniziato a parlare italiano? :)

è corretto
No.

A few issues:
1. The signature of your main function should be int main(), not just main().

2. On line 26, you declare an array of size 2, but you loop over 3 elements. Make your array size be 3 instead of 2.

3. It sounds like you're supposed to use getters/setters, but you don't use your setters. The way you have it isn't wrong, but it's peculiar. You never use your SetCharge or SetVoltage functions.

Other than that... it looks okay.
Last edited on
Perché hai iniziato a parlare italiano? :)
because I am Italian, here the forums are not very good, so I preferred to focus on the American ones 😅
2.

I remember that with Arrays "formula" N-1 was used in the Array dimension because they start from 0, in any case, I will fix it.
3.

How should I use them? in case of SetVoltage I should insert it on line 27/28?
And SetCharge on line 31? "c[i].SetCharge"?
When you declare an array, the number itself is the size of the array.

int arr[5]; allocates 5 ints.
The tricky part is that the first index is 0. So it's 5 ints, but the indices are 0, 1, 2, 3, 4.
That's where the "N-1" thing comes from.

In your SetX functions, you force the user to input data.
Instead, you should do something more like

1
2
3
4
5
double charge;
cin >> charge;
c[i].SetCharge(charge); // each capacitor has a different charge

c[i].SetVoltage(voltage); // each capacitor has the same voltage (in parallel) 


And in your class, SetCharge can look like:
1
2
3
4
5
6
7
8
void SetCharge(double charge)
{
    this->charge = charge;
}
void SetVoltage(double voltage)
{
    this->voltage = voltage;
}
Last edited on
Ok thanks, i've settled all. for resistances, I should work in a similar way right?
The charge and voltage across a capacitor are functions of the circuit that they are in, not the capacitor itself. So I would store the capacitance in the Capacitor class.

Similarly, I'd store the resistance in the Resistor class.

Create a main that allows you to insert 3 resistors and 2 capacitors"

Should they be inserted in a fixed configuration? Or can the user insert them in any combination of series & parallel?

What should be the output of the program? Total capacitance? Resistance? Impedance?

The charge and voltage across a capacitor are functions of the circuit that they are in, not the capacitor itself. So I would store the capacitance in the Capacitor class.
Heh true that makes a lot more sense, actually. I guess I was just trying to go off of the class OP had already established (sort of).
so the operation that should be done is to move the part of the total capacity, inside a function of the capacitor class, right?
Should they be inserted in a fixed configuration? Or can the user insert them in any combination of series & parallel?

it is not specified, but I continue with a parallel circuit, the ideal would be to make the user decide which circuit they are in, but I see it as too complex for my knowledge😅
What should be the output of the program? Total capacitance? Resistance? Impedance?

That's right, those are the output data, momentarily I only have that of the capacitor, but I will add the others.

EDIT:
a friend told me that I should put instead of C = Q / V, Q = C * V, so in case you want to insert 1microFarad there is no need to calculate it by hand, is worth?
Last edited on
Topic archived. No new replies allowed.