Inheritance |
|
The primary characteristic of the objects on the above pictures is that they are balls used in different sports. Another characteristic they share is that they are round. On the other hand, although these balls are used in sport, one made for one sport cannot (or should not) be used in another sport (of course, it is not unusual for a footballer to mess with a basketball on a lawn but it is not appropriate). The common characteristics of these objects can be listed in a group like a C# class. The class would appear as: class Ball { TypeOfSport; Size; } If you were asked to create a class to represent these balls, you may be tempted to implement a general class that defines each ball. This may be a bad idea because, despite their round resemblance, there are many internal differences among these balls. Programming languages like C# provide an alternate solution to this type of situation. Inheritance consists of creating a class whose primary definition or behavior is based on another class. In other words, inheritance starts by having a class that can provide behavior that other classes can improve on.
As you may have guess, in order to implement inheritance, you must first have a class that provides the fundamental definition or behavior you need. There is nothing magical about such a class. It could appear exactly like any of the classes we have used so far. Here is an example: using System; class Circle { private double _radius; public double Radius { get { if( _radius < 0 ) return 0.00; else return _radius; } set { _radius = value; } } public double Diameter { get { return Radius * 2; } } public double Circumference { get { return Diameter * 3.14159; } } public double Area { get { return Radius * Radius * 3.14159; } } } class Exercise { public static int Main() { Circle c = new Circle(); c.Radius = 25.55; Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", c.Radius); Console.WriteLine("Diameter: {0}", c.Diameter); Console.WriteLine("Circumference: {0}", c.Circumference); Console.WriteLine("Area: {0}", c.Area); return 0; } } This would produce: Circle Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Press any key to continue The above class is used to process a circle. It can request or provide a radius. It can also calculate the circumference and the area of a circle. Now, suppose you want to create a class for a sphere. You could start from scratch as we have done so far. On the other hand, since a sphere is primarily a 3-dimensional circle, and if you have a class for a circle already, you can simply create your sphere class that uses the already implemented behavior of a circle class. Creating a class that is based on another class is also referred to as deriving a class from another. The first class serves as parent or base. The class that is based on another class is also referred to as child or derived. To create a class based on another, you use the following formula: class NewChild : Base { // Body of the new class } In this formula, you start with the class keyword followed by a name from your class. On the right side of the name of your class, you must type the : operator, followed by the name of the class that will serve as parent. Of course, the Base class must have been defined; that is, the compiler must be able to find its definition. Based on the above formula, you can create a sphere class based on the earlier mentioned Circle class as follows: class Sphere : Circle { // The class is ready } After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example: using System; class Circle { private double _radius; public double Radius { get { if( _radius < 0 ) return 0.00; else return _radius; } set { _radius = value; } } public double Diameter { get { return Radius * 2; } } public double Circumference { get { return Diameter * 3.14159; } } public double Area { get { return Radius * Radius * 3.14159; } } } class Sphere : Circle { } class Exercise { public static int Main() { Circle c = new Circle(); c.Radius = 25.55; Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", c.Radius); Console.WriteLine("Diameter: {0}", c.Diameter); Console.WriteLine("Circumference: {0}", c.Circumference); Console.WriteLine("Area: {0}", c.Area); Sphere s = new Sphere(); s.Radius = 25.55; Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", s.Radius); Console.WriteLine("Diameter: {0}", s.Diameter); Console.WriteLine("Circumference: {0}", s.Circumference); Console.WriteLine("Area: {0}", s.Area); return 0; } } This would produce: Circle Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Sphere Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Press any key to continue When a class is based on another class, all public (we will also introduce another inheritance-oriented keyword for this issue) members of the parent class are made available to the derived class that can use them as easily. While other methods and classes can also use the public members of a class, the difference is that the derived class can call the public members of the parent as if they belonged to the derived class. That is, the child class doesn't have to "qualify" the public members of the parent class when these public members are used in the body of the derived class. This is illustrated in the following program: using System; class Circle { private double _radius; public double Radius { get { if( _radius < 0 ) return 0.00; else return _radius; } set { _radius = value; } } public double Diameter { get { return Radius * 2; } } public double Circumference { get { return Diameter * 3.14159; } } public double Area { get { return Radius * Radius * 3.14159; } } public void ShowCharacteristics() { Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", Radius); Console.WriteLine("Diameter: {0}", Diameter); Console.WriteLine("Circumference: {0}", Circumference); Console.WriteLine("Area: {0}", Area); } } class Sphere : Circle { public void ShowCharacteristics() { // Because Sphere is based on Circle, you can access // any public member(s) of Circle without qualifying it(them) Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", Radius); Console.WriteLine("Diameter: {0}", Diameter); Console.WriteLine("Circumference: {0}", Circumference); Console.WriteLine("Area: {0}\n", Area); } } class Exercise { public static int Main() { Circle c = new Circle(); c.Radius = 25.55; c.ShowCharacteristics(); Sphere s = new Sphere(); s.Radius = 25.55; s.ShowCharacteristics(); return 0; } } This would produce the same result. |
|
using System; public enum TypeOfIDCardApplication { NewCard = 1, Replacement, Correction, Renewal, Transfer } public class Applicant { private string name; private string addr; private string ct; private string stt; private string ZIP; private string sex; private string dob; private int wgt; private string hgt; private int race; public Applicant() { } public Applicant(string n) { this.name = n; } public Applicant(string n, string s) { this.name = n; this.sex = s; } public string FullName { get { return name; } set { name = value; } } public string Gender { get { return sex; } set { sex = value; } } public void Registration() { char s; Console.Write("Full Name: "); this.name = Console.ReadLine(); do { Console.Write("Sex(F=Female/M=Male): "); s = char.Parse(Console.ReadLine()); if( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') ) Console.WriteLine("Please enter a valid character"); sex = "Male"; }while( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') ); if( (s == 'f') || s == 'F' ) sex = "Female"; else if( (s == 'm') || (s == 'M') ) sex = "Male"; } public void Show() { Console.WriteLine("Full Name: {0}", this.name); Console.WriteLine("Sex: {0}", this.sex); } } public class IdentityCard : Applicant { public int ApplicationReason; public int TypeOfAnswer; public string TransferFrom; public string NotesOrComments; public void CreateIDCard() { do { Console.WriteLine("What's the reason you need an Identity Card?"); Console.WriteLine("1 - I need a new ID Card"); Console.WriteLine("2 - I need to replace my ID Card"); Console.WriteLine("3 - I need a correction on my ID Card"); Console.WriteLine("4 - I want to renew my current ID Card"); Console.WriteLine("5 - I want to transfer my ID Card"); Console.Write("Your Choice: "); ApplicationReason = int.Parse(Console.ReadLine()); if( ApplicationReason < 1 || ApplicationReason > 5 ) Console.WriteLine("\nPlease enter a valid number between 1 and 5"); } while(ApplicationReason < 1 || ApplicationReason > 5); this.Registration(); switch((TypeOfIDCardApplication)ApplicationReason) { case TypeOfIDCardApplication.NewCard: break; case TypeOfIDCardApplication.Replacement: do { Console.WriteLine("What is the reason you want a replacement?"); Console.WriteLine("1 - I lost my ID Card"); Console.WriteLine("2 - My ID Card was stolen"); Console.WriteLine("3 - My ID Card is damaged"); Console.WriteLine("4 - The court/a judge ordered that I replace my ID"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 1 || TypeOfAnswer > 4 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 1 || TypeOfAnswer > 4 ); Console.WriteLine("Write the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfIDCardApplication.Correction: do { Console.WriteLine("What is the reason you want a correction?"); Console.WriteLine("5 - There is an error on the card"); Console.WriteLine("6 - The picture (only the picture) on it is not wright"); Console.WriteLine("7 - I got married"); Console.WriteLine("8 - I got a divorce"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 5 || TypeOfAnswer > 8 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 5 || TypeOfAnswer > 8 ); Console.WriteLine("Write the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfIDCardApplication.Renewal: break; case TypeOfIDCardApplication.Transfer: Console.WriteLine("What US state or Canadian province are you transferring your ID from?"); TransferFrom = Console.ReadLine(); break; } } public void ShowIDCard() { switch((TypeOfIDCardApplication)ApplicationReason) { case TypeOfIDCardApplication.NewCard: Console.WriteLine("Application for New Identity Card"); break; case TypeOfIDCardApplication.Replacement: Console.WriteLine("Application for Identity Card Replacement"); break; case TypeOfIDCardApplication.Correction: Console.WriteLine("Application for Identity Card Correction"); break; case TypeOfIDCardApplication.Renewal: Console.WriteLine("Application for Identity Card Renewal"); break; case TypeOfIDCardApplication.Transfer: Console.WriteLine("Application for Identity Card Transfer"); Console.WriteLine("Identity Card Transferred From: {0}", TransferFrom); break; } Console.WriteLine("Applicant's Information"); this.Show(); Console.WriteLine("Administration Notes or Comments"); Console.WriteLine(NotesOrComments); } } public class Exercise { public static int Main() { int ReasonToBeHereToday; Console.WriteLine(" -=- Motor Vehicle Administration -=-"); do { Console.WriteLine("So what brought you to the Motor Vehicle Department today?"); Console.WriteLine("1 - I need an Identity Card"); Console.WriteLine("2 - I need a Driver's License"); Console.WriteLine("3 - I want to register to vote"); Console.Write("Your Choice: "); ReasonToBeHereToday = int.Parse(Console.ReadLine()); if( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ) Console.WriteLine("Please enter a valid number as 1, 2, or 3"); } while( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ); switch(ReasonToBeHereToday) { case 1: IdentityCard IDApplicant = new IdentityCard(); IDApplicant.CreateIDCard(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); IDApplicant.ShowIDCard(); break; case 2: Console.WriteLine("Come back tomorrow"); break; case 3: Console.WriteLine("Come back in two days"); break; } Console.WriteLine(); return 0; } } |
-=- Motor Vehicle Administration -=- So what brought you to the Motor Vehicle Department today? 1 - I need an Identity Card 2 - I need a Driver's License 3 - I want to register to vote Your Choice: 3 Come back in two days |
Practical Learning: Calling Base Members |
using System; public enum TypeOfIDCardApplication { NewCard = 1, Replacement, Correction, Renewal, Transfer } public class Applicant { . . . No Change } public class IdentityCard : Applicant { . . . No Change public void Registration() { do { . . . No Change } while(ApplicationReason < 1 || ApplicationReason > 5); base.Registration(); switch((TypeOfIDCardApplication)ApplicationReason) { . . . No Change } } public void Show() { switch((TypeOfIDCardApplication)ApplicationReason) { . . . No Change } Console.WriteLine("Applicant's Information"); base.Show(); Console.WriteLine("Administration Notes or Comments"); Console.WriteLine(NotesOrComments); } } public class VoterRegistration : Applicant { public string Citizen; public void Registration() { char IsACitizen; base.Registration(); do { Console.Write("Are you a citizen(y=Yes/n=No)? "); IsACitizen = char.Parse(Console.ReadLine()); if( IsACitizen != 'y' && IsACitizen != 'Y' && IsACitizen != 'n' && IsACitizen != 'N' ) Console.WriteLine("Invalid Answer"); } while( IsACitizen != 'y' && IsACitizen != 'Y' && IsACitizen != 'n' && IsACitizen != 'N' ); if( IsACitizen == 'y' || IsACitizen == 'Y' ) Citizen = "Yes"; else Citizen = "No"; } public void Show() { base.Show(); Console.WriteLine("US Citizen: {0}", this.Citizen); } } public class Exercise { public static int Main() { int ReasonToBeHereToday; Console.WriteLine(" -=- Motor Vehicle Administration -=-"); do { Console.WriteLine("So what brought you to the Motor Vehicle Department today?"); Console.WriteLine("1 - I need an Identity Card"); Console.WriteLine("2 - I need a Driver's License"); Console.WriteLine("3 - I want to register to vote"); Console.Write("Your Choice: "); ReasonToBeHereToday = int.Parse(Console.ReadLine()); if( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ) Console.WriteLine("Please enter a valid number as 1, 2, or 3"); } while( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ); switch(ReasonToBeHereToday) { case 1: IdentityCard IDApplicant = new IdentityCard(); IDApplicant.Registration(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); IDApplicant.Show(); break; case 2: Console.WriteLine("Come back tomorrow"); break; case 3: VoterRegistration Voter = new VoterRegistration(); Voter.Registration(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Voter.Show(); break; } Console.WriteLine(); return 0; } } |
-=- Motor Vehicle Administration -=- So what brought you to the Motor Vehicle Department today? 1 - I need an Identity Card 2 - I need a Driver's License 3 - I want to register to vote Your Choice: 3 Full Name: Jeanne Maillot Sex(F=Female/M=Male): d Please enter a valid character Sex(F=Female/M=Male): f Are you a citizen(y=Yes/n=No)? m Invalid Answer Are you a citizen(y=Yes/n=No)? t Invalid Answer Are you a citizen(y=Yes/n=No)? y -=- Motor Vehicle Administration -=- Full Name: Jeanne Maillot Sex: Female US Citizen: Yes |
Imagine you create a class used to process a circle as we saw earlier. You can use this as the base class for a cube. Both the square and the cube have an area but their values are different. This means that, as mentioned in our introduction to inheritance, when deriving the cube class, you would have to calculate a new area for the cube. If you create or declare a new member in a derived class and that member has the same name as a member of the base class, when creating the new member, you may want to indicate to the compiler that, instead of overriding that same method that was defined in the base class, you want to create a brand new and independent version of that method. When doing this, you would be asking the compiler to hide the member of the base class that has the same name, when the member of the current class is invoked. To do this, type the new keyword to its left. Here is an example: |
using System; class Circle { private double _radius; public double Radius { get { return (_radius < 0) ? 0.00 : _radius; } set { _radius = value; } } public double Diameter { get { return Radius * 2; } } public double Circumference { get { return Diameter * 3.14159; } } public double Area { get { return Radius * Radius * 3.14159; } } } class Sphere : Circle { new public double Area { get { return 4 * Radius * Radius * 3.14159; } } public double Volume { get { return 4 * 3.14159 * Radius * Radius * Radius / 3; } } } class Exercise { public static int Main() { Circle c = new Circle(); c.Radius = 25.55; Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", c.Radius); Console.WriteLine("Diameter: {0}", c.Diameter); Console.WriteLine("Circumference: {0}", c.Circumference); Console.WriteLine("Area: {0}", c.Area); Sphere s = new Sphere(); s.Radius = 25.55; Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", s.Radius); Console.WriteLine("Diameter: {0}", s.Diameter); Console.WriteLine("Circumference: {0}", s.Circumference); Console.WriteLine("Area: {0}", s.Area); Console.WriteLine("Volume: {0}\n", s.Volume); return 0; } }
using System; public enum TypeOfIDCardApplication { NewCard = 1, Replacement, Correction, Renewal, Transfer } public enum TypeOfDriversLicenseApplication { NewLearnersPermit = 1, NewDriversLicense, Replacement, Correction, Renewal, Transfer } public class Applicant { private string name; private string addr; private string ct; private string stt; private string ZIP; private string sex; private string dob; private int wgt; private string hgt; private int race; public Applicant() { } public Applicant(string n) { this.name = n; } public Applicant(string n, string s) { this.name = n; this.sex = s; } public string FullName { get { return name; } set { name = value; } } public string Gender { get { return sex; } set { sex = value; } } public void Registration() { char s; Console.Write("Full Name: "); this.name = Console.ReadLine(); do { Console.Write("Sex(F=Female/M=Male): "); s = char.Parse(Console.ReadLine()); if( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') ) Console.WriteLine("Please enter a valid character"); sex = "Male"; }while( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') ); if( (s == 'f') || s == 'F' ) sex = "Female"; else if( (s == 'm') || (s == 'M') ) sex = "Male"; } public void Show() { Console.WriteLine("Full Name: {0}", this.name); Console.WriteLine("Sex: {0}", this.sex); } } public class IdentityCard : Applicant { public int ApplicationReason; public int TypeOfAnswer; public string TransferFrom; public string NotesOrComments; new public void Registration() { do { Console.WriteLine("\nWhat's the reason you need an Identity Card?"); Console.WriteLine("1 - I need a new ID Card"); Console.WriteLine("2 - I need to replace my ID Card"); Console.WriteLine("3 - I need a correction on my ID Card"); Console.WriteLine("4 - I want to renew my current ID Card"); Console.WriteLine("5 - I want to transfer my ID Card"); Console.Write("Your Choice: "); ApplicationReason = int.Parse(Console.ReadLine()); if( ApplicationReason < 1 || ApplicationReason > 5 ) Console.WriteLine("\nPlease enter a valid number between 1 and 5"); } while(ApplicationReason < 1 || ApplicationReason > 5); base.Registration(); switch((TypeOfIDCardApplication)ApplicationReason) { case TypeOfIDCardApplication.NewCard: break; case TypeOfIDCardApplication.Replacement: do { Console.WriteLine("\nWhat is the reason you want a replacement?"); Console.WriteLine("1 - I lost my ID Card"); Console.WriteLine("2 - My ID Card was stolen"); Console.WriteLine("3 - My ID Card is damaged"); Console.WriteLine("4 - The court/a judge ordered that I replace my ID"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 1 || TypeOfAnswer > 4 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 1 || TypeOfAnswer > 4 ); Console.WriteLine("Write the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfIDCardApplication.Correction: do { Console.WriteLine("\nWhat is the reason you want a correction?"); Console.WriteLine("5 - There is an error on the card"); Console.WriteLine("6 - The picture (only the picture) on it is not wright"); Console.WriteLine("7 - I got married"); Console.WriteLine("8 - I got a divorce"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 5 || TypeOfAnswer > 8 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 5 || TypeOfAnswer > 8 ); Console.WriteLine("Write the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfIDCardApplication.Renewal: break; case TypeOfIDCardApplication.Transfer: Console.WriteLine("What US state or Canadian province are you transferring your ID from?"); TransferFrom = Console.ReadLine(); break; } } new public void Show() { switch((TypeOfIDCardApplication)ApplicationReason) { case TypeOfIDCardApplication.NewCard: Console.WriteLine("Application for New Identity Card"); break; case TypeOfIDCardApplication.Replacement: Console.WriteLine("Application for Identity Card Replacement"); break; case TypeOfIDCardApplication.Correction: Console.WriteLine("Application for Identity Card Correction"); break; case TypeOfIDCardApplication.Renewal: Console.WriteLine("Application for Identity Card Renewal"); break; case TypeOfIDCardApplication.Transfer: Console.WriteLine("Application for Identity Card Transfer"); Console.WriteLine("Identity Card Transferred From: {0}", TransferFrom); break; } Console.WriteLine("Applicant's Information"); base.Show(); Console.WriteLine("Administration Notes or Comments"); Console.WriteLine(NotesOrComments); } } public class DriversLicense : Applicant { public int ApplicationReason; public int TypeOfAnswer; public string TransferFrom; public char DLClass; public char OrganDonorAnswer; public string OrganDonor; public string NotesOrComments; new public void Registration() { do { Console.WriteLine("\nWhat's the reason you need a driver's license today?"); Console.WriteLine("1 - I want to apply for a new learner's permit"); Console.WriteLine("2 - I hold a learner's permit. Now I need a driver's license"); Console.WriteLine("3 - I need to replace my driver's license Card"); Console.WriteLine("4 - I need a correction on my driver's license Card"); Console.WriteLine("5 - I want to renew my current driver's license"); Console.WriteLine("6 - I want to transfer my driver's license from another state"); Console.Write("Your Choice: "); ApplicationReason = int.Parse(Console.ReadLine()); if( ApplicationReason < 1 || ApplicationReason > 6 ) Console.WriteLine("Please enter a valid number between 1 and 5"); } while( ApplicationReason < 1 || ApplicationReason > 6 ); base.Registration(); switch((TypeOfDriversLicenseApplication)ApplicationReason) { case TypeOfDriversLicenseApplication.NewLearnersPermit: break; case TypeOfDriversLicenseApplication.NewDriversLicense: break; case TypeOfDriversLicenseApplication.Replacement: do { Console.WriteLine("\nWhat is the reason you want a replacement?"); Console.WriteLine("1 - I lost my driver's license card"); Console.WriteLine("2 - My driver's license card was stolen"); Console.WriteLine("3 - My driver's license card is damaged"); Console.WriteLine("4 - The court/a judge ordered that I replace my driver's license"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 1 || TypeOfAnswer > 4 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 1 || TypeOfAnswer > 4 ); Console.WriteLine("\nWrite the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfDriversLicenseApplication.Correction: do { Console.WriteLine("\nWhat is the reason you want a correction?"); Console.WriteLine("5 - There is an error on my driver's license card"); Console.WriteLine("6 - The picture (only the picture) my driver's license is not wright"); Console.WriteLine("7 - I have decided to be an organ donor if I die"); Console.WriteLine("8 - I don't want to be an organ donor anymore if I die"); Console.WriteLine("9 - I got married"); Console.WriteLine("10 - I got a divorce"); Console.Write("Your Choice: "); TypeOfAnswer = int.Parse(Console.ReadLine()); if( TypeOfAnswer < 5 || TypeOfAnswer > 10 ) Console.WriteLine("Please enter the correct answer"); } while( TypeOfAnswer < 5 || TypeOfAnswer > 10 ); Console.WriteLine("Write the applican't description"); NotesOrComments = Console.ReadLine(); break; case TypeOfDriversLicenseApplication.Renewal: break; case TypeOfDriversLicenseApplication.Transfer: Console.WriteLine("\nWhat US state or Canadian province are you transferring your driver's license from?"); TransferFrom = Console.ReadLine(); break; } Console.WriteLine("\nWhat class of driver's license are you applying for?"); Console.WriteLine("A - All Non-commercial vehicles except motorcycles"); Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs."); Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs."); Console.WriteLine("K - Mopeds"); Console.WriteLine("M - Motorcycles"); Console.Write("Your Choice: "); DLClass = char.Parse(Console.ReadLine()); do { Console.Write("\nAre you willing to be an Organ Donor(y=Yes/n=No)? "); OrganDonorAnswer = char.Parse(Console.ReadLine()); if( OrganDonorAnswer != 'y' && OrganDonorAnswer != 'Y' && OrganDonorAnswer != 'n' && OrganDonorAnswer != 'N' ) Console.WriteLine("Please enter the correct answer"); } while( OrganDonorAnswer != 'y' && OrganDonorAnswer != 'Y' && OrganDonorAnswer != 'n' && OrganDonorAnswer != 'N' ); if( OrganDonorAnswer == 'y' || OrganDonorAnswer == 'Y' ) OrganDonor = "Yes"; else OrganDonor = "No"; } new public void Show() { switch((TypeOfDriversLicenseApplication)ApplicationReason) { case TypeOfDriversLicenseApplication.NewLearnersPermit: Console.WriteLine("Application for New Learner's Permit"); break; case TypeOfDriversLicenseApplication.NewDriversLicense: Console.WriteLine("Application for New Driver's License"); break; case TypeOfDriversLicenseApplication.Replacement: Console.WriteLine("Application for Driver's License Replacement"); break; case TypeOfDriversLicenseApplication.Correction: Console.WriteLine("Application for Driver's License Card Correction"); break; case TypeOfDriversLicenseApplication.Renewal: Console.WriteLine("Application for Driver's License Renewal"); break; case TypeOfDriversLicenseApplication.Transfer: Console.WriteLine("Application for Driver's License Transfer"); Console.WriteLine("Driver's License Transferred From: {0}", TransferFrom); break; } Console.WriteLine("Applicant's Information"); base.Show(); switch(DLClass) { case 'a': case 'A': Console.WriteLine("Class: A"); break; case 'b': case 'B': Console.WriteLine("Class: B"); break; case 'c': case 'C': Console.WriteLine("Class: C"); break; case 'k': case 'K': Console.WriteLine("Class: K"); break; case 'm': case 'M': Console.WriteLine("Class: M"); break; default: Console.WriteLine("Class: Unknown"); break; } Console.WriteLine("Organ Donor? {0}", OrganDonor); Console.WriteLine("Administration Notes or Comments"); Console.WriteLine(NotesOrComments); } } class VoterRegistration : Applicant { public string Citizen; new public void Registration() { char IsACitizen; base.Registration(); do { Console.Write("\nAre you a citizen(y=Yes/n=No)? "); IsACitizen = char.Parse(Console.ReadLine()); if( IsACitizen != 'y' && IsACitizen != 'Y' && IsACitizen != 'n' && IsACitizen != 'N' ) Console.WriteLine("Invalid Answer"); } while( IsACitizen != 'y' && IsACitizen != 'Y' && IsACitizen != 'n' && IsACitizen != 'N' ); if( IsACitizen == 'y' || IsACitizen == 'Y' ) Citizen = "Yes"; else Citizen = "No"; } new public void Show() { base.Show(); Console.WriteLine("US Citizen: {0}", this.Citizen); } } public class Exercise { public static int Main() { int ReasonToBeHereToday; Console.WriteLine(" -=- Motor Vehicle Administration -=-"); do { Console.WriteLine("So what brought you to the Motor Vehicle Department today?"); Console.WriteLine("1 - I need an Identity Card"); Console.WriteLine("2 - I need a Driver's License"); Console.WriteLine("3 - I want to register to vote"); Console.Write("Your Choice: "); ReasonToBeHereToday = int.Parse(Console.ReadLine()); if( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ) Console.WriteLine("Please enter a valid number as 1, 2, or 3"); } while( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 ); switch(ReasonToBeHereToday) { case 1: IdentityCard IDApplicant = new IdentityCard(); IDApplicant.Registration(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); IDApplicant.Show(); break; case 2: DriversLicense DL = new DriversLicense(); DL.Registration(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); DL.Show(); break; case 3: VoterRegistration Voter = new VoterRegistration(); Voter.Registration(); Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Voter.Show(); break; } Console.WriteLine(); return 0; } } |
-=- Motor Vehicle Administration -=- So what brought you to the Motor Vehicle Department today? 1 - I need an Identity Card 2 - I need a Driver's License 3 - I want to register to vote Your Choice: 2 What's the reason you need a driver's license today? 1 - I want to apply for a new learner's permit 2 - I hold a learner's permit. Now I need a driver's license 3 - I need to replace my driver's license Card 4 - I need a correction on my driver's license Card 5 - I want to renew my current driver's license 6 - I want to transfer my driver's license from another state Your Choice: 3 Full Name: Donnie Pastore Sex(F=Female/M=Male): k Please enter a valid character Sex(F=Female/M=Male): m What is the reason you want a replacement? 1 - I lost my driver's license card 2 - My driver's license card was stolen 3 - My driver's license card is damaged 4 - The court/a judge ordered that I replace my driver's license Your Choice: 3 Write the applican't description The applicant presented a driver's license card that was largely torn on one sid e. He wanted it replaced. What class of driver's license are you applying for? A - All Non-commercial vehicles except motorcycles B - Non-commercial vehicles up to and including 26,001/more lbs. C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs. K - Mopeds M - Motorcycles Your Choice: c Are you willing to be an Organ Donor(y=Yes/n=No)? n -=- Motor Vehicle Administration -=- Application for Driver's License Replacement Applicant's Information Full Name: Donnie Pastore Sex: Male Class: C Organ Donor? No Administration Notes or Comments The applicant presented a driver's license card that was largely torn on one sid e. He wanted it replaced. |
|
||
Previous | Copyright © 2004-2012, FunctionX | Next |
|