Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understand the Fundamentals of Classes

Similar presentations


Presentation on theme: "Understand the Fundamentals of Classes"— Presentation transcript:

1 Understand the Fundamentals of Classes
LESSON 2.1 Software Development Fundamentals Understand the Fundamentals of Classes

2 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

3 Guiding Questions What are the key components of a class in object-oriented programming? How do you implement a class and use it within the context of an overall program? Use the guiding questions as a class starter, allowing the students time to answer the questions in their journals. Discuss student answers to the questions.

4 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?

5 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.

6 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.

7 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. Properties do not have to be primitive data types.

8 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.

9 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.

10 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. A method is a code block containing a series of statements. Every executed instruction is done so in the context of a method.

11 //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. Constructors are public and have no return type.

12 //code continued public void magnify(int factor) { length = length
//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.

13 Inside a Method public void magnify (int factor) { length = length * factor; width = width * factor; } Access modifier Return type Method name Parameter An access modifier restricts the access to the class member. There are four access levels: public, private, protected, and internal. These are reviewed in a later lesson.

14 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); } } Console.WriteLine—writes the specified data, followed by the current line terminator, to the standard output stream.

15 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); } }

16 Lesson Review Define the following terms as they apply to object-oriented programming: Object Class Property Method Constructor This is the last slide of the presentation.


Download ppt "Understand the Fundamentals of Classes"

Similar presentations


Ads by Google