Seeking experts in Overloading Operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Seeking experts in Overloading Operator

I did one of the practical. I am so clueless in this operator overloading. My results fail. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Score tm1 = new Score(2, 3); Score tm2 = new Score(4, 2); Score finalScores = tm1 + tm2; Console.WriteLine("Round 1: " + finalScores.round1Score); Console.WriteLine("Round 2: " + finalScores.round2Score); } } class Score { public int round1Score { get; set; } public int round2Score { get; set; } public Score(int r1, int r2) { round1Score = r1; round2Score = r2; } //your code goes here public static Score operator+(Score a, Score b) { int r1 = a.r1 + b.r1; int r2 = a.r2 + b.r2; Score res = new Score (r1, r2); return res; } } }

6th Jan 2021, 4:24 AM
Faris Mohamed
Faris Mohamed - avatar
8 Answers
+ 8
Your class attributes are : round1Score and round2Score There is no definition for r1 and r2 in your class. So you can't access them. Write it like, int r1 = a.round1Score + b.round1Score; int r2 = a.round2Score + b.round2Score; Hope it helps! P.S. I'm not an expert 😝
6th Jan 2021, 4:56 AM
Minho
Minho - avatar
+ 6
Lol 😆 Glad I could help 🙏
6th Jan 2021, 5:03 AM
Minho
Minho - avatar
+ 3
This is the best solution after // drop code under this public static Score operator+(Score a, Score b) { int r1 = a.round1Score + b.round1Score; int r2 = a.round2Score + b.round2Score; Score res = new Score (r1, r2); return res; }
21st Dec 2022, 12:43 AM
Muhammad Tariq Muhammad Muhammad Almanzalawy
Muhammad Tariq Muhammad Muhammad Almanzalawy - avatar
+ 2
omg. You are really awesome!
6th Jan 2021, 4:57 AM
Faris Mohamed
Faris Mohamed - avatar
+ 1
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Score tm1 = new Score(2, 3); Score tm2 = new Score(4, 2); Score finalScores = tm1 + tm2; Console.WriteLine("Round 1: " + finalScores.round1Score); Console.WriteLine("Round 2: " + finalScores.round2Score); } } class Score { public int round1Score { get; set; } public int round2Score { get; set; } public Score(int r1, int r2) { round1Score = r1; round2Score = r2; } public static Score operator+(Score a, Score b) { int r1 = a.round1Score + b.round1Score; int r2 = a.round2Score + b.round2Score; Score res = new Score (r1, r2); return res; } } }
11th Aug 2023, 6:34 PM
Parthasarathi Panda
Parthasarathi Panda - avatar
0
Operator Overloading You and your friend are playing a game as one team. Each player must pass 2 rounds and gets points for each round passed. The program you are given creates two Score objects where each round scores are stored (they are passed to a constructor). Overload the + operator for the Score class to calculate the team score for every round.
6th Jan 2021, 4:24 AM
Faris Mohamed
Faris Mohamed - avatar
0
😭😭😭 not run
10th Oct 2022, 8:14 PM
claudio presciutti
claudio presciutti - avatar
0
I add public static Score operator+ (score a, score b){ int a = a.round1Score + b.round1Score; int b = a.round2Score + b.round2Score; Score res = new Score(a,b);} return res;} Under the //line But not run.. any suggestion ??
10th Oct 2022, 8:15 PM
claudio presciutti
claudio presciutti - avatar