0

Help with polymorphism

I need help with this exercise. In a turn-based strategy game, each unit can attack. The standard unit attacks with a sword. But there are two more types of units - musketeers and mages, who attack in their own way. The program you are given declares a Unit class that has an Attack () method. This generates "Using sword!". Derive the Musketeer and Magician classes from the Unit class and override their Attack () method to generate the corresponding messages while attacking: Musketeer => "Using musket!" Magician => "Using magic!" Don't forget about the virtual keyword, which allows you to override the method in derived classes. This is my code: https://code.sololearn.com/cSFPE5IV5QIn/?ref=app

8th Jul 2021, 6:14 PM
Alex Narváez
Alex Narváez - avatar
8 Respostas
+ 2
Alex you just need to add the complete string that you are looking for.So do like this "Using sword!" for all of it. Your code is working !!
8th Jul 2021, 6:28 PM
JRAMAHES
JRAMAHES - avatar
+ 2
class Unit { public virtual void Attack() { Console.WriteLine("Using sword!"); } } /*derive the class from Unit class and override Attack() method*/ class Musketeer : Unit { public override void Attack() { Console.WriteLine("Using musket!"); } } /*derive the class from Unit class and override Attack() method*/ class Magician : Unit { public override void Attack() { Console.WriteLine("Using magic!"); } } }
24th Jan 2023, 9:00 PM
Melta
+ 1
I have no idea about this exercise but what output you are looking for ?
8th Jul 2021, 6:23 PM
Abhay
Abhay - avatar
+ 1
the test expect "Using musket!" and "Using magic!", but you forgot "Using " in derived class attack output: Console.WriteLine("Using musket!"); Console.WriteLine("Using magic!");
8th Jul 2021, 6:32 PM
visph
visph - avatar
0
In the description of the exercise is the result that should have
8th Jul 2021, 6:26 PM
Alex Narváez
Alex Narváez - avatar
0
{ :: ( [ " JRamoneMH " ] ) ;; }; Thank you very much for helping me see what I was missing
8th Jul 2021, 6:32 PM
Alex Narváez
Alex Narváez - avatar
0
visph Thanks, I was missing only that detail. And thanks also for helping me
8th Jul 2021, 6:33 PM
Alex Narváez
Alex Narváez - avatar
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Unit unit1 = new Unit(); Unit musketeer = new Musketeer(); Unit magician = new Magician(); unit1.Attack(); musketeer.Attack(); magician.Attack(); } } class Unit { public virtual void Attack() { Console.WriteLine("Using sword!"); } } /*derive the class from Unit class and override Attack() method*/ class Musketeer : Unit { public override void Attack() { Console.WriteLine("Using musket!"); } } /*derive the class from Unit class and override Attack() method*/ class Magician : Unit { public override void Attack() { Console.WriteLine("Using magic!"); } } }
25th Mar 2024, 4:43 PM
Mahima Rathore
Mahima Rathore - avatar