0% found this document useful (0 votes)
22 views6 pages

Single Inheritance

The document contains code examples demonstrating various types of inheritance in C#, including single, multilevel, hybrid, and multiple inheritance. Each section provides a class structure and methods to illustrate how inheritance allows for code reuse and organization. The examples include classes for rectangles, students, and family members, showcasing their properties and methods.

Uploaded by

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

Single Inheritance

The document contains code examples demonstrating various types of inheritance in C#, including single, multilevel, hybrid, and multiple inheritance. Each section provides a class structure and methods to illustrate how inheritance allows for code reuse and organization. The examples include classes for rectangles, students, and family members, showcasing their properties and methods.

Uploaded by

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

Single Inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace single_inheritance
{
class Rectangle
{
protected double length, width;
public void getdata()
{
Console.WriteLine("Enter the length and width of the rectangle");
length = Convert.ToDouble(Console.ReadLine());
width = Convert.ToDouble(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length:{0}",length);
Console.WriteLine("Width:{0}",width);
Console.WriteLine("Area:{0}", GetArea());
}
}// end class Rectangle
class Childclass : Rectangle
{
private double cost;
public double GetCost()
{
double cost;
cost = GetArea()*70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("Cost:{0}",GetCost());
}
}
class ExecuteSingleInheritance
{
static void Main(string[] args)
{
Childclass t =new Childclass();
t.getdata();
t.Display();
Console.ReadKey();}}}
Multilevel Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace multilevelinheritanace
{
public class Person
{
protected string regno, name;
public void get()
{
Console.WriteLine("Multilevel Inheritance!");
Console.WriteLine("Enter the register number and name of a student :-");
regno = Console.ReadLine();
name = Console.ReadLine();
}
public virtual void display()
{
Console.WriteLine("Student register number - {0} and name is {1}", regno, name);
Console.ReadLine();
}
}
class Student : Person
{
public string Classincharge = "PSV";
public override void display()
{
base.display();
Console.WriteLine("Class incharge of the Student is: {0}", Classincharge);
}
}
class Details : Student
{
private string StudentAddress = "BCA, PUCC";
public void display()
{
Console.WriteLine("Student Address: {0}", StudentAddress);
}
}
class TestClass
{
public static void Main()
{
Student s = new Student();
s.get(); s.display();
Details d = new Details();
d.display();
Console.ReadKey();
}
}
}
Hybrid Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hybridinheritanace
{
class Father
{
public void home()
{
Console.WriteLine("Father's home");
}
public void Car()
{
Console.WriteLine("Father's Car");
}
}
class Son : Father
{
public void mobile()
{
Console.WriteLine("Son's mobile");
}
}
class Daughter : Father
{
public void purse()
{
Console.WriteLine("Daughter's purse");
}
}
public class TestHybridInheritance
{
public static void Main(String[] args)
{
Son s = new Son();
s.Car();
s.home();
s.mobile();
Daughter d = new Daughter();
d.Car();
d.home();
d.purse();
Console.ReadKey();
} }}
Multiple Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace multipleinheritance
{
class Shape
{
protected int width,height;
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
}
public interface PaintCost //interface declarations
{
int getCost(int area);
}
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class Test
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
Console.WriteLine("\t \t IMPLEMENTATION OF MULTIPLE INHERITANCE \n");
Console.WriteLine("Painting Details:- \n");
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}", Rect.getCost(area));
Console.ReadKey();
}
}
}

You might also like