Inheritance- in c#
# Inheritance-
1.it
is a reusability of class
2.
Types of class-
There
are two types of class
a.Based
class
b.
Derived class
Ex-
example of base class
Class
a
Class
b
Class
c
Class
d
A,b,c is called base class(all the
previous class is called base class)
Ex. Of derived class b,c,d (the class
which is after the base is called derived).
·
Rule
for inheritances:-
1.
Inheritance cannot count the main class.
2.
The object is created of the last class.
3.In
inheritances we used classaccessbility operator – (:)
#
Type of inheritances:-
1.Single
inheritances- (1 base & 1 derived
2.Multilevel
inheritances
3.Multiple
inheritances (not supported )
#
Structures of inheritances-
Derivedclassname
: baseclassname
1.
Single level inheritances
Ex-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
Inheritancesprog
{
class A
{
public void display()
{
Console.WriteLine("Welcome");
}
}
class b : A // This is inheritances
{
public void show()
{
Console.WriteLine("nagpur");
}
}
class Program
{
static void Main(string[] args)
{
b b1 = new b();
b1.show();
b1.display();
Console.ReadKey();
}
}
}
2.Multi
level inheritances:-
In multi level inheritances derived class
access its own based class.
Ex. Class a
Class b:a
Class c:b
Class d:c
Ex for multiple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inheritancesprog
{
class A
{
public void display()
{
Console.WriteLine("Welcome");
}
}
class b : A // This is
inheritances
{
public void show()
{
Console.WriteLine("nagpur");
}
}
class c:b
{
public void print()
{
Console.WriteLine("to");
}
}
class Program
{
static void Main(string[] args)
{
c b1 = new c();
b1.show();
b1.print();
b1.display();
Console.ReadKey();
}
}
}
Task 13 :- Write a program to
implement inheritances logic
Create a class- student
Create a method- input
Create a parameter- roll name,city,branch,m1,m2,m3,m4,m5
Create another class – college
Create a method- grade
Create a parameter total
Create another class -result
Create a method -display
Logic 1 all parameters given by the user
using named parameter
Logic 2
grade method only accepted total parameter
check condition inside grade method
total>250 a grade
total>150 & total<250 b grade
otherwise fail
if students is fail then cannot display
student info
if student generated a specify grade then
display all information with the help of result class
Comments
Post a Comment