Create a class animal Contains methodsSpeak Message box “grrrr” Move Message box
ID: 3838093 • Letter: C
Question
Create a class animal
Contains methodsSpeak
Message box “grrrr”
Move
Message box “location changed”
Create a class pet
Inherits from animal
Contains
name field
readonly name property
single parameter constructor which will take in a name string
sets the name field
Create dog class
inherits from animal
contains methodsspeak
message box “woof”
move
message box “obediently changes position”
Create dog class
inherits from animal
contains methodsspeak
message box “meow”
move
message box “glares at you and stays put”
Create a method on the form
move
takes in an animal class
calls the move method of the object
Create a form that will have buttons
dog speak
create a dog and make it speak
cat speak
create a cat and make it speak
dog move
create a dog and call the form’s move method sending in the dog
cat move
create a cat and call the form’s move method sending in the cat
need answer in visual studio(C#) its inheritance and polymorphism
please answer ASAP with simple and easy code.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace W3___MDORFMAN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Function
// Recieves Animal Class
// Calls Animal Class Move Function
public void move(Animal NewAnimal)
{
NewAnimal.Move();
}
// Button Click Event
// Creates New Dog
// New Dog Calls Function Speak
private void btnDogSpeak_Click(object sender, EventArgs e)
{
Dog DSPEAK = new Dog();
DSPEAK.Speak();
}
// Button Click Event
// Creates New Dog
// New Dog is passed to move function (in form)
private void btnDogMove_Click(object sender, EventArgs e)
{
Dog DMOVE = new Dog();
move(DMOVE);
}
private void btnCatSpeak_Click(object sender, EventArgs e)
{
Cat CSPEAK = new Cat();
CSPEAK.Speak();
}
private void btnCatMove_Click(object sender, EventArgs e)
{
Cat CMOVE = new Cat();
move(CMOVE);
}
}
public class Animal
{
public virtual void Speak()
{
MessageBox.Show("Grrrrr.....");
}
public virtual void Move()
{
MessageBox.Show("Location Changed");
}
}
public class Pet : Animal
{
// Field
private string _name;
// Get Only (Get = Read)
public string name
{
get { return _name; }
}
// Constructor (With Parameter)
public Pet(string name)
{
_name = name;
}
}
public class Dog : Animal
{
public override void Speak()
{
MessageBox.Show("Bork");
}
public override void Move()
{
MessageBox.Show("obediently changes position");
}
}
public class Cat : Animal
{
public override void Speak()
{
MessageBox.Show("Meow");
}
public override void Move()
{
MessageBox.Show("glares at you and stays put");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.