Skip to content

Instantly share code, notes, and snippets.

@janell-baxter
Created June 14, 2018 19:38
Show Gist options
  • Save janell-baxter/4e593fe89ebede781baf7d5dce308829 to your computer and use it in GitHub Desktop.
Save janell-baxter/4e593fe89ebede781baf7d5dce308829 to your computer and use it in GitHub Desktop.
Inheritance example for Introduction to Programming
using System;
using static System.Console;
namespace UnderWaterWorld
{
class Angler: Creature
{
public Angler(string _name, string _greeting) : base(_name, _greeting)
{
name = _name;
greeting = _greeting;
}
public Angler(string _name, string _greeting, ConsoleColor _color)
{
name = _name;
greeting = _greeting;
color = _color;
}
}
}
using System;
using static System.Console;
namespace UnderWaterWorld
{
class Creature
{
protected string name = "";
protected int size = 0;
protected string greeting = "hello";
protected ConsoleColor color = ConsoleColor.Cyan;
public Creature()
{
}
public Creature (string _name, string _greeting)
{
name = _name;
greeting = _greeting;
}
public Creature(string _name, string _greeting, ConsoleColor _color)
{
name = _name;
greeting = _greeting;
color = _color;
}
public void Talk()
{
Write(name + " says: ");
ForegroundColor = color;
Write(greeting + "\n");
ResetColor();
}
public void Eat(string food)
{
WriteLine(name + " eats " + food + "\n");
}
}
}
using System;
using static System.Console;
namespace UnderWaterWorld
{
class Octopus: Creature
{
public Octopus(string _name, string _greeting) : base(_name, _greeting)
{
name = _name;
greeting = _greeting;
}
public Octopus(string _name, string _greeting, ConsoleColor _color)
{
name = _name;
greeting = _greeting;
color = _color;
}
}
}
namespace UnderWaterWorld
{
class Program
{
static void Main()
{
World world = new World();
}
}
}
using System;
namespace UnderWaterWorld
{
class Shark: Creature
{
public Shark(string _name, string _greeting) : base(_name, _greeting)
{
name = _name;
greeting = _greeting;
}
public Shark(string _name, string _greeting, ConsoleColor _color)
{
name = _name;
greeting = _greeting;
color = _color;
}
}
}
using System;
using System.Collections.Generic;
using static System.Console;
namespace UnderWaterWorld
{
class World
{
//create instances of the creatures
Creature sam, dolly, fig, jack, violet, petunia;
//create a list to store the creature instances
List<Creature> creatures = new List<Creature>();
//instance of random
Random random = new Random();
//array of strings - food items
string[] food = new string[] {"yogurt", "pineapple", "gold fish crackers", "broccoli", "an old shoe", "fish food", "a soggy peanut butter and jelly sandwich" };
public World()
{
Title = "Under Water World Inheritance Example";
//constructors are overloaded: pass in 2 or 3 arguments
fig = new Octopus("Fig", "Greetings");
jack = new Octopus("Jack", "Greetings", ConsoleColor.Yellow);
violet = new Angler("Violet", "Greetings", ConsoleColor.Magenta);
petunia = new Angler("Petunia", "Greetings");
sam = new Shark("Sam", "Rawwwwr", ConsoleColor.Red);
dolly = new Shark("Dolly", "Yippeeeeeee");
//add instances to a list
creatures.Add(sam);
creatures.Add(dolly);
creatures.Add(fig);
creatures.Add(jack);
creatures.Add(violet);
creatures.Add(petunia);
//for each instance in the creatures list,
//call the methods Talk and Eat,
//and pass into Eat a random string from the food array
foreach(Creature c in creatures)
{
c.Talk();
c.Eat(food[random.Next(food.Length)]);
}
//keep the window open
ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment