Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following code has a problem with polymorphism. I keep getting a runtime error. Error: An unhandled exception of type System.InvalidCastException occured in polymorphism.exe Apparently,

The following code has a problem with polymorphism. I keep getting a runtime error.

Error: "An unhandled exception of type System.InvalidCastException occured in polymorphism.exe"

Apparently, I need one line of code to fix it.

C# Code:

class Sensor

{

private string sensorName;

public Sensor(string _name)

{

sensorName = _name;

}

public virtual void ActionType()

{

Console.WriteLine("Sensor Detect Nothing.");

}

}

class SmokeSensor : Sensor

{

private string type;

public SmokeSensor(string _type, string _name) : base(_name)

{

type = _type;

}

public override void ActionType ()

{

Console.WriteLine("Somke Sensor Detect Smoke.");

}

}

class Program

{

static void Main(string[] args)

{

Sensor super1, super2;

SmokeSensor sub1, sub2;

super1 = new Sensor("Sensor");

sub1 = new SmokeSensor("Smoke", "Smoke Sensor");

super2 = super1;

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \\Tip: this is where the code should go.

sub2 = (SmokeSensor)super1;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions

Question

Why do firms use cross-border strategic alliances? Discuss.

Answered: 1 week ago

Question

What is disclosure of cost method investment?

Answered: 1 week ago