C 1 Base Class Create a class with these fields id int prot
C#
1.
Base Class
Create a class with these fields:
id - int, protected
name - string, protected
---
Properties
ID - get
Name - get, protected set
---
Methods:
virtual Print method
-When this method is called it prints the id and name on the same line with a tab between them.
---
Main
Create an object of your type.
Use the get on ID & Name.
Verify the set on ID & Name are invalid.
Call the Print method.
---------------------------------------------------------------------------------------------
C#
2.
Student Class
Create a student class that inherits from your base class.
Fields:
gpa - double
firstAttended - DateTime
---
Add properties for the gpa & firstAttended fields
---
Constructor that takes an id and name and sets the base fields.
Override the Print method to print id, name, gpa and first attended.
---
Main
Create a student object.
Use the set/get on ID & Name, GPA & FirstAttended as applies.
Call the Print method.
Solution
//Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InheritanceApplication
{
class Person
{
protected int _id;
protected String _name;
public Person()
{
_id = 0;
_name = \"\";
}
public Person(int id,String name)
{
this._id = id;
this._name = name;
}
//ID property
public int ID
{
get
{
return _id;
}
}
//Name property
protected String Name
{
set
{
_name = value;
}
get
{
return _name;
}
}
//Override print method
public virtual void print()
{
Console.WriteLine(\"id : {0} \\tName : {1}\", _id, _name);
}
}
}
--------------------
//Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InheritanceApplication
{
class Student :Person
{
private double _gpa;
private DateTime _firstAttended;
public Student(int id,String name,double gpa, DateTime firstAttended):base(id,name)
{
this._gpa=gpa;
this._firstAttended=firstAttended;
}
//GPA property
public double GPA
{
set
{
_gpa = value;
}
get
{
return _gpa;
}
}
//FirstAttended property
protected DateTime FirstAttended
{
set
{
_firstAttended = value;
}
get
{
return _firstAttended;
}
}
//Override print method
public override void print()
{
base.print();
Console.WriteLine(\"Gpa : {0} \\t Date : {1}\", _gpa, _firstAttended);
}
}
}
----------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InheritanceApplication
{
class Program
{
static void Main(string[] args)
{
//Create an instance of Studnet class
Student s=new Student(1111,\"johnson\",5.5, new DateTime(2016,12,1));
s.print();
Console.ReadKey();
}
}
}
------------------------------------------------------------------------------------------------------------
Sample Output:
id : 1111 Name : johnson
Gpa : 5.5 Date : 12/1/2016 12:00:00 AM



