Super simple multi-state Calculator

Source: Internet
Author: User

Exam 4

1. Compile an operation parent class

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace polymorphism calculator {// <summary> /// calculate the parent class /// </Summary> class operation {public int NUMA {Get; set ;} public int numb {Get; set;} public operation (int numa, int numb) {This. NUMA = NUMA; this. numb = numb;} Public Virtual int getres () {return 0 ;}}}

Ii. Write the class for addition, subtraction, multiplication, division, and inherit from the operation class

Add

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace polymorphism calculator {class Add: Operation {public add (int numa, int numb): Base (NUMA, numb) {} public override int getres () {return base. numb + NUMA ;}}}

Subtraction

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace polymorphism calculator {class Jian: Operation {public Jian (int numa, int numb): Base (NUMA, numb) {} public override int getres () {return base. NUMA-Numb ;}}}

Multiplication

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace polymorphism calculator {class Ji: Operation {public Ji (int numa, int numb): Base (NUMA, numb) {} public override int getres () {return base. NUMA * numb ;}}}

Division

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace polymorphism calculator {class Div: Operation {public Div (int numa, int numb): Base (NUMA, numb) {} public override int getres () {return base. NUMA/numb ;}}}

Iii. Write main

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using polymorphism calculator; namespace c09 polymorphism calculator {class program {static void main (string [] ARGs) {// polymorphism is based on inheritance // finding the parent class // finding the common method of the subclass // finding the same method of each subclass, with different implementation consoles. writeline ("Enter the first number"); int NUMA = int. parse (console. readline (); console. writeline ("enter the second number"); int numb = int. parse (console. readline (); // console. writeline ("Enter the operator: 1. add 2. 3. multiply by 4. except "); string opt = console. readline (); operation = NULL; Switch (OPT) {Case "1": Operation = new add (NUMA, numb); break; Case "2 ": operation = new Jian (NUMA, numb); break; Case "3": Operation = new JI (NUMA, numb); break; Case "4 ": operation = new Div (NUMA, numb); break;} console. writeline (operation. getres (); console. readkey ();}}}

Okay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.