Consider
an example of a BASE Class University
with the protected data members that are student name and roll number. Create a derived class with name
SE and initialize student name and roll
number using get and set accessors through a derive class object. Make sure that the student name cannot be null
and roll number cannot be negative in this case.
using System;
class Uni
{
protected int RN;
protected string name;
}
class SE : Uni
{
public int setRN
{
get
{
return RN;
}
set //set means write
{
do
{
Console.WriteLine("Enter
stduent Roll Number=");
value = Convert.ToInt32(Console.ReadLine());
if (value <= 0)
Console.WriteLine("\n Roll-Number must be greater than ZERO , try
again");
else
Console.WriteLine("\n Roll Number saved\n\n");
} while (value <= 0);
// check to aviod entry of -ve number
this.RN = value;
}
}
public string setName
{
get
{
return name;
}
set //set means write
{
do
{
Console.WriteLine("Enter
stduent Name (null value is not allowed)= ");
value = Console.ReadLine();
if (string.IsNullOrEmpty(value))
Console.WriteLine("\n Student Name cannot be NULL or EMPTY");
else
Console.WriteLine("\n Roll Number saved\n\n");
} while (string.IsNullOrEmpty(value));
// check to aviod entry of -ve number
this.name = value;
}
}
}
class Program
{
static void Main(string[] args)
{
SE obj = new SE();
obj.setRN = -100;
obj.setName = "";
Console.WriteLine(obj.setRN);
Console.WriteLine(obj.setName);
Console.ReadKey();
}
}
0 Comments