Answer to Question #116297 in C# for ayesha

Question #116297
Create a class that imitates part of the functionality of the basic data type int. Call the
class Int (note different capitalization). The only data in this class is an int variable.
Include member functions to initialize an Int to 0, to initialize it to an int value, to display
it (it looks just like an int), and to add two Int values.
Write a program that exercises this class by creating one uninitialized and two initialized
Int values, adding the two initialized values
1
Expert's answer
2020-05-18T16:57:26-0400
using System;

class Int {
  private int num;

  public Int(int val = 0) {
    num = val;
  }

  public void display() {
    Console.WriteLine(num);
  }

  public void add(Int val1, Int val2) {
    num = val1.num + val2.num;
  }
}

class MainClass {
  public static void Main (string[] args) {
    Int a = new Int();
    Int b = new Int(2);
    Int c = new Int(4);

    a.add(b, c);
    a.display();
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS