0% found this document useful (0 votes)
14 views5 pages

Assignment c#53

Uploaded by

ghadgejigar9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Assignment c#53

Uploaded by

ghadgejigar9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name :- Sahil rajen ra ka am

Roll.no : A 50
Class : Bca sem5
Batch : A
Subject : C#

Assignment C#
➢ Single level inheritance:
using System;
class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
public void Sleep()
{
Console.WriteLine($"{Name} is sleeping.");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} is barking.");
}
}
class Program
{ Output:
static void Main()
{
// Create an instance of the base class
Animal myAnimal = new Animal
{
Name = "Generic Animal",
Age = 5
};
// Use methods from the base class

Console.WriteLine($"{myAnimal.Name} is
{myAnimal.Age} years old.");
myAnimal.Eat();
myAnimal.Sleep();
// Create an instance of the derived class
Dog myDog = new Dog
{
Name = "Buddy",
Age = 3
};
// Use methods from both the base and
derived classes
Console.WriteLine($"{myDog.Name}
is {myDog.Age} years old.");
myDog.Eat();
myDog.Sleep();
myDog.Bark();
}
}
➢ Multi-level inheritance:
using System; class Program
class Animal {
{ static void Main()
public string Name { get; set; } {
public Animal(string name) Dog myDog = new Dog("Buddy");
{ Console.WriteLine($"{myDog.Name}
Name = name; is a dog.");
} myDog.Eat();
public void Eat() myDog.Sleep();
{ myDog.GiveBirth();
Console.WriteLine($"{Name} is myDog.Bark();
eating."); }
} }
public void Sleep()
{
Console.WriteLine($"{Name} is
Output:
sleeping.");
}
}
class Mammal : Animal
{
public Mammal(string name) :
base(name)
{
}
public void GiveBirth()
{
Console.WriteLine($"{Name} is
giving birth to live young.");
}
}
class Dog : Mammal
{
public Dog(string name) : base(name)
{
}
public void Bark()
{
Console.WriteLine($"{Name} is
barking.");
}
}
{
➢ Hierarchical inheritance:
using System;
class Animal
Console.WriteLine($"{Name} is
{
barking.");
public string Name { get; set; }
}
}
public Animal(string name)
{
class Program
Name = name;
{
}
static void Main()
{
public void Eat()
Cat myCat = new Cat("Whiskers");
{
Dog myDog = new Dog("Buddy");
Console.WriteLine($"{Name} is
eating.");
Console.WriteLine($"{myCat.Name} is
}
a cat, and {myDog.Name} is a dog.");
myCat.Eat();
public void Sleep()
myCat.Sleep();
{
myCat.Meow();
Console.WriteLine($"{Name} is
myDog.Eat();
sleeping.");
myDog.Sleep();
}
myDog.Bark();
}
}
}
class Cat : Animal
{
public Cat(string name) : base(name)
{ Output:
}

public void Meow()


{
Console.WriteLine($"{Name} is
meowing.");
}
}

class Dog : Animal


{
public Dog(string name) : base(name)
{
}

public void Bark()


Program to Add any 3 control
}

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;

amespace WindowsFormsApp2

public partial class Form1: Form Output:


{

public Form1()

InitializeComponent();

ListBox ListItem = new ListBox();

private void Form1_Load(object sender, EventArgs e)

ListItem.Location = new System.Drawing.Point(15, 1

5);

ListItem.Name = "ListItem";

ListItem.Size = new System.Drawing.Size(350, 400);

ListItem.BackColor = System.Drawing.Color.Orange;

ListItem.ForeColor = System.Drawing.Colo lack;

ListItem.Items.Add("Vaishali Tyagi");

ListItem.Items.Add("Samlesh Tyagi");

ListItem.Items.Add("Preeti Tyagi");

ListItem.Items.Add("Priyanka Tyagi");

this.Controls.Add(ListItem);

You might also like