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 In this chapter, you will: 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 Programming with Visual C++2

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

4 Methods 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(); Programming with Visual C++4

5 Methods (continued) Programming with Visual C++5

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

7 Methods (continued) Programming with Visual C++7

8 System-Defined Class Methods 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() Programming with Visual C++8

9 The System::Math Class Library The Math class contains constants and methods for mathematical operations See MSDN Web site 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 Programming with Visual C++9

10 The System::Math Class Library (continued) 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) Programming with Visual C++10

11 The System::Math Class Library (continued) Methods requiring angles Angles must be measured in radians, not degrees Include Math::Sin(), Math::Cos(), Math::Tan(), and others Programming with Visual C++11

12 The System::Math Class Library (continued) Programming with Visual C++12 Consider a program in which: – The user enters an angle in degrees – The program computes the radians and then determines the sine, cosine, and tangent of that angle

13 The System::Math Class Library (continued) Programming with Visual C++13

14 System-Defined Instance Methods An object is said to be an “instance” of the class definition used to create it An instance method is a method, defined within a class, that will belong to every object constructed from that class Example: – Every Random object has its own Next() method (an instance method) – randomNumberGenerator->Next(0,100); Programming with Visual C++14

15 Application Methods Your programs are often called applications Applications (like Form1) can have methods 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 Programming with Visual C++15

16 Application Methods (continued) Formal method definitions The first line is called the signature line Comes before the method body Method body What the method does Enclosed in { } Syntax diagram Programming with Visual C++16

17 Application Methods (continued) Signature line components Access mode Return type Name Parameter list Programming with Visual C++17

18 Application Methods (continued) Programming with Visual C++18

19 Programmer-Defined Methods Programmer-defined methods Programmer defines the signature line and body Must be defined outside of all other 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 Programming with Visual C++19

20 Programmer-Defined Methods (continued) Programming with Visual C++20

21 Methods without Parameters or a Return Type 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(); Programming with Visual C++21

22 Methods without Parameters or a Return Type (continued) Programming with Visual C++22 This program contains duplicate code in Form1_Load() and btnClear_Click()

23 Methods without Parameters or a Return Type (continued) Programming with Visual C++23 To remedy the duplicate code problem, write a programmer-defined method – reset() – Have Form1_Load() and btnClear_Click() each call reset()

24 Methods without Parameters or a Return Type (continued) Programming with Visual C++24

25 Programming with Visual C++25

26 Methods with Value Parameters Consider this program example: The last two instructions process the data that was read and display the results Programming with Visual C++26

27 Methods with Value Parameters (continued) Programming with Visual C++27

28 Methods with Value Parameters (continued) Programming with Visual C++28 The program has been modified to call a programmer-defined method called ProcessAndDisplay() Actual arguments Specify the values sent into the method through its parameters The actual arguments for ProcessAndDisplay() are width and height Copies of the values in width and height are passed into the method (pass-by-value)

29 Methods with Value Parameters (continued) Programming with Visual C++29 Here is the code for the ProcessAndDisplay() method The parameters in the method definition signature line are called formal parameters They define what type of values are expected when the method is called (value parameters) Create local variables for those values The formal parameters here are rect_width and rect_height

30 Methods with Value Parameters (continued) Programming with Visual C++30

31 Methods with Value Parameters (continued) Programming with Visual C++31

32 Methods with Value Parameters (continued) Programming with Visual C++32 Relationship between actual arguments and formal parameters – There must be the same number of each – The order must be the same – The data types must match – The names DO NOT have to be the same This is because the formal parameters are local variables.; they store copies that the data passed into them from the actual arguments

33 Methods with Reference Parameters Reference parameters are used when you wish to make a change in a variable named as an actual argument Reference parameters are not like value parameters – Value parameters store copies of data coming from actual arguments – Reference parameters “refer back” to the memory cell locations represented by the actual arguments – Pass-by-reference Reference parameters use the & symbol to indicate the relationship Programming with Visual C++33

34 Methods with Reference Parameters (continued) In this example, a programmer-defined method called ReadData() is called Its purpose is to read data into the actual arguments width and height Programming with Visual C++34

35 Methods with Reference Parameters (continued) Programming with Visual C++35 The formal parameters for ReadData() are reference parameters (notice the & symbol) Any change made to rect_width and rect_height will be communicated back to the actual arguments ( width and height )

36 Methods with Reference Parameters (continued) Programming with Visual C++36

37 Methods with Reference Parameters (continued) Programming with Visual C++37

38 Methods with Return Values Methods can be assigned a return data type – When the method is over, data of this type is returned to the calling program For example, TryParse() does more than read data into variables; it also returns a Boolean value – true if the parsing operation was successful – false if the parsing operation was not This return value can be stored in a Boolean variable Programming with Visual C++38

39 Methods with Return Values (continued) Programming with Visual C++39 Consider a programmer-defined method called ConvertToRadians() that returns the value of an angle after conversion to radians It could be called like this:

40 Methods with Return Values (continued) Programming with Visual C++40 Code for ConvertToRadians() Notice the return statement

41 Graphics Class Objects and Methods Graphics objects are used to “draw” on other objects (like Form1) Graphics objects include 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(); Programming with Visual C++41

42 Graphics Class Objects and Methods (continued) The System::Drawing class can create objects like brushes, pens Declaration example: Drawing::Brush^ yellowBrush; Instantiation: yellowBrush = gcnew Drawing::SolidBrush(Color::Yellow); Programming with Visual C++42

43 Graphics Class Objects and Methods (continued) Programming with Visual C++43

44 Graphics Class Objects and Methods (continued) Programming with Visual C++44

45 Graphics Class Objects and Methods (continued) Programming with Visual C++45 Graphics and Drawing objects are initially declared as instance variables The ^ operator is called a “handle” A handle may be connected to an object at a later time in the program

46 Graphics Class Objects and Methods (continued) Programming with Visual C++46 To construct a new object and connect it to a handle, the gcnew operator is used

47 Graphics Class Objects and Methods (continued) Drawing takes place within Rectangle s Rectangle declarations have four parameters X and y coordinates of the upper-left corner For example: x = 38, y = 25 Rectangle width and height (in pixels) For example: width = 25, height = 25 Programming with Visual C++47

48 Graphics Class Objects and Methods (continued) Both x and y coordinates are measured from the upper-left corner of the object that is being drawn in X coordinates start at 0 and increase as you move right Y coordinates start with 0 and increase as you move down Programming with Visual C++48

49 Graphics Class Objects and Methods (continued) Programming with Visual C++49

50 Graphics Class Objects and Methods (continued) Programming with Visual C++50 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

51 Graphics Class Objects and Methods (continued) Programming with Visual C++51

52 The Use of Constants 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 int WIDTH = 150; Programming with Visual C++52

53 Programming with Visual C++53

54 Summary 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 Programming with Visual C++54

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

56 Summary (continued) The drawing process Create object to be drawn on (i.e., 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 Programming with Visual C++56


Download ppt "Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)"

Similar presentations


Ads by Google