Simple Classes in C# CSCI 293 September 12, 2005.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
C++ Classes & Data Abstraction
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
CSCI-383 Object-Oriented Programming & Design Lecture 14.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
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.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
CIS162AD Constructors Part 2 08_constructors.ppt.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Comp1004: Building Better Objects II Encapsulation and Constructors.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Andrew(amwallis) Classes!
Re-Intro to Object Oriented Programming
Classes and Objects.
Static data members Constructors and Destructors
By Muhammad Waris Zargar
Examples of Classes & Objects
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review: Two Programming Paradigms
Creating Your OwnClasses
Object Oriented Analysis and Design
Corresponds with Chapter 7
Automatics, Copy Constructor, and Assignment Operator
Overloading and Overriding
Classes & Objects: Examples
Inherited Classes in Java
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
The University of Texas – Pan American
Simple Classes in Java CSCI 392 Classes – Part 1.
Assessment – Java Basics: Part 1
Introduction to Classes and Objects
Instance Method – CSC142 Computer Science II
CIS 199 Final Review.
NAME 436.
COP 3330 Object-oriented Programming in C++
String Class.
Review for Midterm 3.
Constructors & Destructors
Inheritance in C++ Inheritance Protected Section
Object Oriented Programming (OOP) Lecture No. 12
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Simple Classes in C# CSCI 293 September 12, 2005

Today's Objectives Terminology Scope of Variables and Methods public, private, and more Constructors, Initializers, and Destructors Instance Variables and Class (static) Variables

Terminology of C# Classes an object is a particular instance of a class class = elephant, object = Dumbo Classes contain methods - functions fields - member variables properties - methods that looks like fields to users of the method

Limiting Scope of Fields Given a class named "list" with a variable named "size" public int size; everyone can see size private int size; only list objects can see size, default is private protected int size; list objects and objects derived from list can see size internal and protected internal

Constructors Just like C++ Constructors are optional in C# public class list { private int size; // variable public list () // constructor size = 0; } Constructors are optional in C# this constructor sets size=0, which is the default anyway

Initializers Since constructors are optional… public class list { private int maxsize = 10; // set default public list () maxsize = 10; // redundant } public list (int newmax) maxsize = newmax; // override default

Creating an Object Don't forget to call new() class MainProgram { static void Main() list mylist1 = new list(); list mylist2 = new list(50); ...

Passing Parameters void find_max (int size, ref int max); { search the array and set max } ... // pass 10 by value and new_max by reference find_max (10, ref new_max);

"this" = this object public class list { private int size; ... public set_size (int size) this.size = size; }

Instance Variables vs Class Variables Sometimes, you need a variable that belongs to the entire class, not one variable for each object. public class WarningBox { private string message; private static int boxcount; Each WarningBox instance has its own message, but there is only one boxcount for all WarningBox objects.

Static Constructors Operate on the class, not an individual object. Only called once, even with multiple instances of a class. Not allowed to access non-static variables. public class WarningBox { private string message; private static int boxcount; static WarningBox() boxcount = 0; }

Destructors Not necessary to call free(), because garbage collection is automatic. Example Destructor ~WarningBox() { boxcount--; }

Properties Properties look like variables to the class user, but they look like methods to class programmer. private int securitylevel; // object variable ... public int SecurityLevel // property { get //search database for level return level; } set { securitylevel = value; } --- inside main() --- mymessage.SecurityLevel = 5;

Q u i z Are variables and methods public or private by default? Why do I need a constructor if I can initialize fields when I declare them? How is a "class" variable declared? Given "private static int BoxCount" is this legal: "this.BoxCount = 0;" ?