C# Classes and Inheritance CNS 3260 C#.NET Software Development.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Road Map Introduction to object oriented programming. Classes
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
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++
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Java Unit 9: Arrays Declaring and Processing Arrays.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Pointer Data Type and Pointer Variables
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
ArrayList, Multidimensional Arrays
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
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.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Introduction to Object-Oriented Programming Lesson 2.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Properties and Indexers 9. © Aptech Ltd.Building Applications Using C# / Session 92 Objectives  Define properties in C#  Explain properties, fields,
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP: Encapsulation &Abstraction
Static data members Constructors and Destructors
Module 5: Common Type System
Classes.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Arrays in Java What, why and how Copyright Curt Hill.
Object Oriented Programming in java
Object-Oriented Programming
Arrays in Java.
CIS 199 Final Review.
Java Programming Language
Review for Midterm 3.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

C# Classes and Inheritance CNS 3260 C#.NET Software Development

Questions for Project 2 does the params array load the integers in sequentially (left to right, top to bottom)? yes Can you overload operator= or operator new? no Can I provide multiple overloads for one operator? yes (and you should) do I need to allow for any order of for the scalar operations (int + Matrix and Matrix + int) It’s easy to do, but I only expect you to do one order (doesn’t matter to me which one it is)

Points to review Immutable strings Inheritance Virtual

Immutable strings Immutable means the string does not change when an operation is performed on it. How do you change a string? After the Replace function what does str contain? “Welcome” To do it right you must reassign to str. str now contains: “We-me” string str = “Welcome”; str.Replace(“lco”, “-”); string str = “Welcome”; str = str.Replace(“lco”, “-”);

Immutable strings static void Main() { string s1 = "original"; ChangeMyString(s1); } static void ChangeMyString(string str) { str = "changed"; } Passing an immutable string to a function After calling ChangeMyString(string str), s1 contians “original”

Immutable strings (again) static void Main() { string s1 = "original"; ChangeMyString(ref s1); } static void ChangeMyString(ref string str) { str = "changed"; } Passing an immutable string to a function After calling ChangeMyString(ref string str), s1 contians “changed”

Access specifiers public: anyone can access protected: only the class and derived classes can access private: only the class can access

Review Overriding vs. Hiding public class Base { protected void fun1(){} protected virtual void fun2(){} } public class Derived : Base { protected new void fun1(){} protected override void fun2(){} } Hides base.fun1() Overrides base.fun1()

Overriding static void Main() { Base b = new Derived(); b.fun1(); b.fun2(); } Calls Base.fun1() Calls Derived.fun2() The vtable is used during runtime to resolve virtual function calls.

Overloading a method Overloading vs. Overriding or hiding Overriding: a derived class method covers (you can override or hide a base class method with a method of the same name.) Overloading: Creating methods of the same name that take a different set of parameters C# allows no default parameters You may change access specifiers on various overloaded methods You cannot overload on the return type alone You can overload a base class method in a derived class Only if the signatures match does it hide the base class method

Overloading Example public string GetValue(string str) { // do work here } public float GetValue(float f) { // do work here } public string GetValue(string str, int index) { // do work here }

Design consideration public Token GetToken() { // do some work here return nextToken; } public Token GetToken(string name) { // don't duplicate work if you can help it Token t = GetToken(); if(t.Name != name) throw(new UnexpectedTokenException()); return t; } Avoid duplicating code:

Properties Look like variables Smell like variables Taste like vegetables? Used like variables BUT are really methods getters -- accessors setters -- mutators

Property Example class MyPropDemo { private int myValue; public int MyValue { get{ return myValue; }//accessor set{ myValue = value; }//mutator }

The value keyword Implicit variable passed to properties and indexers Type is the same as the return type of the property or indexer May be used as a local-variable name or a field class MyPropDemo { private int myValue; public int MyValue { set { myValue = value; }// mutator get { return myValue; }// accessor } type is int

Properties Method in disguise Allows us to change object state Syntactical sugar Allows to control access outside the class: class MyPropDemo { private int myValue; public int MyValue { set { myValue = value; }// mutator get { return myValue; }// accessor }

Recursive Properties Be careful with syntax not to recall the property from within the property class MyClass { private int myClassInt; public int MyClassInt { get { return MyClassInt; // Uh oh } set { myClassInt=value; } }

virtual, abstract, static? yes, yes and yes: public virtual int MyInt { get { return myInt; } set { myInt=value; } } abstract public int MyInt { get; set; } public static int MyInt { get { return myStaticInt; } set { myStaticInt=value; } }

Why not just use public variables? You can have extra functionality in a property if needed: public int MyInt { set { if(value < 0) throw(new SomeExceptionThatMakesSenseHere("Duh")); DoSomeWork(value); DontEvenHaveToInitializeAVariableIfYouDontWant(); } get{ return myInt; } }

Why not just use public variables? Can have just a get or just a set: public int Count { get{return count;} }

Why not just use public variables? Allows you to protect data in a multi-threaded situation: public int MyInt { set { lock(this) { myInt=value; }

Why not just use public variables? Allows you to disguise complex lookups as simple or foreign variables as your own: private int Count { get { return ((SomeObject as SomeOtherObject)[i, j] as object).Container.Count; }

What’s faster (See PropertyVSMemberSpeedTest demo)

New in version 2.0? standard 2.7 working draft p292 allows for changing the access specifier on the get or set or both: class MyPropDemo { private int myValue; public int MyValue { get{ return myValue; }//accessor protected set{ myValue = value; }//mutator }

A quick look at arrays Arrays are stored in contiguous memory Arrays are objects Multidimensional arrays are either jagged or rectangular Single dimensional array syntax: All elements are zeroed (nulled for reference types) int[] intArray = new int[10];

Jagged Arrays int[][] intArray = new int[10][]; for(int i=0; i<10; ++i) intArray[i] = new int[15+i]; An array of arrays 2 nd rank can be an array of any length Works the same for n dimensions Index into the array: intArray[3][5] = 100;

Rectangular Arrays 2 nd rank are all of the same length Works the same for n dimensions Index into the array: int[,] intArray = new int[10,15]; int[,,,] intArray = new int[10,15,6,5]; intArray[3,5] = 100; intArray[4,5,3,2] = 200;

Indexers class MyIndexDemo { private int[] iArray = {10,20,30,40,50}; public int this[int index] { get{ return iArray[index]; } set{ iArray[index] = value; } } Similar to operator[] in C++ Similar to properties in C#

Indexer Allows array-like access into a class Good if you want you’re class to work as a collection Can have get and set, or just one or the other Can be overloaded Can’t have ref or out parameters

Overloading an indexer class MyClass { System.Collections.Hashtable table = new System.Collections.Hashtable(); public object this[int i] { get { return table[i]; } set { table[i] = value; } } public object this[string i] { get { return table[i]; } set { table[i] = value; } }

Jagged-style brackets class Class2 { private int[][] int2DArray = new int[10][]; public int[] this[int index] { get { return int2DArray[index]; } set { int2DArray[index] = value; } } void Main() { Class2 c = new Class2(); c[4][5] = 100; }

Rectangular-style Indexer class Class3 { private int[,] int2DRectangularArray = new int[10, 10]; public int this[int x, int y] { get { return int2DRectangularArray[x, y]; } set { int2DRectangularArray[x, y] = value; } } void Main() { Class3 c = new Class3(); c[4, 5] = 100; } }