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.

Slides:



Advertisements
Similar presentations
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 9 Concepts of Programming Languages
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CPS120: Introduction to Computer Science Functions.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
ECE122 Feb. 22, Any question on Vehicle sample code?
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Introduction to Object-Oriented Programming Lesson 2.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Classes - Intermediate
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Methods.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes (Part 1) Lecture 3
Understand the Fundamentals of Classes
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Classes.
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Group Status Project Status.
Implementing Non-Static Features
Flow of Control.
Object Oriented Programming in java
CIS 199 Final Review.
Classes, Objects and Methods
Corresponds with Chapter 5
Creating and Using Classes
Presentation transcript:

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 type. Generally, you should use fields only for variables that have private or protected accessibility. Data that your class exposes to client code should be provided through methods, properties and indexers. A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. Indexers allow instances of a class to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

Allocating Memory for a Class Declaring the variable of the class type allocates the memory to hold the reference, but not the memory to hold the actual data of the class object. To allocate memory for the actual data, you use the new operator. The new operator allocates and initializes memory for an instance of the specified type. It allocates the memory from either the stack or the heap, depending on the type. Use the new operator to form an object-creation expression, which consists of the following: The keyword new. The name of the type of the instance for which memory is to be allocated. Matching parentheses, which might or might not include parameters. If the memory allocated is for a reference type, the object-creation expression returns a reference to the allocated and initialized instance of the object in the heap.

A class declaration acts as a blueprint from which you can create as many instances of the class as you like. Instance members: Each instance of a class is a separate entity that has its own set of data members, distinct from the other instances of the same class. These are called instance members since they are associated with an instance of the class. Static members: Instance members are the default, but you can also declare members called static members, which are associated with the class, not the instance. Instance Members

Public and Private Access Public members of an instance are accessible to other objects in the program. You must use the public access modifier to specify public access.

Accessing Members from Inside the Class

Accessing Members from Outside the Class

Putting It All Together

You can call other methods from inside a method body. The phrases call a method and invoke a method are synonymous. You call a method by using its name, along with the parameter list, which I’ll discuss shortly. For example, the following class declares a method called PrintDateAndTime, which is called from inside method Main: Method Invocations

In the following code, method GetHour is called in the WriteLine statement in Main and returns an int value to that position in the WriteLine statement. Method Returning a Value

Early Returns Often, however, you can simplify your program logic by exiting the method early when certain conditions apply. You can exit a void method at any time by using the following form of the return statement, with no parameters: return; This form of the return statement can be used only with methods declared void.

So far, you’ve seen that methods are named units of code that can be called from many places in a program and can return a single value to the calling code. Returning a single value is certainly valuable, In C# methods can accept and return multiple values. Parameters are a special kind of variable that can allow you to do both these things. Parameters public void moveXY( ref int x, ref int y, ref int vx, ref int vy, int maxx, int maxy) { x += vx; y += vy; if(x>maxx || x<0) vx = -vx; if(y>maxy || v<0) vy = -vy; }

A class can have more than one method with the same name. This is called method overloading. Each method with the same name must have a different signature than the others. The signature of a method consists of the following information from the method header of the method declaration: − The name of the method − The number of parameters − The data types and order of the parameters − The parameter modifiers The return type is not part of the signature—although it’s a common mistake to believe that it is. Notice that the names of the formal parameters are not part of the signature. Method Overloading

Besides instance fields, classes can have what are called static fields. A static field is shared by all the instances of the class, and all the instances access the same memory location. Hence, if the value of the memory location is changed by one instance, the change is visible to all the instances. Instance members come into existence when the instance is created and go out of existence when the instance is destroyed. Static members exist and are accessible even if there are no instances of the class. Static Fields

Besides static fields, there are also static function members. Static function members, like static fields, are independent of any class instance. Even if there are no instances of a class, you can still call a static method. Static function members cannot access instance members. They can, however, access other static members. For example, the following class contains a static field and a static method. Notice that the body of the static method accesses the static field. Static Function Members

In most of the examples so far, the property has been associated with a backing field, and the get and set accessors have referenced that field. However, a property does not have to be associated with a field. In the following example, the get accessor computes the return value. In the code, class RightTriangle represents, not surprisingly, a right triangle. Computed Read-Only Property