0% found this document useful (0 votes)
10 views69 pages

cshtp5 04

The document discusses object-oriented programming concepts like classes, methods, and properties. It uses examples of engineering drawings for cars and bank accounts to explain how classes model real-world objects and how methods and properties describe their behaviors and attributes. Code examples in C# are provided to demonstrate how to define classes and methods and use them in a console application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views69 pages

cshtp5 04

The document discusses object-oriented programming concepts like classes, methods, and properties. It uses examples of engineering drawings for cars and bank accounts to explain how classes model real-world objects and how methods and properties describe their behaviors and attributes. Code examples in C# are provided to demonstrate how to define classes and methods and use them in a console application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 69

Slide 42

Visual C# 2012 How to Program

©1992-2014 by Pearson Education, Inc. All Rights Reserved.


 A car begins as engineering drawings, similar
to the blueprints used to design a house.
 An accelerator pedal “hides” the complex

mechanisms that actually make the car go


faster.
 Before you can drive a car, it must be built

from the engineering drawings that describe


it.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 A method describes the internal mechanisms
that actually perform its tasks.
 A class is used to house (among other things)

a method, just as a car’s engineering


drawings house (among other things) the
design of an accelerator pedal.
 A class that represents a bank account might

contain one method to deposit money in an


account, another to withdraw money from an
account and a third to inquire what the
current account balance is.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Just as you cannot drive an engineering
drawing of a car, you cannot “drive” a class.
 Just as someone has to build a car from its

engineering drawings before you can actually


drive it, you must build an object of a class
before you can make an app perform the
tasks the class describes.
 You send messages to an object by making

method calls.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 A car also has many attributes, such as its
color, the number of doors, the amount of
gas in its tank, its current speed and its total
miles driven.
 These attributes are represented in its

engineering drawings, but every car


maintains its own attributes.
 Attributes are specified by the class’s

instance variables.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Attributes are not necessarily accessible directly.
 Customers talk to a bank teller or check
personalized online bank accounts to obtain their
account balance.
 Similarly, you do not need to have access to an
object’s instance variables in order to use them,
you can use get accessors and set accessors to
manipulate attributes.
 Select File > New Project... and create a GradeBook
Console Application.
 The GradeBook class declaration (Fig. 4.1) contains
a DisplayMessage method that displays a
message on the screen.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Keyword public is an access modifier.
 Access modifiers determine the accessibility of
properties and methods.
 The class’s body is enclosed in a pair of left
and right braces ({ and }).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 The method declaration begins with public to indicate
that the method can be called from outside the class
declaration’s body.
 Keyword void—known as the method’s return type—
indicates that this method will not return information to
its calling method.
 When a method specifies a return type other than void,
the method returns a result to its calling method.
int result = Square( 2 );

 The body of a method contains statements that perform


the method’s task.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 To add a class, right click the project name in
the Solution Explorer and select Add >
New Item….
 In the Add New Item dialog that appears,

select Code File, enter the name of your new


file (GradeBookTest.cs) then click Add.
 The GradeBookTest class declaration

(Fig. 4.2) contains the Main method that


controls our app’s execution.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Any class that contains a Main method can be
used to execute an app.
 A static method is special because it can be

called without creating an object of the class


(in this case, GradeBookTest) in which the
method is declared.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Figure 4.3 presents a UML class diagram for class
GradeBook.
 Classes are modeled as a rectangle with three
compartments.
 The top compartment contains the name of the
class.
 The middle compartment contains the class’s
attributes.
 The bottom compartment contains the class’s
operations.
 The plus sign (+) indicates that DisplayMessage is a
public operation.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 A method can specify parameters, additional
information required to perform its task.
 A method call supplies values—called arguments—for
each of the method’s parameters.
 For example, the Console.WriteLine method
requires an argument that specifies the data to be
displayed in a console window.
 Our next example declares Class GradeBook
(Fig. 4.4) with a DisplayMessage method that displays
the course name as part of the welcome message.
 The new class is used from the Main method of class
GradeBookTest (Fig. 4.5).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 The method’s parameter list is located in the parentheses that
follow
the method name.
 Empty parentheses indicate that a method does not require
any
parameters.
 The argument value in the call is assigned to the
corresponding
parameter in the method header.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 The UML class diagram of Fig. 4.6 models
class GradeBook.
 The UML models DisplayMessage’s

parameter by listing the parameter name and


type.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Classes in the same project are considered to
be in the same namespace.
 using indicates that the app uses classes in

another namespace.
 Without using, we would write the fully

qualified class name:


System.Console.WriteLine(
"Please enter the course
name:" );

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Variables declared in the body of a method are
known as local variables.
 When a method terminates, the values of its local
variables are lost.
 Attributes are represented as variables in a class
declaration.
 When each object of a class maintains its own copy
of an attribute, the field is known as an instance
variable.
 Class GradeBook (Fig. 4.7) maintains the course
name as an instance variable so that it can be used
or modified.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Variables, properties or methods declared
with access modifier private are accessible
only to members (such as properties and
methods) of the class in which they’re
declared.
 Declaring instance variables with access

modifier private is known as information


hiding (or encapsulation).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
•We need to provide controlled ways for
programmers to “get” (i.e., retrieve) and
“set” (i.e., modify) the value of an instance
variable.
•Properties contain get and set accessors
that handle the details of returning and
modifying data.
•After defining a property, you can use it like
a variable in your code.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 The get accessor begins with the identifier
get and is delimited by braces.
 The expression’s value is returned to the client
code that uses the property.
string theCourseName = gradeBook.CourseName;
 gradeBook.CourseName implicitly executes
the get accessor, which returns its value.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 The set accessor begins with the identifier set
and is delimited by braces.
gradeBook.CourseName =
"CS100 Introduction to Computers";
 The text "CS100 Introduction to Computers" is
assigned to the set accessor’s keyword named
value and the set accessor executes.
 A set accessor does not return any data.
 Class GradeBookTest (Fig. 4.8) creates a
GradeBook object and demonstrates property
CourseName.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Unlike local variables, every instance
variable has a default initial value.
 The default value for an instance variable of

type string is null.


 When you display a string variable that

contains the value null, no text is


displayed.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Figure 4.9 contains an updated UML class diagram for
the version of class GradeBook.
 We model properties in the UML as attributes preceded
by the word “property” in guillemets (« and »).
 To indicate that an attribute is private, a class diagram
would list the private visibility symbol—a minus sign (–)
—before the attribute’s name.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 Properties allow the class to control how the data is set
or returned.
 For example, get and set accessors can translate
between the format used by the client and the format
stored in the private instance variable.
 Properties of a class should also be used by the class’s
own methods.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 Notice­that CourseName’s get accessor
simply returns courseName’s value and the
set accessor simply assigns a value to the
instance variable.
 For such cases, C# provides automatically

implemented properties as in
public string CourseName { get; set;
}
 If you later decide to include other logic in
the get or set accessors, you can simply
modify the property’s implementation.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 A variable of a value type (such as int) simply contains
a value of that type (Fig. 4.10).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 A reference-type variable (sometimes called a reference)
contains the address of a location in memory where the data
referred to by that variable is stored.
 Such a variable is said to refer to an object in the program.
 Line 11 of Fig. 4.8 creates a GradeBook object, places it in
memory and stores the object’s reference in variable
myGradeBook of type GradeBook as shown in Fig. 4.11.
 Reference-type instance variables are initialized by default to
the value null.
 A client of an object must use a variable that refers to the
object to invoke (i.e., call) the object’s methods and access
the object’s properties.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
• Each class can provide a constructor to
initialize an object of a class when the object
is created.
• The new operator calls the class’s constructor
to perform the initialization.
• The compiler provides a public default
constructor with no parameters, so every
class has a constructor.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
 When you declare a class, you can provide
your own constructor to specify custom
initialization:
GradeBook myGradeBook = new GradeBook(
"CS101 Introduction to C#
Programming" );
 "CS101 Introduction to C# Programming"
is passed to the constructor.
 Figure 4.12 contains a modified GradeBook class
with a custom constructor.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 A constructor must have the same name as
its class.
 Like a method, a constructor has a

parameter list.
 Figure 4.13 demonstrates initializing

GradeBook objects using the constructor.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 The UML class diagram of Fig. 4.14 models class
GradeBook.
 To distinguish a constructor from other operations, the
UML places the word “constructor” between guillemets
( « and » ).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 C# provides three simple types for storing real numbers—
float, double, and decimal.
 Types float and double are called floating-point types.
 decimal variables store a limited range of real numbers
precisely, whereas floating-point variables store only
approximations of real numbers, but across a much
greater range of values.
 To type a decimal literal, you must type the letter “M” or
“m” at the end of a real number.
 A class named Account (Fig. 4.15) maintains the balance
of a bank account.
 AccountTest (Fig. 4.16) creates two Account objects and
initializes them with 50.00M and -7.53M (decimal literals).

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 A value output with the format item {0:C}
appears as a monetary amount.
 The : indicates that the next character
represents a format specifier.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
 It’s possible to declare the get and set accessors with
different access modifiers.
 One of the accessors must implicitly have the same
access as the property and the other must be declared
with a more restrictive access modifier.
 The UML class diagram in Fig. 4.18 models class
Account.

©1992-2014 by Pearson Education, Inc. All


Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson Education, Inc. All Rights
Reserved.

You might also like