Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
These materials where developed by Martin Schray
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
Object-Oriented Programming: Classes and Objects
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Object-Oriented Programming: Classes and Objects
Lecture 22 Inheritance Richard Gesick.
Lecture Set 11 Creating and Using Classes
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
Creating and Using Classes
Presentation transcript:

Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data

Slide 2 Objectives Describe these members of a class: constructor, method, field, and property Explain how instantiation works Describe the concept of overloading a method Explain what a shared member is

Slide 3 Common Class Elements (repeated) Classes contain other elements: Procedures (methods) – functions and subs – operations that can be performed by an object. Constructors – special type of method that’s executed when an object is instantiated Data stores (including properties) Other forms – objects of the form data type Numerous objects of types defined by other classes Variables (of primitive types) Events (and Event handlers -- not always tied to forms) – an event is a signal that notifies other objects that something noteworthy has occurred

Slide 4 Common Class Elements (continued - repeated) User defined data types – structures, enumerations, complex structured types (arrays of structures, lists or arrays of objects, etc) Delegate - A special type of object that’s used to wire an event to a method Data Stores Property - Represents a data value associated with an object instance Field - A variable that’s declared at the class level Constant- A constant associated with the class Default property - A special type of property that is used by default if a property name isn’t specified. It must include a parameter, which is typically used to access individual items within a class that represents a collection of objects. Operator- A special type of method that’s performed for a Visual Basic operator such as + or =.

Slide 5 How Instantiation Works The process of creating an object from a class is called instantiation You can create many instances of a single class A class defines a reference type The variable that is created for the object of a class thus contains a reference to the memory location where the object is stored and not the object itself When an object is instantiated the appropriate constructor is executed

Slide 6 Fields Fields are class variables that are global to (accessible by) all of the executable components of a class Usually declared as private and are not accessible outside the class May also be declared as public or protected and may also be declared using the friend modifier

Slide 7 Fields (continued) The desirable goal in using classes is to encapsulate and protect class data stores from external reference except through methods of the class Declaring fields as public defeats the purpose of using classes If accessing some class data stores from outside the class is considered cumbersome, we can access them through the use of properties, which simplifies the access mechanism we have to use while still protecting the data

Slide 8

Slide 9

Slide 10 Shared versus Instance Data Class data can be instance data or shared data Shared data is declared with the Shared keyword One copy exists no matter the number of class instances One copy of instance data exists for each class instance How are shared data useful? 

Slide 11 A Note on Designing with Shared Data NOTE: A shared function operates within the context of shared data – it can access any shared data in a class, but it cannot access instance members How is the shared counter NextHuman used below?

Slide 12

Slide 13 Using Properties to Access Private Data If we wish to provide more convenient access to the private or protected data of a class, we can do so through the use of public properties

Slide 14

Slide 15 Different Types of Properties

Slide 16 Accessing Properties

Slide 17 Methods Classes contain subs and functions The public subs and functions are called methods There also can be private subs and functions I call these methods as well (although the book does not seem to) Why might we want a private method? Methods perform operations on the data stores (attributes) of a class

Slide 18 Method Signatures and Overloading A method’s signature is defined by its name and its unique combination of parameters Note that the return value is not involved – it is assumed to always be the same We can write methods having the same name but different sets of parameters  This is known as overloading Overloading provides multiple ways of invoking a given method

Slide 19 Methods and Overloading

Slide 20 Overloading and Intellisense How does Intellisense handle overloaded functions (when it shows you the parameter list)

Slide 21 Constructors By default, VB will provide you with a default constructor for each class you write When you use new to create a new object (an instance of a class), the VB default constructor assigns default values to all instance variables of the new object If this is not what you want, you can write your own constructor(s) by taking advantage of overloading How does the compiler know which constructor is being referenced when you use New applied to a type (class) with multiple constructors

Slide 22 Overloading Constructors

Slide 23 More on Constructors

Slide 24 Murach’s Visual Basic 2005, C11 © 2006, Mike Murach & Associates, Inc. Slide 24

Slide 25

Slide 26

Slide 27 Class Shared Members (optional)

Slide 28

Slide 29

Slide 30

Slide 31