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.

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

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Road Map Introduction to object oriented programming. Classes
Classes and Objects Systems Programming.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Using C++ Functions Object-Oriented Programming Using C++ Second Edition 4.
Chapter 11: Classes and Data Abstraction
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Chapter 6: User-Defined Functions
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 10 Introduction to Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
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 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
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.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Programming with ANSI C ++
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Chapter 9 Classes: A Deeper Look, Part 1
Object-Oriented Programming Using C++ Second Edition
Classes and Objects.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

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 about instance variables and methods How to declare objects

3 Objectives How to compile and run a program that instantiates class objects How to organize your classes Learn about public fields and private methods Learn about the this reference

4 Objectives How to write constructor methods How to pass parameters to constructors How to overload constructors How to write destructor methods

5 Understanding Class Concepts There are two distinct types of classes created in C#: –Classes that contain a Main() method –Classes from which you instantiate objects Everything is considered an object in the object-oriented approach Object-oriented programmers recognize “is-a relationships”

6 Understanding Class Concepts The use of objects provides a better understanding of the idea that is being programmed The data components of a class are called the instance variables of that class Class object attributes are called fields The contents of a class object’s instance variables are also known as its state In addition to attributes, class objects also have methods associated with them

7 Creating a Class from Which Objects Can Be Instantiated A class header or class definition contains three parts: –An optional access modifier –The keyword class –Any legal identifier you choose for the name of your class

8 Creating a Class from Which Objects Can Be Instantiated Private and protected classes have limited uses When you declare a class using a namespace, you can only declare it to be public or internal Class access is internal by default

9 Creating Instance Variables and Methods Instance variables are created using the same syntax used to declare other variables The allowable field modifiers are new, public, protected, internal, private, static, readonly, and volatile

10 Creating Instance Variables and Methods Identifying a field as private means that no other class can access the field’s value, and only methods of the same class will be allowed to set, get, or otherwise use the field Using private fields within classes is an example of information hiding Although most fields are private, most class methods are public

11 Creating Instance Variables and Methods In the following code, the variable idNumber is private, yet remains accessible to outside classes through the GetId() method Methods used with object instantiations are called instance methods

12 Creating Instance Variables and Methods Figure 4-4 creates a method that is a counterpart to the GetID() method, called SetId() The SetId() method does not return any value, rather, it is used to assign a value in the class

13 Declaring Objects Declaring a class does not create any actual objects A two step process creates an object that is an instance of a class: –Supply a type and an identifier –Allocate computer memory for the object Declaring an object notifies the compiler of the identifier and the type, but it does not allocate memory

14 Declaring Objects To allocate the needed memory for an object, use the new operator For example: Employee myAssistant = new Employee(); The value being assigned to myAssistant is a memory address at which it will be located Any class created can be referred to as a reference type, while the predefined types are called value types The method called after the new keyword is called the constructor method

15 Compiling and Running a Program That Instantiates Class Objects When you create an application that uses multiple class definitions, the class definitions can be stored in either a single file or each can be placed in its own file Storing all class definitions in one file shortens development time and simplifies the compilation process Using separate files for each class definition provides a more organized and manageable technique, and it allows for easier reusability

16 Organizing Your Classes Most classes you create will have multiple methods and fields, which can become difficult to track in large programs It is a good idea to arrange fields and methods in a logical order: –Alphabetically –By “Sets” and “Gets” –Pairing of “Sets” and “Gets”

17 Organizing Your Classes In addition to organizing fields and methods, comments should also be used

18 Using Public Fields and Private Methods The private fields/public method arrangement ensures that data will be used and changed only in ways provided in your methods Although it is easier to declare fields as public, doing so is considered poor object-oriented programming design Data should always be hidden when possible and access should be controlled by well-designed methods

19 Using Public Fields and Private Methods Poorly designed Desk class and program that instantiates a Desk

20 Using Public Fields and Private Methods The Carpet class

21 Using Public Fields and Private Methods Occasionally, there are instances where a data field should be declared as public A data field may be declared public if all objects of a class contain the same value for that data field The preceding example (fig 4-12) declared a public const field A named constant within a class is always static

22 Understanding the this Reference When instances of a class are created, the resulting objects require separate memory locations in the computer In the following code, each object of Book would at least require separate memory locations for title, numPages, and Price

23 Understanding the this Reference Unlike the class fields that are unique to each instance, the methods of the class are shared Although the methods are shared, there must be a difference between two method calls (from different objects), because each returns a different value (based on their unique fields) When a method is called, you automatically pass a reference to the memory of the object into the method The implicitly passed reference is the this reference

24 Understanding the this Reference Book class methods explicitly using the this reference

25 Understanding the this Reference Book class method that requires explicit this reference

26 Understanding Constructor Methods A constructor method is a method that establishes an object Every class created is automatically supplied with a public constructor method with no parameters Any constructor method you write must have the same name as its class, and constructor methods cannot have a return type

27 Passing Parameters to Constructors You can create a constructor that sets the same value for all objects instantiated. The program can eventually call the method of the object to set the value. However, this might not always be the most efficient technique.

28 Passing Parameters to Constructors In this code, the constructor receives an argument and initializes the object with the appropriate information

29 Overloading Constructors C# automatically provides a default constructor However if you create your own constructor, the automatically provided default constructor is not available C# constructor methods, like other methods, can be overloaded

30 Understanding Destructor Methods A destructor method contains the actions you require when an instance of a class is destroyed To explicitly declare a destructor method, use an identifier that consists of a tilde(~) followed by the class name Destructors cannot receive parameters and therefore cannot be overloaded A class can have at most one destructor

31 Understanding Destructor Methods Employee class with destructor method

32 Understanding Destructor Methods Destructor methods are automatically called when an object goes out of scope You cannot explicitly call a destructor The last object created is the first object destroyed

33 Chapter Summary When you write programs in C#, you create two distinct type of classes When you create a class, you create a class header and a class body You declare the attributes and instance variables for a class within the curly braces using the same syntax you use to declare other variables Declaring a class does not create any actual objects After an object has been instantiated, its public methods can be accessed using the object’s identifier, a dot, and a method call

34 Chapter Summary When you create a class that describes objects you will instantiate, and another class that instantiates those objects, you physically can contain both classes within the same file or place each class in its own file Although there is no requirement to do so, most programmers place data fields in some logical order at the beginning of a class Most programmers make class data fields private and class methods public

35 Chapter Summary When you create an object, you provide storage for each of the object’s instance variables, but not for each instance method A constructor method establishes an object You can create objects that hold unique fields right from the start Like any other C# methods, constructors can be overloaded A destructor method contains the actions you require when an instance of a class is destroyed