Presentation is loading. Please wait.

Presentation is loading. Please wait.

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)

Similar presentations


Presentation on theme: "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)"— Presentation transcript:

1 VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)

2 Objectives Visual C++ Programming 2  Learn how to use system-defined class methods  Create programmer-defined methods  Pass data into a method by value  Pass data into a method by reference

3 Objectives (continued) Visual C++ Programming 3  Use methods with return types  Explore the use of System::Drawing objects and their methods  Use the PictureBox and Timer controls

4 Methods Visual C++ Programming 4  Methods are named, self-contained sections of code  System-defined methods  Class methods Belong to system-defined classes Example, the Math class System::Math::Sqrt()  Instance methods Belong to objects derived from system-defined classes Example, an object of the TextBox class textBox1->Focus();

5 Visual C++ Programming 5

6 Methods (continued) Visual C++ Programming 6  Application methods  Event handlers Example: btnCalc_Click()  Programmer-defined methods Created and named by the programmer

7 Visual C++ Programming 7

8 System-Defined Class Methods Visual C++ Programming 8 Part of System namespace Scope resolution operator :: identifies the name of the method and the System class it belongs to Example: System::Int32::TryParse() System::Convert::ToDouble() Use of the System:: designator is optional Int32::TryParse() Convert::ToDouble()

9 The System::Math Class Library Visual C++ Programming 9 The Math class contains constants and methods for mathematical operations See MSDN website for a complete list Constant Math::PI Commonly used ones Math::Sqrt() – square root Math::Pow() – exponentiation Math::Sin() – sine Math::Cos() – cosine Math::Tan() - tangent

10 The System::Math Class Library (continued) Visual C++ Programming 10 Some methods require parameters Parameters are data values provided to the method when it is called Parameters are put in parameter lists Example Math::Sqrt(25) - the parameter is 25 Math::Pow(2,3) The first parameter is the mantissa (2) The second parameter is the exponent (3) Methods requiring angles Angles must be measured in radians, not degrees Include Math::Sin(), Math::Cos(), Math::Tan(), and others

11 Visual C++ Programming 11

12 Visual C++ Programming 12

13 Visual C++ Programming 13

14 Visual C++ Programming 14

15 System-Defined Instance Methods Visual C++ Programming 15  An instance is an object created from a class definition  Instance methods belong to every object (instance) of a class  Example:  Every Random object has its own Next() method that requires two parameters  randomNumberGenerator->Next(0,100);

16 Application Methods Visual C++ Programming 16  Application methods belong to a specific application class (like Form1)  Types of application methods  Event handlers The first line (signature line) and body { } are automatically generated  Programmer-defined methods Written by the programmer Become instance methods belonging to the Form1 class

17 Application Methods (continued) Visual C++ Programming 17  Method definition  Specifies how the method will be called and operate  Formal definition  Signature line Comes before the method body  Method body What the method does Enclosed in { }  Signature line  Access mode  Return type  Name  Parameter list

18 Visual C++ Programming 18

19 Visual C++ Programming 19

20 Programmer-Defined Methods Visual C++ Programming 20  Programmer-defined methods  Programmer defines the signature line and body  Must be defined outside of existing methods  Uses for Programmer-defined Methods  Used to reduce code redundancy  Used to isolate specific tasks  Types of programmer-defined methods  Methods without parameters or a return type  Methods with a return type  Methods with value parameters  Methods with reference parameters

21 Visual C++ Programming 21

22 Methods without Parameters or a Return Type Visual C++ Programming 22  Return type is void ( System::Void )  No parameters  Example  Reduce code redundancy with a Reset() method  Can be called from other methods  Called by its name and an empty set of parentheses Reset();

23 Visual C++ Programming 23

24 Visual C++ Programming 24

25 Visual C++ Programming 25

26 Visual C++ Programming 26

27 Methods with Value Parameters Visual C++ Programming 27  Formal parameters  In the signature line  Define what type of values are expected when the method is called (value parameters)  Create local variables for those values  Actual arguments  Specify the values sent into the method through its parameters  Only copies of the values are passed into the method (pass-by-value)

28 Visual C++ Programming 28

29 Visual C++ Programming 29

30 Visual C++ Programming 30

31 Visual C++ Programming 31

32 Visual C++ Programming 32

33 Visual C++ Programming 33

34 Methods with Reference Parameters Visual C++ Programming 34  Reference parameters are formal parameters that stand for the actual argument (pass-by-reference)  Reference parameters use the & symbol to indicate the relationship  Reference parameters are used to alter the data stored in the actual arguments  Actual arguments must be variables

35 Visual C++ Programming 35

36 Visual C++ Programming 36

37 Visual C++ Programming 37

38 Visual C++ Programming 38

39 Methods with Return Values Visual C++ Programming 39  The return value data type is identified in the signature  A return statement sends a value back to the calling program

40 Visual C++ Programming 40

41 Visual C++ Programming 41

42 Visual C++ Programming 42

43 Graphics Class Objects and Methods Visual C++ Programming 43  Graphics objects are used to “draw” on other objects (like Form1)  Graphics objects use Brushes, Pens and other System::Drawing objects  A common Graphics object name is simply g  Declare Graphics objects as instance variables  Graphics^ g;  Instantiate the Graphics object  Graphics^ g = Form1->CreateGraphics();

44 Graphics Class Objects and Methods (continued) Visual C++ Programming 44  The System::Drawing class contains objects that do the drawing  Brushes, pens, and other objects  Drawing takes place inside of areas called Rectangles  Declaration example:  Drawing::Brush^ yellowBrush;  Instantiation  yellowBrush = gcnew Drawing::SolidBrush(Color::Yellow);

45 Visual C++ Programming 45

46 Visual C++ Programming 46

47 Visual C++ Programming 47

48 Visual C++ Programming 48

49 Graphics Class Objects and Methods (continued) Visual C++ Programming 49  Rectangles are defined as rectangular areas in which drawing can take place  Rectangle declarations have four parameters  To define a Rectangle object  indicate the x and y coordinate of its upper left corner The coordinates are measured from the upper left corner of the object that is to be drawn upon X coordinates start at 0 and increase as you move to the right Y coordinates start with 0 and increase as you move down  Indicate the width and height (in pixels)  Example: x=38, y=25, width=150, height=150  Drawing::Rectangle circleRect(38,25,150,150);

50 Graphics Class Objects and Methods (continued) Visual C++ Programming 50  Graphics objects have system-defined instance methods that draw on the object to which they belong  Example: to draw a color-filled ellipse  FillEllipse()  To draw a circle use the FillEllipse() method  Parameters are the Brush you wish to use and the Rectangle object that the ellipse must be created within  Example  g->FillEllipse(yellowBrush, circleRect);

51 Visual C++ Programming 51

52 Visual C++ Programming 52

53 Visual C++ Programming 53

54 Visual C++ Programming 54

55 The Use of Constants Visual C++ Programming 55  Constants are declared (similar to variables)  Unlike variables  They cannot be changed when the program runs  Example  const int WIDTH = 150;  Constants can be shared by all objects derived from a class (static constants)  static const WIDTH = 150;

56 Visual C++ Programming 56

57 Visual C++ Programming 57

58 Summary Visual C++ Programming 58 Methods may belong to System-defined classes System-defined objects Applications Event-handlers Programmer-defined methods Instance methods Methods without a return type or parameters Methods with value parameters Methods with reference parameters Methods with a return type

59 Summary (continued) Visual C++ Programming 59 Constants Cannot be changed during execution Static constants are shared by all instances of a class Graphics objects Belong to the object to be drawn on Use Brushes, Pens and other objects to draw Drawing objects Brush, Pen, Rectangle

60 Summary (continued) Visual C++ Programming 60 The drawing process Create object to be drawn on (ie. PictureBox) Construct Graphics object assigned to it Graphics^ g = pictureBox1->CreateGraphics(); Construct drawing objects Example: yellowBrush Construct Rectangle object Example: circleRect Use the instance methods belonging to the Graphics object to draw Parameters are drawing object and Rectangle


Download ppt "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)"

Similar presentations


Ads by Google