Creating and Using Classes

Slides:



Advertisements
Similar presentations
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Advertisements

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.
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.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
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.
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.
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.
Introduction to Object-Oriented Programming Lesson 2.
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 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 Introduction to Object Oriented Programming Chapter 10.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
The Object-Oriented Thought Process Chapter 03
Topic: Classes and Objects
Creating and Using Objects, Exceptions, Strings
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Classes and Objects.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
Object-Oriented Programming: Classes and Objects
Chapter 9 More on Objects and Classes
Java Primer 1: Types, Classes and Operators
More About Objects and Methods
Microsoft Visual Basic 2005: Reloaded Second Edition
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
This pointer, Dynamic memory allocation, Constructors and Destructor
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object Oriented Analysis and Design
Object Based Programming
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
Lecture Set 7 Procedures and Event Handlers
Lecture 22 Inheritance Richard Gesick.
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Computer Programming with JAVA
Lecture Set 11 Creating and Using Classes
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
CIS 199 Final Review.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

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

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 static member is 9/18/2019 6:23 AM

Common Class Elements (repeated) Classes contain a number of elements: Functions (methods) – 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 and fields) 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 9/18/2019 6:23 AM

Common Class Elements (continued) 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 Operator - A special type of method that’s performed for a Visual Basic operator such as + or =. 9/18/2019 6:23 AM

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 9/18/2019 6:23 AM

Fields aka Attributes Fields (Class attributes) are class variables/objects 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 9/18/2019 6:23 AM

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 Properties, as implemented in C# do not provide much of an advantage. I would not use them 9/18/2019 6:23 AM

9/18/2019 6:23 AM

9/18/2019 6:23 AM

9/18/2019 6:23 AM

Static versus Instance Data Class data can be instance data or static data Static data is declared with the static keyword One copy exists no matter the number of class instances One copy of instance data exists for each class instance How are static data useful?  9/18/2019 6:23 AM

A Note on Designing with Static Data NOTE: A static function operates within the context of static data – it can access any static data in a class, but it cannot access instance members How is the static counter NextHuman used below? 9/18/2019 6:23 AM

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 These are described, with examples, in the text I don’t advocate using public properties in C# The cause more confusion and provide little advantage 9/18/2019 6:23 AM

Methods Classes contain functions which we refer to as the called methods Methods can be public (often the case) or private (lower level functions usually used by higher level private functions) Whatever the case, we can write public or private functions in a class Why might we want a private function in a class? Methods perform operations on the data stores (attributes) of a class 9/18/2019 6:23 AM

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 part of a method’s signature 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 9/18/2019 6:23 AM

Methods and Overloading 9/18/2019 6:23 AM

Constructors By default, C# 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 C# 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 (See slide 9) How does the compiler know which constructor is being referenced when you use new applied to a type (class) with multiple constructors? 9/18/2019 6:23 AM

More on Constructors 9/18/2019 6:23 AM

9/18/2019 6:23 AM

9/18/2019 6:23 AM

9/18/2019 6:23 AM