Understand the Fundamentals of Classes

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
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.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Visual Programming Fall 2012 – FUUAST Topic: Development environment.
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.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Chapter 10 Introduction to Classes
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Applications Development
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 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Topics Instance variables, set and get methods Encapsulation
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Modern Programming Tools And Techniques-I
Creating and Using Objects, Exceptions, Strings
Creating Your Own Classes
Objects as a programming concept
Object-Oriented Programming Concepts
Classes (Part 1) Lecture 3
Objects as a programming concept
Classes and OOP.
Review What is an object? What is a class?
Intro To Classes Review
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Java Programming: From Problem Analysis to Program Design,
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Creating Your OwnClasses
Understand Windows Forms Applications and Console-based Applications
Can perform actions and provide communication
Can perform actions and provide communication
Defining Classes and Methods
Chapter 4: Writing classes
Implementing Classes Chapter 3.
Can perform actions and provide communication
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Defining Classes and Methods
Review of Previous Lesson
Dr. R Z Khan Handout-3 Classes
CPS120: Introduction to Computer Science
Creating and Using Classes
Presentation transcript:

Understand the Fundamentals of Classes LESSON 2.1 98-361 Software Development Fundamentals Understand the Fundamentals of Classes

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

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.

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?

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.

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.

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.

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.

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.

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.

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

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

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.

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.

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

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.