Reputation: 57
In main, I need to add the int dodatkowaWartosc to the variable liczbaGlosow after showing all the parameters of Kandydat, so first it shows some amount of liczbaGlosow and then it shows the new value with added dodatkowaWartosc, but I can't figure out how to do that with overloading operators, and I have to do that with them.
class Kandydat
{
private string imie, nazwisko, miejscowosc, wyksztalcenie;
public int liczbaGlosow { get; set; }
public Kandydat(string imie, string nazwisko, string miejscowosc, string wyksztalcenie, int liczbaGlosow)
{
this.imie = imie;
this.nazwisko = nazwisko;
this.miejscowosc = miejscowosc;
this.wyksztalcenie = wyksztalcenie;
this.liczbaGlosow = liczbaGlosow;
}
public void PokazKandydata()
{
Console.WriteLine("{0} {1} z {2}, wykształcenie {3}. Liczba głosów: {4}.", imie, nazwisko, miejscowosc, wyksztalcenie, liczbaGlosow);
}
public static Kandydat operator +(Kandydat liczbaGlosow, int dodatkowaWartosc)
{
return new Kandydat.liczbaGlosow + dodatkowaWartosc;
}
Upvotes: 0
Views: 46
Reputation: 37059
Don't forget that this does not alter the first Kandyat
:
var k = new Kandyat(...);
k.liczbaGlosow = 1
var l = k + 2;
// k.liczbaGlosow is still 1
// l is a new, different instance of Kandyat, and l.liczbaGlosow == 3
Upvotes: 1