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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Chapter 9 Color, Sound and Graphics
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Introduction to Computers and Programming Introduction to Methods in Java.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: User-Defined Functions I
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
C++ for Engineers and Scientists Third Edition
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Chapter 4 Introduction to Classes, Objects, Methods and strings
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6B Methods (Tutorial)
Chapter 9 Classes: A Deeper Look, Part I Part II.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSCI 171 Presentation 6 Functions and Variable Scope.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Functions.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Function There are two types of Function User Defined Function
Java Programming: Guided Learning with Early Objects
CSCI 161: Introduction to Programming Function
User-Defined Functions
METHODS AND BEHAVIORS AKEEL AHMED.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Presentation transcript:

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

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

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

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();

Visual C++ Programming 5

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

Visual C++ Programming 7

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()

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

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

Visual C++ Programming 11

Visual C++ Programming 12

Visual C++ Programming 13

Visual C++ Programming 14

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);

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

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

Visual C++ Programming 18

Visual C++ Programming 19

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

Visual C++ Programming 21

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();

Visual C++ Programming 23

Visual C++ Programming 24

Visual C++ Programming 25

Visual C++ Programming 26

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)

Visual C++ Programming 28

Visual C++ Programming 29

Visual C++ Programming 30

Visual C++ Programming 31

Visual C++ Programming 32

Visual C++ Programming 33

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

Visual C++ Programming 35

Visual C++ Programming 36

Visual C++ Programming 37

Visual C++ Programming 38

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

Visual C++ Programming 40

Visual C++ Programming 41

Visual C++ Programming 42

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();

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);

Visual C++ Programming 45

Visual C++ Programming 46

Visual C++ Programming 47

Visual C++ Programming 48

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);

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);

Visual C++ Programming 51

Visual C++ Programming 52

Visual C++ Programming 53

Visual C++ Programming 54

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;

Visual C++ Programming 56

Visual C++ Programming 57

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

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

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