Fundamentals of Classes

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 10 months ago)

118 views

Understand the
Fundamentals of Classes


98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Lesson Overview

Students will understand the fundamentals of classes.


In this lesson, you will learn:


Properties, methods, events, and constructors


How to create a class


How to use classes in code

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Guiding Questions

1.
What are the key components of a class in object
-
oriented programming?

2.
How do you implement a class and use it within the context of an overall
program?



98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Activator


Look around the room. What objects do you see that are related
to others?


What general name would you give to each of these groups?


If we were to model a classroom as a computer program, how
would these groups be represented?




98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Activator

Explanation


All the objects discussed interact with one another in the context
of a classroom.


We can represent objects with similar characteristics with a class
by defining properties and behaviors.




98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Review Terms


Class

in object
-
oriented programming, a generalized category
that describes a group of more specific items, called
objects,

that
can exist within it.


Constructor

a method that allows the programmer to set default
values, limit instantiation, and write code that is flexible and easy
to read.


Event

an action or occurrence, often generated by the user, to
which a program might respond (for example, key presses, button
clicks, or mouse movements).


Method

in object
-
oriented programming, a process performed
by an object when it receives a message.




98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Review Terms



Object

1. short for object code (machine
-
readable code). 2. In
object
-
oriented programming, a variable comprising both
routines and data that is treated as a discrete entity.


Object
-
oriented programming

a programming paradigm in
which a program is viewed as a collection of discrete objects that
are self
-
contained collections of data structures and routines that
interact with other objects.


Property

members of a class that provide a flexible mechanism
to read, write, or compute the values of private fields. Properties
are viewed or accessed by “accessor” methods within the class
and are changed or modified by “modifier” methods within the
class.




98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Objects and Classes

The Relationship


Objects are instances of a given data type. The data type provides a
blueprint for the object that is created

or instantiated

when the
application is executed.


New data types are defined using classes.


Classes form the building blocks of C# applications, containing
code and data. A C# application will always contain at least one
class.

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Objects and Classes

A Comparison


An object belongs to a class and is an instance of that class. The
class is the blueprint of the object and defines the fields and
methods.


The object exists during the time that a program executes and must
be explicitly declared and construed by the executing program. For
most programming languages, a class cannot be changed during
program execution.


Classes have fields that can change in value and methods that can
execute during program execution. The class to which the object
belongs defines these attributes and methods.

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

public class Rectangle

{

public double length

{


get { return length; }


set


{


if (value > 0)


length = value;


}


}


public double width

{


// code not shown

Properties can be used as
though they are public data
members, but they are
actually special methods
called
accessors.
This
enables data to be accessed
easily while still providing
the safety and flexibility of
methods.

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

//code continued


public Rectangle(double l, double w)

{


length = l;


width = w;

}



// more code follows...

Constructors are class
methods that are executed
when an object of a given
type is created. Constructors
have the same name as the
class and usually initialize
the data members of the new
object.

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

//code continued


public void magnify(int factor)

{


length = length * factor;


width = width * factor;

}


}
// end of class Rectangle

A
method

is a code block
containing a series of
statements. Every executed
instruction is performed in
the context of a method.

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Inside a Method




public void magnify (int factor)

{


length = length * factor;


width = width * factor;

}


Access modifier

Return type

Method name

Parameter

98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Example 1: Using the
Rectangle

Class

public class ShapeProgram

{

public static void Main()

{


Rectangle rec1 = new Rectangle(10,20);


rec1.magnify(3);


Console.WriteLine(“Length ” + rec1.length);


Console.WriteLine(“Width ” + rec1.width);

}

}


98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Example 2: Using the
Rectangle

Class

public class Window

{

Rectangle outerFrame;

Rectangle innerFrame;


public Window ()

{


outerFrame = new Rectangle(10,8);


innerFrame = new Rectangle(8,6);

}


public void enlargeWindow()

{


outerFrame.magnify(2);

}

}


98
-
361 Software Development Fundamentals

L E S S O N

2
.
1

Lesson Review


Define the following terms as they apply to object
-
oriented
programming:


Object


Class


Property


Method


Constructor