C - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

C

Description:

enum Status {On, Off, Manual, Auto}; public enum Status : byte ... int status; const int ON = 1; const int OFF = 2; const int MANUAL = 4; const int AUTO = 8; ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 24
Provided by: Robert9
Category:
Tags: status

less

Transcript and Presenter's Notes

Title: C


1
C Basics
  • CS2335
  • Spring 2007

2
Basic Characteristics
  • Primary language for .Net
  • 60 Java 25 C/C 15 New
  • One class or partial class per file

3
Code Organization
using System ///ltsummarygt ///This is the
standard hello world app ///lt/summarygt namespace
Bob class Hello /
another type comment that spans
multi-lines / public static void Main()
//Single line
comment System.Console.WriteLine(
Hello World)
4
Type Mapping
5
Control Statements (When diff from Java)
  • switch (must have break case can be string)
  • foreach (used in collection iteration)
  • foreach ( int i in intarray)
  • goto (not into a loop or nested block)
  • for ( ) if (loc gt 0) goto Found
  • continue Found m

6
Preprocessor (like C/C)
  • define, undef
  • if, elif, else, endif
  • line
  • region, endregion
  • error, warning
  • pragma

7
Enumerations
  • Can be of several integral types(default is int).
  • enum Status On, Off, Manual, Auto
  • public enum Status byte On 1, Off 2,
    Manual 4, Automatic 8

8
Enumerations help type safety
int status const int ON 1 const int OFF
2 const int MANUAL 4 const int AUTO
8 status ON status 10000.
enum Status Status status status
Status.On status 10000 //compile error!!!!
If powers of 2, can use as bit flags currentStat
us Status.On Status.Manual use Flags
attribute so ToString gives correct output.
9
Attributes
  • Sends information (metadata) to the compiler
    about our code
  • We already mentioned Flags
  • Obsolete
  • Serializable
  • We can define our own.

10
Arrays (Just like Java, except multi-dim)
  • int x new int5
  • int x 1,2,3,4,5
  • has Properties
  • x.Length
  • utility methods
  • Array.Reverse(x)
  • Array.Sort(x)
  • int , x new int 5, 6

11
Properties
  • Replaces Java getter and setter
  • public class Person
  • private string name
  • public string Name
  • get return name
  • set name value

Person p new Person() p.Name
Bob System.Console.WriteLine(p.Name)
12
Class/Interface Definitions Like Java
  • class modifiers abstract, static, sealed
  • visibility public, protected, internal,
    protected internal, private
  • interface name starts with I like IComparable,
    ISerializable
  • class names do NOT start with C
  • const vs. readonly attributes
  • const, predefined type, compile time evaluation,
    static
  • readonly, value assigned only once (decl or
    construct)

13
Inheritance (C syntax)
  • public class Person IComparable
  • public class Employee Person, ISerializable

14
Constructor
public Person (string name, int ssn)
public Employee(string name, int ssn, int
phone) base(name, ssn) public
Employee(string name) this (name, 999,
999) //static constructor static Employee(
)
15
Overriding Methods
  • Must mark all overridable methods in the
    superclass with virtual
  • public virtual int foo()
  • Must mark all overriding methods in the subclass
    with override
  • public override int foo()
  • Use base.MethodName() to call the superclass
    method

16
Overloading operators
  • Like C, we can overload and redefine operators
    like and .
  • public static bool operator (MyClass lhs,
    MyClass rhs)
  • public static MyClass operator(MyClass lhs,
    MyClass rhs)

17
Parameter passing
  • Default is by value like Java
  • Can change to reference using either ref or out.
  • ref pass by reference calling initializes
  • out pass by reference called initializes
  • double orbit
  • p.ComputeOrbit(planet, fuel, out orbit)
  • public int ComputeOrbit(Planet p, int fuel, out
    double orbit)
  • double orbit 10.756
  • p.ComputeOrbit(planet, fuel, ref orbit)
  • public int ComputeOrbit(Planet p, int fuel, ref
    double orbit)
  • Variable parameters params

18
structs (like C/C)
  • value type (on stack) (calling overhead)
  • cannot subclass from (sealed)
  • can implement interfaces
  • are considered subclass of type Object.
  • cannot initialize attributes in definition
  • must initialize all member attributes on creation
    (or before use).
  • can be more efficient for small classes, but
    beware of boxing/unboxing and paramater passing
    overhead

19
Use structs for simple types that will be used
like built-in types
public struct Location private int x
private int y public Location
(int px, int py) x px
y py
public int X get return x
set x value Location loc new
Location(10, 20)
20
Interfaces
  • no access modifiers (everything is public)
  • no constants, attributes, or implementation of
    methods.
  • is operator, test if a class implements an
    interface
  • if (e is ISerializable) .
  • as operator, test a class and cast it
  • ISerializable doc e as ISerializable
  • if (doc ! null)

21
Interfaces vs. Abstract Classes
  • If you are creating libraries, or classes subject
    to change (adding new methods), then abstract
    classes are preferred over interfaces. (virtual
    empty methods).
  • Classes for a single project, Interfaces can be
    easier to understand.

22
What if interfaces have the same method?
public interface IReadable void
Read() public interface IFile void
Read() public class Foo IReadable, IFile
void IReadable.Read() void
IFile.Read() Foo f f.Read()
//no IReadable r f as IReadable r.Read()
23
Collections
  • Some basic data structures are provided
  • Listltintgt mylist Listltintgt()
  • Dictionaryltint, Employeegt empTable
  • Queueltstringgt priority
  • Stackltchargt tokens
Write a Comment
User Comments (0)
About PowerShow.com