Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods.

Similar presentations


Presentation on theme: "Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods."— Presentation transcript:

1 Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods Console String Programmer defined functions

2 Math class methods Abs(x) absolute value of x Ceiling(x) round x to smallest integer not less than x Cos(x) cosine of x Exp(x) exponential e^x Floor(x) rounds x to the largest integer not greater than x Log(x) natural logarithm of x ( base e) Max(x,y) larger value of x and y Min(x,y) smaller value of x and y Pow( x, y) x raised to power y Sin(x) sine of x Sqrt( x) square root of x Tan( x) tangent of x

3 Example b = 5;a = 2;c = -2; Console::WriteLine( Math::Sqrt(b*b – 4*a*c)); Y = Math::Sin(x); Z= Math::Pow( 2, 10);

4 Random number generation Class Random located in namespace System Create a object of class Random Random *ran = new Random(); To generate a random integer number between 0 and constant Int32::MaxValue ( 2,147,483,647) int num = ran->Next(); To generate a random number between 0 and 5 int num2 = ran->Next(6);

5 String A string is a series of characters treated as a single unit. In MC++, a string is an object of class String in the System namespace. Strings are a reference type MC++ programs use String * pointers to manipulate objects of type String. Declaration : String *name=“student”; However, Strings without prefix S are standard C++ string Managed string literals cannot be used where standard C++ string types are expected.

6 Methods in Strings class String::Concat(x.ToString(), S”\t”, (x+y).ToString()); String::CompareTo, Equals, ==  If( string1 == S”Hello”)  String1->Equals(S”Hello”)  String1->CompareTo(S”Hello”) CompareTo return 0, -1 or 1 when String1 is equal, less, or greater Equals and == return true or false

7 Methods in String class StartWith – determines whether a string instance stars with the string literal passed to it as an argument. Return true or false  If( string1->StartWith(S”He”) ) EndsWith – determines whether a string instance ends with the string literal passed to it as an argument  If( string1->EndWith(S”o”) )

8 Methods of String class IndexOf – locate the first occurrence of a character or substring in a string. Return index of the character or –1 if the character is not found  N = String1->IndexOf(‘l’); LastIndexOf – locate the last occurrence of a character in a string

9 More methods in String class SubString( int) – takes one int argument which specifies the starting index. The subString returned contains a copy of the characters from the starting index to the end of the string. SubString(int,int) – takes two int arguments. The first is starting index and the second is length of substring

10 practice One of the most popular games of Chance is a dice game known as “craps”. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. Each face contains 1,2,3,4,5 or 6 spots. After the dice has come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2,3 or 12 on the first throw( called “craps”), the player loses(i.e. the house wins). If the sum is 4,5,6,8,9 or 10 on the first throw, that sum becomes the player’s “point”. To win, player must continue rolling the dice until they “make their point” (i.e. roll their point value). The player loses by rolling a 7 before making the point. Write a program to simulate the game.


Download ppt "Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods."

Similar presentations


Ads by Google