CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Road Map Introduction to object oriented programming. Classes
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Computer Programming 1 More on functions. Computer Programming 2 Objectives Function overloading Scope rules and namespace Inline Templates Pass by value.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Review of C++ Programming Part II Sheng-Fang Huang.
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Learners Support Publications Functions in C++
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 6 Methods Chapter 6 - Methods.
Classes, Interfaces and Packages
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Fundamentals BCIS 3680 Enterprise Programming.
C++ Functions A bit of review (things we’ve covered so far)
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
User-Written Functions
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Chapter 10: Void Functions
Chapter 5 Functions DDC 2133 Programming II.
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Variables and Their scope
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 9 Objects and Classes
Parameters and Overloading
Defining Classes and Methods
Nate Brunelle Today: Functions again, Scope
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

CS-212 Classes (cont) Dick Steflik

Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state data – mutators used to modify state data – functions that model the classes behavior tend to be action words (verbs or verb phrases) – take_off, reverse_direction, open...

Function prototypes Prototypes are models of how a function is to be called – are not necessarily part of the function definition, but may be float calculate_attack_angle (float, float); notice that in a prototype only the types are required, parameter names are not

Function signatures The signature of a function consists of: – returned type (if any) – the function name – the ordered set of types in the parameter list within its scope, a function’s signature must be unique even if its name is overloaded

Parameters Parameters are required on a function definition – parameter consists of a type and an identifier function formal parameters – parameters in the function definition alias names for the actual arguments scope is local to the function parameters may be passed to a function – by value – by reference ex. boolean exists(const point p);

Value Parameters “read only” or “input only” parameters makes a local copy (on the stack) of the value for use in the function; copies value of argument before running the function “pass by value” slower than pass by reference use only for small objects

Reference Parameters “read/write” parameter; value of argument can be changed uses address of argument to manipulate it doesn’t require local storage use & to identify reference parameters – remember & is the address operator ex. void rotate (point & p);

Reference Parameters (cont.) use const reference parameters instead of value parameters (faster), less memory rqd. const indicates a promise to not modify the reference parameter exists ex. double distance(const point & p); p is passed by reference as an input only parameter

Inlining functions Two ways: – inline keyword (place on function definition line) – replaces a call to the function with a copy of the function’s code. – defining the function in the class definition automatically makes it inline – use for small functions many calls to the function will bloat the program slightly faster than a regular function call

Function Overloading The function name may be reused (overloaded) within a class each definition must have a unique signature (pattern made up of function name and the parameter types)