Classes In C#.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

C++ Classes & Data Abstraction
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Tutorial 6 February 4th/5th, 2015
Road Map Introduction to object oriented programming. Classes
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
ASP.NET Programming with C# and SQL Server First Edition
Object Oriented Software Development
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Written by: Dr. JJ Shepherd
Re-Intro to Object Oriented Programming
Creating Your Own Classes
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Understand the Fundamentals of Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Table of Contents Class Objects.
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
More about OOP and ADTs Classes
Creating Your OwnClasses
Object Oriented Analysis and Design
User-Defined Classes and ADTs
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Building Java Programs
Encapsulation & Visibility Modifiers
Corresponds with Chapter 7
An Introduction to Java – Part II
More about OOP and ADTs Classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Learning Objectives Classes Constructors Principles of OOP
More on Classes and Objects
CS2011 Introduction to Programming I Objects and Classes
Today’s topics UML Diagramming review of terms
Defining Classes and Methods
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
Object-Oriented Programming
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects CGS3416 Spring 2019.
Classes.
Creating and Using Classes
Day 11 The Last Week!.
Presentation transcript:

Classes In C#

Namespaces and Classes Classes can be contained in a Namespace, but everything else must be contained in a class. namespace MyProgram { class Mileage }

Definition A Class is an abstract description of an object For example, A customer class would describe the typical attributes and actions of a customer. A customer would have a name, address, phone etc. An object is a particular instance of the class in your program, for instance the customer Robert Jones who lives at 121 Dexter bld. With the phone number 206.555.3212

Elements of a class A class definition can contain Fields Properties (or accessor, mutator methods) Methods Constructor(s)

Fields Fields are class level variables that represent some attribute of the class, like customerName. In good design these fields are private, which means they are only accessible within the class This is to enforce the Object Oriented principle of “Encapsulation.”

Encapsulation Encapsulation means that: A class should be as self contained and independent as possible. It should protect its inner workings and variables from uncontrolled change by other classes or programs Ideally, a class should be a component that can just be plugged in where ever it is appropriate

Properties Properties (or accessor/mutator methods) are a way of controlling how other classes can use the classes private fields. In C# properties do not take parameters and so do not have ( ) following the property name Properties are special public methods that allow a user of the class to “get” a variables value, or to “set” a value You can use a get without a set and visa versa You can use properties to validate the user’s input or to change the output

Methods Methods are things the class can do For instance, in a class that converts Miles to Kilometers the class would contain a method to do that conversion Methods can be public, which means they can be accessed from other classes or they can be private which means they can only be called from within the class that contains them

Method signatures Methods usually have an accessor like public or private (if none is stated in c# the default is private) They also have a return type like int. If the method will not return anything the type is void If the type is not void the method must contain a return statement The return type is followed by an method name and parenthesis () The parenthesis can contain parameters, values or objects passed to the method from the calling method

Method Signatures Examples public void Display() { } private double CalculateGPA(int totalCredits, double weightedAverage)

Constructors Constructors are special methods that “Construct” the class when it is initialized That is, the set all the variables to their initial values and call any methods that need to run to prepare the class for use If no constructor is created, C# will create a default constructor that initializes all numeric values to 0 and all objects to null

Constructor Signatures Constructors have no return type, and are almost always public Constructors always have the same name as the class itself Constructors can take parameters You can have as many constructors as you want in a class as long as each has a different signature Only one constructor will run for any particular instance of a class