0% found this document useful (0 votes)
2 views

20# Inheritance Base & Derived Object Oriented Programming

The document presents a C# implementation of object-oriented programming concepts, specifically inheritance, with classes representing people and enemies. It includes a hierarchy of classes such as Person, Toddler, and Child, as well as Enemy, BasicEnemy, and FlyingEnemy, demonstrating methods for introducing themselves and performing actions. The code showcases the creation of instances and method calls to illustrate the functionality of these classes.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

20# Inheritance Base & Derived Object Oriented Programming

The document presents a C# implementation of object-oriented programming concepts, specifically inheritance, with classes representing people and enemies. It includes a hierarchy of classes such as Person, Toddler, and Child, as well as Enemy, BasicEnemy, and FlyingEnemy, demonstrating methods for introducing themselves and performing actions. The code showcases the creation of instances and method calls to illustrate the functionality of these classes.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

20# Inheritance Base & Derived Object Oriented Programming

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.name = "David";
p.sex = "Male";
p.age = 21;
p.introduceSelf();

Toddler t = new Toddler();


t.name = "Alenere";
t.sex = "Female";
t.age = 1;
t.introduceSelf();
t.cry();
}
}
class Person
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }

public void introduceSelf()


{
Console.WriteLine("Name : " + name);
Console.WriteLine("Sex : " + sex);
Console.WriteLine("Age : " + age);
}
}
class Toddler : Person
{
public void cry()
{
Console.WriteLine(name + " : Crying");
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("David","Male",21);
p.introduceSelf();
Toddler t = new Toddler("Alenere","Female",1,"Peek-a-Boo");
t.introduceSelf();
t.cry();

Child c = new Child("Jaymar","Male",5,"Valorant","Sleeping");


c.introduceSelf();
c.cry();
c.doHobby();
}
}
class Person
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }

public Person(string name, string sex, int age)


{
this.name = name;
this.sex = sex;
this.age = age;
}

public void introduceSelf()


{
Console.WriteLine("Name : " + name);
Console.WriteLine("Sex : " + sex);
Console.WriteLine("Age : " + age);
}
}
class Toddler : Person
{
public string faveGame { get; set; }
public Toddler(string name, string sex, int age, string faveGame) :
base(name, sex, age)
{
this.faveGame = faveGame;
}
public void cry()
{
Console.WriteLine(name + " : Crying");
}
}
class Child : Toddler
{
public string hobby { get; set; }
public Child(string name, string sex, int age, string faveGame, string
hobby) : base(name, sex, age, faveGame)
{
this.hobby = hobby;
}

public void doHobby()


{
Console.WriteLine(name + " is " + hobby);
}
}
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("David", "Male", 21);
Toddler t = new Toddler("Alenere", "Female", 1, "Peek-a-Boo");
}
}
class Person
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }

public Person(string name, string sex, int age)


{
this.name = name;
this.sex = sex;
this.age = age;
}
public void introduceSelf()
{
Console.WriteLine("Name : " + name);
Console.WriteLine("Sex : " + sex);
Console.WriteLine("Age : " + age);
}
}
class Toddler : Person
{
public string faveGame { get; set; }
public Toddler(string name, string sex, int age, string faveGame) :
base(name, sex, age)
{
this.faveGame = faveGame;
}
public void introduceSelf()
{
base.introduceSelf();
Console.WriteLine("Favorite Game : " + faveGame);
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

/////////////
//Enemy.cs//
//\\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class Enemy
{
public string name { get; set; }
public int health { get; set; }

public Enemy(string name, int health)


{
this.name = name;
this.health = health;
}
public void attack()
{
Console.Write("Attacking ");
}
public void die()
{
Console.WriteLine(name + " is Dead");
}
}
}

//////////////////
//BasicEnemy.cs//
//\\\\\\\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class BasicEnemy : Enemy
{
public int walkSpeed { get; set; }
public BasicEnemy(string name, int health,int walkSpeed) : base(name,
health)
{
this.walkSpeed = walkSpeed;
}
public void attack()
{
base.attack();
Console.WriteLine("on the Ground");
}
public void walk()
{
Console.WriteLine(name + " is Walking!");
}
}
}

///////////////////
//FlyingEnemy.cs//
//\\\\\\\\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class FlyingEnemy : Enemy
{
public int flightSpeed { get; set; }
public FlyingEnemy(string name, int health, int flightSpeed) : base(name,
health)
{
this.flightSpeed = flightSpeed;
}
public void attack()
{
base.attack();
Console.WriteLine("on the Air");
}
public void fly()
{
Console.WriteLine(name + " is Flying");
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
BasicEnemy be = new BasicEnemy("Bad", 100, 5);
be.attack();
be.walk();
be.die();
FlyingEnemy fe = new FlyingEnemy("Flight", 25, 10);
fe.attack();
fe.fly();
fe.die();
}
}
}

You might also like