C# Context: . Design a class for a student who has details, such as id, name and email. The class has method Show() which prints the details of the student. Create one object of the class and initialize the object with details at the time of initialization. Create another object and give the default detail to the object. Perform constructor overloading and method overloading in the class. Answer question
Question
C# Context: . Design a class for a student who has details, such as id, name and email. The class has method Show() which prints the details of the student. Create one object of the class and initialize the object with details at the time of initialization. Create another object and give the default detail to the object. Perform constructor overloading and method overloading in the class. Answer question
Solution
Here is a simple implementation of a Student class in C# that includes constructor overloading and method overloading:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
// Default constructor
public Student()
{
Id = 0;
Name = "Default Name";
Email = "Default Email";
}
// Parameterized constructor
public Student(int id, string name, string email)
{
Id = id;
Name = name;
Email = email;
}
// Method overloading
public void Show()
{
Console.WriteLine("ID: " + Id);
Console.WriteLine("Name: " + Name);
Console.WriteLine("Email: " + Email);
}
public void Show(string message)
{
Console.WriteLine(message);
Show();
}
}
class Program
{
static void Main(string[] args)
{
// Creating object with details
Student student1 = new Student(1, "John Doe", "[email protected]");
student1.Show();
// Creating object with default details
Student student2 = new Student();
student2.Show("These are the default details:");
}
}
In this code, we have a Student class with properties Id, Name, and Email. We have two constructors - one without parameters (default constructor) and one with parameters (parameterized constructor). The default constructor initializes the properties with default values, while the parameterized constructor initializes the properties with the values passed as arguments.
We also have two Show methods - one without parameters and one with a string parameter. The Show method without parameters prints the details of the student, while the Show method with a string parameter first prints the passed message and then calls the other Show method to print the details.
In the Main method, we create two Student objects - student1 with specific details and student2 with default details. We then call the Show method on both objects to print their details.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.