lecture 4 overview of classes, containers & generics

52
Lecture 4 Overview of Classes, Containers & Generics

Upload: rupali

Post on 19-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Lecture 4 Overview of Classes, Containers & Generics. What is a Programming Object?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 4 Overview of Classes, Containers & Generics

Lecture 4Overview of Classes, Containers & Generics

Page 2: Lecture 4 Overview of Classes, Containers & Generics

What is a Programming Object?

An object is an instance of a class. A class is a template for an object. Everything's an object!!! Statements such as these are not all that helpful when we are struggling to understand the basic concepts of object-oriented programming (OOP).

It is better to look as some examples of objects, such as a textbox, a push-button or some user-defined non-GUI object like a cardDeck. Each of these objects has properties that can be accessed and sometimes manipulated. Multiple instances can be defined for them, and there can be different characteristics for each instance.

hello there... Start

A programming object is a collection of data (variables or literal constants) and a set of functions that operate on the data. Programming objects give us the illusion of material objects and can be used to simplify the task of program design.

Page 3: Lecture 4 Overview of Classes, Containers & Generics

Life Cycle of an Object

Every object has a clearly defined life cycle. Apart from the normal state of "being in use," this life cycle includes two important stages:

Construction: When an object is first instantiated it needs to be initialized. This initializaztion is known as construction and is carried out by a constructor function.

Destruction: When an object is destroyed, there are often some clean-up tasks to perform, such as freeing memory. This is the job of a destructor function.

Note: The actions of the destructor function may not occur immediately, so we cannot rely on this function to release memory when we need it. Later we will look into the use of disposable objects to help manage critical resources.

Page 4: Lecture 4 Overview of Classes, Containers & Generics

Class Members

The methods (functions) and data in a class are called class members.

Data Members - Data members are instance members unless they are explicitly designated as static. Static data values are associated with the class as a whole. Each instance of a class has its own copy of an instance data value. Data members can be fields, constants, and events.

Function Members - Function members perform some operation on the data members of the class. Function members can be methods, properties, constructors, finalizers, operators, and indexers.

Page 5: Lecture 4 Overview of Classes, Containers & Generics

Methods

Are the standard form for functions and procedures. They may have zero or more arguments, they can be either instance or static, and they may or may not return a data value.

public static void SomeMethod(){ // stuff}

public static int SomeOther(){ return // some integer}

public int YetAnother(int val){ // so something with val return //some integer}

public void OneMore(ref int val){ // change the value of val}

Page 6: Lecture 4 Overview of Classes, Containers & Generics

Accessors for Properties & FieldsThese are sets of functions that can be accessed from the client through dot-notation. Properties can do the same type of things that methods can do, except they have their own syntax (e.g. get and set).

public string SomeProperty{ get { return // the value of the property } set { //set the property }}

public string SomeOtherProperty{ get { return // the value }}

Page 7: Lecture 4 Overview of Classes, Containers & Generics

Constructors

public class TheClassName{ public TheClassName { // stuff to do the construction } // other stuff}

public class AnExample{ public AnExample() { // default constructor stuff }

public AnExample(int num) { // constructor stuff that uses num }}

The constructor has the same name as the class and it initializes data or does whatever is needed to be done when a class instance is created.

CupOfCoffee myCup = new CupOfCoffee( );CupOfCoffee myCup = new CupOfCoffee("Maxwell House - Original",false,true);

CupOfCoffee(string coffeebrand, bool cream, bool sugar)

Page 8: Lecture 4 Overview of Classes, Containers & Generics

Finalizers (Destructors)

The finalizer has the same name as the class (preceded by a tilde, ~) and it releases data or does whatever is needed to be done when a class instance is disposed.

class SampleClass{ ~SampleClass() { // destructor stuff }}

"Destructors are used by the .NET Framework to clean up after objects. In general, you don't have to provide code for a destructor method; instead, the default operation works for you."

We cannot rely on the destructor to free up resources that are used by an object instance, as this may be a long time after the last time the object is used. Instead we can create Disposable Objects.

Page 9: Lecture 4 Overview of Classes, Containers & Generics

Operators

Operators are infix functions usually defined by one or two symbols. Examples are =, +, >=, &&, and so on. In C# we can overload operators for user-defined data types.

public double x,y,z;public Vec_3(double x, double y, double z){ this.x = x; this.y = y; this.z = z;}

public static Vec_3 operator +(Vec_3 lhs, Vec_3 rhs) { Vec_3 result = new Vec_3(lhs);

result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; }

public Vec_3(Vec_3 rhs){ x = rhs.x; y = rhs.y; z = rhs.z;}

Page 10: Lecture 4 Overview of Classes, Containers & Generics

Indexers

Indexers are used like index values to gain access into an array, except that they can refer to an object. Instead of creating a name, you use the this keyword, which refers the the current object.

public somedatatype this[int index]{ get { // do something return some_value; } set { //set a value in the class related to index }}

Page 11: Lecture 4 Overview of Classes, Containers & Generics

Inheritance

There are two types of inheritance, implementation and interface. We will review implementation inheritance first, since it is the more common form of inheritance.

In implementation inheritance, a type derives from a base type, taking all the base type's member fields and functions.

class ExampleClass : object{ // stuff}

class ExampleClass{ // same stuff}

these are the same

Page 12: Lecture 4 Overview of Classes, Containers & Generics

Virtual Methods

Declaring a method as virtual allows it to be overidden by any class that is derived from the defining class. Virtual members cannot be private, as this would cause a paradox -- a member cannot be overridden by a derived class and yet be inaccessible from the derived class.

class SomeBaseClass{ public vitual void SomeVirtualMethod() { // method stuff }}

Page 13: Lecture 4 Overview of Classes, Containers & Generics

Overridding a Virtual Methodin a Derived Class

class BaseGraphicsClass{ public virtual void DrawLine() { } public virtual void DrawPoint() { }}

class NewDerivedClass : BaseGraphicsClass{ public override void DrawPoint() { }}

Instances of NewDerivedClass have access to the DrawLine() method as well as the new version of the DrawPoint() method.

Page 14: Lecture 4 Overview of Classes, Containers & Generics

OOP in Wndows Applications

Page 15: Lecture 4 Overview of Classes, Containers & Generics
Page 16: Lecture 4 Overview of Classes, Containers & Generics
Page 17: Lecture 4 Overview of Classes, Containers & Generics
Page 18: Lecture 4 Overview of Classes, Containers & Generics
Page 19: Lecture 4 Overview of Classes, Containers & Generics

namespace ButtonMakerDemo{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void newButton_Click(Object sender, System.EventArgs e) { ((Button) sender).Text = "Clicked!!"; }

private void button1_Click(object sender, EventArgs e) { ((Button)sender).Text = "Clicked!"; Button newButton = new Button(); newButton.Click += new EventHandler(newButton_Click); Controls.Add(newButton); } }}

There is no Magic

Page 20: Lecture 4 Overview of Classes, Containers & Generics

Classes and Interfaces Demonamespace ClassInterfaceDemo{ public abstract class MyBase { }

internal class MyClass : MyBase { }

public interface IMyBaseInterface { }

public interface IMyBaseInterface2 { }

internal interface IMyInterface : IMyBaseInterface, IMyBaseInterface2 { }

internal sealed class MyComplexClass : MyClass, IMyInterface { }

class Program { static void Main(string[] args) { MyComplexClass myObj = new MyComplexClass(); Console.WriteLine(myObj.ToString()); Console.ReadKey(); } }}

Page 21: Lecture 4 Overview of Classes, Containers & Generics

The Class Diagram

Page 22: Lecture 4 Overview of Classes, Containers & Generics

System.Object Methods

Page 23: Lecture 4 Overview of Classes, Containers & Generics

Collections

Page 24: Lecture 4 Overview of Classes, Containers & Generics
Page 25: Lecture 4 Overview of Classes, Containers & Generics
Page 26: Lecture 4 Overview of Classes, Containers & Generics

Console.WriteLine("Create an Array type collection of Animal " + "objects and use it:"); Animal[] animalArray = new Animal[2]; Cow myCow1 = new Cow("Deirdre"); animalArray[0] = myCow1; animalArray[1] = new Chicken("Ken"); foreach (Animal myAnimal in animalArray) { Console.WriteLine("New {0} object added to Array collection, " + "Name = {1}", myAnimal.ToString(), myAnimal.Name); }

Console.WriteLine("Array collection contains {0} objects.", animalArray.Length); animalArray[0].Feed(); ((Chicken)animalArray[1]).LayEgg(); Console.WriteLine();

Main Program (Animal Array)

Page 27: Lecture 4 Overview of Classes, Containers & Generics

Console.WriteLine("Create an ArrayList type collection of Animal " + "objects and use it:"); ArrayList animalArrayList = new ArrayList(); Cow myCow2 = new Cow("Hayley"); animalArrayList.Add(myCow2); animalArrayList.Add(new Chicken("Roy")); foreach (Animal myAnimal in animalArrayList) { Console.WriteLine("New {0} object added to ArrayList collection," + " Name = {1}", myAnimal.ToString(), myAnimal.Name); }

Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count); ((Animal)animalArrayList[0]).Feed(); ((Chicken)animalArrayList[1]).LayEgg(); Console.WriteLine(); Console.WriteLine("Additional manipulation of ArrayList:"); animalArrayList.RemoveAt(0); ((Animal)animalArrayList[0]).Feed(); animalArrayList.AddRange(animalArray); ((Chicken)animalArrayList[2]).LayEgg(); Console.WriteLine("The animal called {0} is at index {1}.", myCow1.Name, animalArrayList.IndexOf(myCow1)); myCow1.Name = "Janice"; Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);

Main Program (Animal ArrayList Collection)

Page 28: Lecture 4 Overview of Classes, Containers & Generics

Limitations of Arrays

Page 29: Lecture 4 Overview of Classes, Containers & Generics

ArrayList Collection

Page 30: Lecture 4 Overview of Classes, Containers & Generics

The foreach Construct

Page 31: Lecture 4 Overview of Classes, Containers & Generics

Ienumerable is Implicit in foreach

Page 32: Lecture 4 Overview of Classes, Containers & Generics

Iterators in the .NET Framework are called "enumerators" and represented by the IEnumerator interface. IEnumerator provides a MoveNext() method, which advances to the next element and indicates whether the end of the collection has been reached; a Current property, to obtain the value of the element currently being pointed at; and an optional Reset() method, to rewind the enumerator back to its initial position. The enumerator initially points to a special value before the first element, so a call to MoveNext() is required to begin iterating.

Enumerators are typically obtained by calling the GetEnumerator() method of an object implementing the IEnumerable interface. Container classes typically implement this interface. However, the foreach statement in C# can operate on any object providing such a method, even if it doesn't implement IEnumerable. Both interfaces were expanded into generic versions in .NET.

The following shows a simple use of iterators in C# :// explicit versionIEnumerator<MyType> iter = list.GetEnumerator();while (iter.MoveNext()) Console.WriteLine(iter.Current); // implicit versionforeach (MyType value in list) Console.WriteLine(value);

http://en.wikipedia.org/wiki/Iterator

C# Iterators – alternative definition

Page 33: Lecture 4 Overview of Classes, Containers & Generics

The Length & Count Properties

Page 34: Lecture 4 Overview of Classes, Containers & Generics

Collections Accessibility

Page 35: Lecture 4 Overview of Classes, Containers & Generics

Other Useful Methods in Collections Class

Page 36: Lecture 4 Overview of Classes, Containers & Generics

AddRange( )

Page 37: Lecture 4 Overview of Classes, Containers & Generics

IndexOf( )

Page 38: Lecture 4 Overview of Classes, Containers & Generics

The Generic List<T>loading data from a file

using System.IO; : :namespace EmpListDemo{ static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline;

List<Employee> Emps = new List<Employee>(); // a generic list of Employee List<Employee> Emps2 = new List<Employee>(); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));

} while (true); tr.Close(); : :

Page 39: Lecture 4 Overview of Classes, Containers & Generics

// display the contents of the list Empsforeach (Employee emp in Emps){ Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Displaying the Contents of the List Emps

Page 40: Lecture 4 Overview of Classes, Containers & Generics

// we are making a copy of Emps called Emps2foreach (Employee emp in Emps){ Employee emp2 = new Employee(); emp2 = emp; Emps2.Add(emp2);}// so why not just assign one list to the other?// Emps2 = Emps;//// because this would not make a separate copy but// rather point both Emps2 and Emps to the same records!

Making a Copy of a List

Page 41: Lecture 4 Overview of Classes, Containers & Generics

// we "tag" each record that passes our criteriaforeach (Employee emp in Emps2){ if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true;}

// now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false// this construct is implemented using a delegateEmps2.RemoveAll(delegate(Employee emp){ return !emp.Tag;});

The RemoveAll Delegate Method

age>39 and yrsemp >= 10Wade Boggs 45 20 12.5Amanda Rekonwith 55 25 22.5

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Page 42: Lecture 4 Overview of Classes, Containers & Generics

// this is a LINQ query!var queryresults = from q in Emps where q.LastName.StartsWith("B") select q;

// so what's in queryresults???// let's take a lookConsole.WriteLine();foreach (var q in queryresults){ Console.WriteLine(q.FirstName + " " + q.LastName);}

Working with LINQ

Wade BoggsRobin BanksAnita Break

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Page 43: Lecture 4 Overview of Classes, Containers & Generics

// let's try another example // notice we don't redefine the var queryresults queryresults = from q in Emps where q.Age>30 select q;

// now what's in queryresults??? Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Console.ReadKey();

Another LINQ Query Example

Wade BoggsRobin BanksAmanda RekonwithDoug WellsJuan AbrewBen Dover

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Page 44: Lecture 4 Overview of Classes, Containers & Generics

// one more examplequeryresults =from q in Empswhere ((q.Age>40 & q.Wage<20.0) | (q.Age<40 & q.Wage>25.0))select q;

Wade BoggsDoug Wells

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Page 45: Lecture 4 Overview of Classes, Containers & Generics

5 6 4 3 2 7 7 8 6 54 3 2 5 3 2 3 8 8 81 1 2 2 3 3 6 4 5 33 5 5 6 4 9 7 5 2 04 3 8 7 0 4 3 2 5 41 3 2 4 3 5 4 6 5 77 5 6 8 7 7 5 4 7 91 3 2 4 3 6 5 4 3 28 8 9 6 5 5 3 0 0 11 1 1 6 6 8 8 7 6 5

using System.IO;using System.Collections.Generic;using System.Linq;using System.Text;

namespace LoadArrayFromTextfile{ class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k;

TextReader tr = new StreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } } } tr.Close();

Loading an Array from a Text File

for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); } }}

Page 46: Lecture 4 Overview of Classes, Containers & Generics

List<Employee> Emps = new List<Employee>();string txtline;

TextReader tr = new StreamReader("employees.txt");

do{ txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));

} while (true);tr.Close();

Reading and Writing Textfiles

Page 47: Lecture 4 Overview of Classes, Containers & Generics

foreach (Employee emp in Emps){ Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName);}

TextWriter tw = new StreamWriter("employees.txt");foreach (Employee emp in Emps){ tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}tw.WriteLine("xxx");tw.Close();

Reading and Writing Text Filescontinued

Page 48: Lecture 4 Overview of Classes, Containers & Generics

Dealing with Datacut & paste

Page 49: Lecture 4 Overview of Classes, Containers & Generics

Pasting into Word or PPT Preserves Cells

Page 50: Lecture 4 Overview of Classes, Containers & Generics

Pasting to a Text Editor Creates Separators e.g. Tabs

Page 51: Lecture 4 Overview of Classes, Containers & Generics
Page 52: Lecture 4 Overview of Classes, Containers & Generics