This article tells you how to create your own class with some function in it and how to call these functions from main. 1. Creating your class : This is how my class mcCalculator look like. Its has five functions besides constructor and destructor called Add, Subtract, Devide ( spell mistake ) , Multiply, and DisplayOutVal. As you can see from their names, these functions adds, subtracts, multiplies, divides correspondingly and stores result in a private variable iOutVal which is called by DisplayOutVal to display the result.public class mcCalculator{private int iOutVal;//Default constructorpublic mcCalculator(){iOutVal = 0;}public mcCalculator(int iVal1 ){iOutVal = iVal1 ;}//destructor~mcCalculator(){iOutVal = 0;}//methodspublic void displayiOutVal(){Console.WriteLine("iOutVal = {0}", iOutVal);}// this function adds two values and saves in iOutValpublic void add(int iVal1, int iVal2 ){iOutVal = iVal1 + iVal2 ;}// this function subtracts one from other and saves in iOutValpublic void subtract(int iVal1, int iVal2 ){iOutVal = iVal1 - iVal2 ;}// this function multiplyies two values and saves in iOutValpublic void Multiply(int iVal1, int iVal2 ){iOutVal = iVal1 * iVal2 ;}// this function devides one value from other and saves in iOutValpublic void Devide(int iVal1, int iVal2 ){iOutVal = iVal1 / iVal2 ;}} 2. Calling your class from Main : Now call mcCalculator from main. First create instance of mcCalculator and then call its member functions.// Main Program class mcStart{public static void Main(){mcCalculator mcCal = new mcCalculator(50);mcCal.add(12, 23 );mcCal.displayiOutVal();mcCal.subtract( 24, 4 );mcCal.displayiOutVal();mcCal.Multiply( 12, 3 );mcCal.displayiOutVal();mcCal.Devide( 8, 2 );mcCal.displayiOutVal(); }}3. Don't forget to call using System; as first line of your file.Quick Method: Download attached file second.cs and run from command line csc second.cs.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: