c classes and inheritance
Download
Skip this Video
Download Presentation
C# Classes and Inheritance

Loading in 2 Seconds...

play fullscreen
1 / 32

C# Classes and Inheritance - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

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?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' C# Classes and Inheritance' - farren


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c classes and inheritance

C# Classes and Inheritance

CNS 3260

C# .NET Software Development

questions for project 2
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
Points to review
  • Immutable strings
  • Inheritance
  • Virtual
immutable strings
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 strings1
Immutable strings
  • Passing an immutable string to a function
  • After calling ChangeMyString(string str), s1 contians “original”

static void Main()

{

string s1 = "original";

ChangeMyString(s1);

}

static void ChangeMyString(string str)

{

str = "changed";

}

immutable strings again
Immutable strings (again)
  • Passing an immutable string to a function
  • After calling ChangeMyString(ref string str), s1 contians “changed”

static void Main()

{

string s1 = "original";

ChangeMyString(ref s1);

}

static void ChangeMyString(ref string str)

{

str = "changed";

}

access specifiers
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
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
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 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
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
Design consideration
  • Avoid duplicating code:

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;

}

properties
Properties
  • Look like variables
  • Smell like variables
  • Taste like vegetables?
  • Used like variables
  • BUT are really methods
    • getters -- accessors
    • setters -- mutators
property example
Property Example

class MyPropDemo

{

private int myValue;

public int MyValue

{

get{ return myValue; } //accessor

set{ myValue = value; } //mutator

}

}

the value keyword
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

properties1
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
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
virtual, abstract, static?
  • yes, yes and yes:

public virtual int MyInt{

get { return myInt; }

set { myInt=value; }}

public static int MyInt

{

get { return myStaticInt; }

set { myStaticInt=value; }

}

abstract public int MyInt

{

get;

set;

}

why not just use public variables
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 variables1
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 variables2
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 variables3
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
What’s faster

(See PropertyVSMemberSpeedTest demo)

new in version 2 0
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
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
Jagged Arrays
  • An array of arrays
  • 2nd rank can be an array of any length
  • Works the same for n dimensions
  • Index into the array:

int[][] intArray = new int[10][];

for(int i=0; i<10; ++i)

intArray[i] = new int[15+i];

intArray[3][5] = 100;

rectangular arrays
Rectangular Arrays
  • 2nd 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
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
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
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
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
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; }

}

ad