Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.

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

Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
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.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Road Map Introduction to object oriented programming. Classes
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 6: A First Look at Classes
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Writing Classes (Chapter 4)
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Introduction to Object-Oriented Programming Lesson 2.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming: Inheritance and Polymorphism.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
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.
Inheritance and Polymorphism
Object-Oriented Programming: Classes and Objects
Topics Procedural and Object-Oriented Programming Classes
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Methods.
Object Oriented Programming in java
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects

Copyright © 2012 Pearson Education, Inc. Topics 9.1 Introduction to Classes 9.2 Properties 9.3 Parameterized Constructors and Overloading 9.4 Storing Class Type Object in Arrays and Lists 9.5 Finding the Classes and Their Responsibilities in a Problem 9.6 Creating Multiple Forms in a Project

9.1 Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object, yet it is not an object. –It specifies the fields and methods a particular type of object can have. –One or more object can be created from the class. –Each object created from a class is called an instance of the class. Copyright © 2012 Pearson Education, Inc.

Creating a Class You can create a class by writing a class declaration. A generic form is: class className // class header { Member declaration(s)… } Class headers starts with the keyword class, followed by the name of the class. Member declarations are statements that define the class’s fields, properties, and/or methods. A class may contains a constructor, which is special method automatically executed when an object is created. Copyright © 2012 Pearson Education, Inc.

Sample Code Copyright © 2012 Pearson Education, Inc. class Coin { private string sideUp; // field public Coin() // constructor { sideUp = “Heads”; } public void Toss() // a void method { MessageBox.Show(sideUp); } public string GetSideUp() // a value-returning method { return sideUp; }

Creating an Object Given a class named Coin, you can create a Coin object use: Coin myCoin = new Coin(); where, –myCoin is a variable that references an object of the Coin class; –the new keyword creates an instance of the Coin class; and –the = operator assigns the reference that was returned from the new operator to the myCoin variable. Once a Coin object is created, you can access members of the class with it. E.g. myCoin.Toss(); Copyright © 2012 Pearson Education, Inc.

Where to Write Class Declarations In C# you have flexibility in choosing where to write class declarations. E.g. To create the Coin class, you can: –Save the class declaration is a separated.cs file; or –Add the Coin class next to the Form1 class inside the Form1.cs file. Copyright © 2012 Pearson Education, Inc. Namespace Example { public partial class Form1 : Form { public Form1() { InitializeComponent(); } …. } class Coin { …. }

Passing an Object to a Method Objects of a class can be used as parameter of a method. E.g. private void ShowCoinStatus(Coin coin) { MessageBox.Show(“Side is “ + coin.GetSideUp()); } In this example, a method named ShowCoinStatus accepts a Coin object as an argument. To create a Coin object and pass it as an argument to the ShowCoinStatus method, use: Coin myCoin = new Coin(); ShowCoinStatus(myCoin); Copyright © 2012 Pearson Education, Inc.

9.2 Properties A property is a class member that holds a piece of data about an object. –Properties can be implemented as special methods that set and get the value of corresponding fields. –Both set and get methods are known as accessors. –In the code, there is a private field (_name) which is a known as backing field and is used to hold any data assigned to the Name property. –The value parameter of set accessor is automatically created by the compiler. Copyright © 2012 Pearson Education, Inc. class Pet { private string _name; // backing field public Pet() { _name = “”; } public string Name { get { return _name; } set { _name = value; }

Setting the myDog object’s Name Property to “Fido” Copyright © 2012 Pearson Education, Inc. // Name property public string Name { get { return _name; } set { _name = value; } _name “Fido” Pet object myDog.Name = “Fido”;

The Backing Field The private backing field is a variable that stores a value assigned to the property which the backing fields is associated with. It is declared to be private to protect it from accidental corruption. If a backing field is public, it can then be accessible directly by code outside the class without the need for accessors. Copyright © 2012 Pearson Education, Inc.

get vs set Accessors The get accessor, if not empty, is a method that returns the property’s value because it has a return statement. –It is executed whenever the property is read. The set accessor, if not empty, gets the value stored in the backing field and assigns the value to the property –It has an implicit parameter named value. –It is executed whenever a value is assigned to the property. Copyright © 2012 Pearson Education, Inc.

Read-Only Properties A read-only property can be read, but it cannot be modified. –To set a read-only property, simply do no write a set accessor for the property. E.g. Copyright © 2012 Pearson Education, Inc. // read and write public double Diameter { get { return _diameter; } set { _diameter = value; } } // read-only public double Diameter { get { return _diameter; } }

9.3 Parameterized Constructor & Overloading A constructor that accepts arguments is known as parameterized constructor. E.g. public BankAccount(decimal startingBalance) { } A class can have multiple versions of the same method known as overloaded methods. How does the compiler know which method to call? –Binding relies on the signature of a method which consists of the method’s name, the data type, and argument kind of the method’s parameter. E.g. public BankAccount(decimal startingBalance) { } public BankAccount(double startingBalance) { } –The process of matching a method call with the correct method is known as binding. Copyright © 2012 Pearson Education, Inc.

Overloading Methods When a method is overloaded, it means that multiple methods in the same class have the same name but use different types of parameters. public void Deposit(decimal amount) { } public void Deposit(double amount) { } // overloaded public void Deposit(int numbers) { } // overloaded public void Deposit(string names) { } // overloaded Copyright © 2012 Pearson Education, Inc.

Overloading Constructors Constructors are special type of methods. They can also be overloaded. public BankAccount() { } // parameterless constructor public BankAccount(decimal startingBalance) { } // overloaded public BankAccount(double startingBalance) { } // overloaded –The parameterless constructor is the default constructor Compiler will find the matching constructors automatically. E.g. BankAccount account = new BankAccount(); BankAccount account = new BankAccount(500m); Copyright © 2012 Pearson Education, Inc.

9.4 Storing Class Type Objects in Array & Lists Objects that are instances of a class can be stored in an array. E.g. Const int SIZE = 4; CellPhone[] phone = new CellPhone[SIZE]; phone[0] = new CellPhone(); phone[1] = new CellPhone(); …. You can use a loop to step through the array. E.g. for (int index = 0; index < phone.Length; index++) { phones[index] = new CellPhone(); } Copyright © 2012 Pearson Education, Inc.

Initializing Array Elements You can initialize the array elements in the declaration statement: CellPhone[] phone = { new CellPhone(), new CellPhone(), new CellPhone(), new CellPhone() }; You can also initialize an array and assign its elements with references to a class. E.g. BankAccount[] accounts = { new BankAccount(1000), new BankAccount(2000), new BankAccount(3000), new BankAccount(4000), }; Copyright © 2012 Pearson Education, Inc.

Lists of Class Type Objects You can create a List to hold a class object. E.g. List phoneList = new List (); –This statement creates a List object, referenced by the phoneList variable. Each object of the CellPhone class needs an instance of CellPhone class to hold data. E.g. CellPhone myPhone = new CellPhone(); myPhone.Brand = “Acme Electronics”; myPhone.Model = “M1000”; myPhone.Price = 199; To add the Cellphone object to the List, use: phoneList.Add(myPhone); Copyright © 2012 Pearson Education, Inc.

9.5 Finding the Classes & their Responsibilities in a Problem When developing an object-oriented program, you need to identify the classes that you will need to create. One simple and popular techniques involves the following steps: –Get a written description of the problem domain. –Identify all the nouns (including pronouns and noun phrases) in the description. Each of these is a potential class. –Refine the list to include only the classes that are relevant to the problem. Once the classes have been identified, you need to identify each class’s responsibilities. The responsibilities are: –The things that the class is responsible for knowing –The actions that the class is responsible for doing Copyright © 2012 Pearson Education, Inc.

Example In the textbook, there are three classes: Customer, Car, and ServiceQuote. –The Customer class has the following actions: Create and initialize an object of the Customer class. Get and set the customer’s name. Get and set the customer’s address. Get and set the customer’s telephone number. –The Car class has the following actions: Create and initialize an object of the Car class. Get and set the car’s make. Get and set the car’s model. Get and set the car’s year. –The ServiceQuote class has the following actions: Create and initialize an object of the ServiceQuote class. Get and set the estimated parts charges. Get and set the estimated labor charges. Get and set the sales tax rate. Get the sales tax. Get the total estimated charges. Copyright © 2012 Pearson Education, Inc.

9.6 Creating Multiple Forms in a Project Every project that has a form in a Visual C# has a class. –The default name of the form is Form1 and its class is also named Form1 which is stored in the Form1.cs file. A Visual C# project can have multiple forms. –Each form has its own class that can be instantiated and displayed on the screen. When you add additional forms to a project, you add additional classe(s), which are stored in their own files. When you create event handler for a specific form’s controls, you write them as methods in the form’s class. Copyright © 2012 Pearson Education, Inc.

Displaying a Form To display a form in your application, you need to create an instance of the form’s class. E.g. ErrorForm myErrorForm = new ErrorForm(); The above code: –declares a reference variable named myErrorForm; –creates an object of the ErrorForm class in memory; and –assigns a reference to the object to the myErroForm variable. After creating the instance, use ShowDialog method to display the form. E.g. myErrorForm.ShowDialog(); The ShowDialog method displays a form on the screen and it gives that form the focus. Copyright © 2012 Pearson Education, Inc.

Accessing Control on a Different Form Controls provided by the.NET Framework have public access, even when they are placed on a form. Code that is outside the form’s class can still have access to these controls. For example, you can create an instance of GreetingsForm, yet assign a value to a Label control of another form. GreetingsForm greetingsForm = new GreetingsForm(); greetingsForm.messageLabel.Text = “Good day!”; greetingsForm.ShowDialog(); Copyright © 2012 Pearson Education, Inc.

Modal and Modeless Forms A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application. –When a modal form is displayed, no other form in the application can receive the focus until the modal form is closed. A modeless form allows the user to switch focus to another form while it is displayed. –The user does not have to close a modeless form to switch focus to another form. Copyright © 2012 Pearson Education, Inc.

Flow of Executions Copyright © 2012 Pearson Education, Inc. In a modal form, statement next to the ShowDialog method will not execute until the modal form is closed. E.g. statement; messageForm.ShowDialog(); statement; statement; // not executed statement; In a modeless form, statement next to the Show method will execute immediately after the modeless form is displayed. E.g. statement; messageForm.ShowDialog(); statement; statement; // executed statement;