Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
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.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
The Java Programming Language
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
1 CSC241: Object Oriented Programming Lecture No 02.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Topics Instance variables, set and get methods Encapsulation
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Andrew(amwallis) Classes!
Classes and Objects.
Chapter 7: User-Defined Functions II
Static data members Constructors and Destructors
Chapter 3: Using Methods, Classes, and Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
Classes, Encapsulation, Methods and Constructors (Continued)
Object Oriented Programming Review
Object Oriented Programming in java
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Creating and Using Classes
Presentation transcript:

Classes and Objects

Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages to Objects Constructors Static Data and Static Methods

Objectives At the completion of this topic, students should be able to: Design and use programmer written classes in a C# program Correctly declare instance member data in a class Correctly write instance member methods in a class Correctly create objects (instance of a class) Correctly send messages to objects Correctly write and use constructors Explain the use of static data and static methods

Book - title: string - price: double - rating: double + Book( ) + Book(:string, :double, :double) + GetTitle( ): string + SetTitle(:string): void + GetPrice( ): double + SetPrice(:double): void + CalcScore(: ): void Consider the UML class Diagram for a Book Class

To start a class definition class Book { } The keyword “class” The class name we have chosen A set of curly braces … The body of the class will go in between them.

Declaring Member Data class Book { private string title; } Member data is “private” Indent each line inside the block data type variable name

Declaring Member Data class Book { private string title; } We call data members of a class “Instance Data” because each instance (object) of the class will contain its own unique copy of this data.

Declaring Member Data class Book { private string title; private double price; private double rating; }

Declaring Member Methods class Book { private string title; private double price; private double rating; public void SetTitle( string t) { } Member methods are usually public return type method name parameters

Declaring Member Methods class Book { private string title; private double price; private double rating; public void SetTitle( string t) {... } The body of the method goes between these curly braces

“Getter”Methods class Book {... public string GetTitle( ) { return title; } getters always return something they are usually named “get” plus the name of the instance variable they will return the value of. getters take no parameters

“Setter”Methods class Book {... public void SetTitle(string t ) { title = t; } setters never return anything they are usually named “set” plus the name of the instance variable they will return the value of. setters always take a parameter The value of the parameter is stored in an instance variable.

Don’t forget the method prologue // the setTitle method // Purpose” to set the title of a book object // Parameters: the title to set, as a string // Returns: Nothing public void SetTitle(string t ) { title = t; }

Data Manipulation Methods class Book {... public double CalcScore( ) { double score = rating / price; return score; } They usually return something They work on the data inside of the object and do some calculation They usually take no parameters

Creating Objects

Class Book { private string title; private double price; private double rating;... } Class definition Book nextBook = new Book( ); this statement takes the Book class definition and uses it to create the object “nextBook”. When creating the object, storage is allocated for each of the data members defined in the class. Each data member is initialized to a standard default value. title price rating nextBook

Sending Messages to Objects

nextBook.SetTitle(“C# for Everyone” ); title price rating message nextBook object name. Method name parameters

void SetTitle(string t) { title = t; } nextBook.SetTitle(“C# for Everyone” ); This statement send the SetTitle message to the object named nextBook. As the method executes, the value of the parameter t is stored in the instance variable title. title price rating message nextBook

Constructors Creating objects with un-initialized member data can be dangerous. The instance variables in the object may not be set to what you expected. Constructors provide us with a handy way to initialize member data when an object is created.

Important Note: Constructors don’t create objects! They are used to initialize data in an object.

Constructor Definitions A constructor is a member method of a class, but it has two unique qualities: * It must have the same name as the class * It has no return type ( not even void)

This is the constructor for the Book class. Notice that it has the same name as the class and has no return type. class Book { private string title; private double price; private double rating; public Book( ) {... }

In a default (non-parameterized) constructor set values to reasonable default values. class Book { private string title; private double price; private double rating; public Book( ) { title = “none”; price = 0.0; rating = 0; }

Constructors can be overloaded class Book { private string title; private double price; private double rating; public Book( ) { title = “none”; price = 0;0; rating = 0; } public Book(string t, double pr, double rt ) { title = t; price = pr; rating = rt; }

This constructor stores values that are passed as parameters class Book { private string title; private double price; private double rating; public Book( ) { title = “none”; price = 0;0; rating = 0; } public Book(string t, double pr, double rt ) { title = t; price = pr; rating = rt; }

You do not have to write a constructor if you are happy with the default values that the compiler uses. The compiler builds a default constructor for you.

However … if you write a parameterized constructor you should also write your own default constructor. the compiler won’t create one for you.

Connecting the class to the User Interface Remember that good program design means separating the user interface logic from the business logic of the program. but how do we connect the two things together to make the program work?

An object that provides the business logic for the application The Form object – manages the user interface

Form Class Code Create a reference to an object of your class. Create a reference to an object of your class. By making the reference at the class level, it can be seen by every method in the Form.

Form Class Code Now you can create an object whenever You need it. When you need an object of your class, just create it and store The reference to it in the reference you declared earlier. Now the object can be seen from anyplace in the Form code.

For example, this event handler uses the GetCounterValue method That belongs to the Counter object we created in the previous slide.

Static Data Members Normally, each object of a class keeps its own copy of the data members defined in the class. class Book { private double price; … } book1 price $75.95 book2 price $85.00 book3 price $64.50

Static Data Members When a data member is declared as static, there is only one copy of the variable, and it is shared by all object of the class. This data is stored in the data segment. class Book { private static double price; … } book1 price book2 price book3 price $64.50

Static Member Methods Member methods of a class can also be declared as static. A static method can be invoked without an object of the class ever having been created. As a result, static methods cannot do anything that depends on there being a calling object. In particular, a static method cannot use non-static member data. Static member functions are invoked using the class name: Book.SetPrice (54.00);

Now you can see why the method Main( ) is declared as static. We can invoke Main( ) without ever creating an object.

A Class Design Exercise

Design a class for a combination lock. To open the lock you would turn the dial right to the first number, left to the second number, and finally right to the last number. Define the following operations: a constructor( int, int, int ); void TurnLeft ( int n ); // turns the dial right to the number n void TurnRight( int n ); // turns the dial left to the number n bool Open( ); // returns true if the lock opens void Reset( ); // resets the lock, ready to try again

A Class Design Exercise Design the class, create a class diagram Code up the class Create a very small driver to test it create a lock object dial the combination try to open it print a message