Introduction to C - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Introduction to C

Description:

memory leaks. circular references. CsharpIntro..ppt. 11. other C# features ... in C#, all code and data must live within a class ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 36
Provided by: patpa
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C


1
Introduction to C
2
a survey of C features
  • object-oriented concepts
  • classes and objects
  • type safety
  • garbage collection
  • exception handing
  • Visual Studio .NET 2005
  • multi-language applications
  • file extensions
  • design / run / debug modes
  • converting data
  • namespaces
  • parameter passing
  • foreach
  • looping on collections and arrays

3
object-oriented concepts
4
C has C-like syntax
namespace Hello public class Hello
static void Main(string args)
System.Console.WriteLine ("Hello world in
C") System.Console.ReadLine()
  • it is case sensitive
  • braces are used to group statements
  • statements must be terminated with a semi-colon
  • argument-passing is by value
  • well talk about exactly what this means later
  • but the resemblance is only superficial
  • C is a very different language than C

5
C is object oriented
  • encapsulation
  • no global data
  • all data (fields) and operations (methods)
    are inside a class
  • a form of information hiding
  • leads to looser coupling and increases ease of
    modifying code later
  • dan be defeated with bad coding practices
  • inheritance
  • a way of reusing code by extending (adding to)
    or overriding (modifying) parts of existing
    classes, even when you cant see the code of the
    original class
  • extending (inheritance) is only appropriate for
    very stable classes
  • operators and methods may also be overloaded
    (defineds with multiple signatures and behaviors)
  • Issues and terminology can become rather complex
  • in C, a class may inherit from only one
    superclass
  • polymorphism
  • interfaces
  • not so much code reuse as design reuse

6
classes
  • In C, all data and code must reside within a
    class
  • every type is represented by a class

public class Customer . . .
public class App public static void Main()
. . .
public class Globals . . .
public class Utility . . .
7
what is an object?
  • in C, an object is a programmer-defined data
    structure allocated in the managed heap
  • the managed heap lives within the .NET runtime
  • it is completely separate from the Windows system
    heap that unmanaged code would use
  • objects in the managed heap are automatically
    garbage-collected when no more references to them
    exist

8
what is a class?
  • for object, a class must have been written in C
  • the class describes what data resides in objects
    of this type
  • well talk about what kinds of data can be used
    in a few minutes
  • the class describes any initialization which must
    be done on the data when a new object is created
  • the class may also define operations which can be
    performed on the data held within this type of
    object
  • In older languages, operations might have been
    called functions, subroutines or procedures
  • In the object-oriented world, these are now
    called methods
  • the variables are now called fields

9
C is type-safe
  • all code and data are associated with a type
  • all objects have an associated type
  • programs cannot access objects in inappropriate
    ways
  • type usually means class in this context
  • this avoids a group of errors that could occur in
    C and C
  • invalid casts
  • bad pointer arithmetic
  • malicious code

10
automatic memory management
  • generational garbage collection
  • programmers need not remember to free memory
  • eliminates a group of errors
  • dangling pointers
  • memory leaks
  • circular references

11
other C features
  • consistent error-reporting mechanism
  • show exception handling here
  • compiler detects uninitialized variables
  • C doesnt do this (unless you run lint)source of
    many bugs
  • can detect unused variables
  • this warning can be turned on or off in Visual
    Studio .NET
  • array bounds checking (optional)
  • checked arithmetic

12
VS.NET
13
multi-language applications
  • you can mix languages within the same application
  • but only by using a component
  • example
  • multi-language version of a program

14
working with Visual Studio
  • modes of Visual Studio
  • formerly Design" coding ? now nothing is
    displayed
  • Running" program is actively
    running
  • Debugging" program is paused (in
    debugger)
  • how to know which mode you're in?
  • look at the title bar

15
Visual Studio Files
  • Visual Studio produces lots of files
  • bin folder contains .EXE, program input files
  • obj folder contains temporary files
  • solution (.sln) is main file for working with VS
  • project (.csproj) tracks source files, settings
  • C (.cs) denotes source code files

16
type conversion
  • there are various ways to accomplish conversions

int i 5 double d 3.2 string s
"496" d i i (int) d i
System.Convert.ToInt32(s)
implicit conversion
cast required
conversion required
17
example
  • an example of using types in C
  • declare before you use (compiler enforced)
  • initialize before you use (compiler enforced)

public class App public static void Main()
int width, height width 2
height 4 int area width height
int x int y x 2 ...
declarations
decl initializer
error, x not initialized
18
A customer class
  • a Customer class

/ customer.cs / public class Customer
public string Name // fields public int
ID public Customer(string name, int id) //
constructor this.Name name this.ID
id public override string ToString()
// method return "Customer "
this.Name //class
19
Main method application entry point
  • here's the source code for Main, using the
    Customer class from the previous slide

/ main.cs / public class App public
static void Main() Customer c c
new Customer(Jim Allchin", 94652)
System.Console.WriteLine( c.ToString() )
//class
20
procedural vs. object-oriented
  • in procedural programming, it's about the
    sequence of statements
  • in object-oriented programming, it's about
    operations on objects
  • think in terms of object.field and object.method(
    ), not code( )

/ C code / char s1, s2 s1 "THIS IS A
STRING" strcpy(s2, s1) tolower(s2)
21
namespace
22
why System.Console prefix?
  • in C, all code and data must live within a class
  • Classes are often nested within namespaces to
    help organize their names
  • namespaces help prevent naming collisions

System.Console.WriteLine("Hello World!")
System namespace in FCL
Console class
WriteLine subroutine
23
namespace
  • a namespace N is a set of names qualified by N

namespace Workshop public class Customer
. . . public class
Product . . .
//namespace
Workshop.Customer
Workshop.Product
24
fully-qualified references
  • a fully-qualified reference starts with the
    outermost namespace
  • if you want, you can import a namespace drop
    imported prefix
  • using directive allows you to import a namespace

System.Console.WriteLine("message")
using System . . . Console.WriteLine("message"
)
25
complete example
  • using directive(s) specified at top of file

namespace Workshop public class Customer
. . . public class
Product . . .
/ main.cs / using System using
Workshop public class App public static
void Main() Customer c c new
Customer("jim bag", 94652)
Console.WriteLine( c.ToString() )
26
parameter passing
27
parameter passing
  • C offers three options
  • pass-by-value (default)
  • pass-by-reference
  • pass-by-result ("copy-out")
  • More subtle than you might think

28
parameter passed by value (the default)
  • bits are copied

Stack
public class App public static void Main()
int i 99 Foo(i)
System.Console.WriteLine(i) // i 99
private static void Foo(int value) value
value 1
stack frame for Main
29
parameter with ref keyword
  • reference to a reference is passed
  • ref field must be assigned to by caller
  • callee can change the original thing being
    referenced

public class App public static void Main()
int Vals Vals new int1000
Vals0 99 Foo2(ref Vals)
System.Console.WriteLine(Vals0) // 9
private static void Foo2(ref int A) A
new int2000 // enlarge array A0 9

30
parameter with out keyword
  • no value is passed in
  • called method must assign to parameter

public class App public static void Main()
int a, b ComputeXYZ(out a, out b)
System.Console.WriteLine("Results " a ", "
b) private static void ComputeXYZ(out
int r1, out int r2) r1 ... r2
...
31
statements in C
  • C supports the standard assortment
  • assignment
  • subroutine and function call
  • conditional
  • if, switch
  • iteration
  • for, while, do-while
  • control Flow
  • return, break, continue, goto

32
examples
x obj.foo() if (x gt 0 x lt 10)
count else if (x -1) ... else ...
while (x gt 0) ... x--
for (int k 0 k lt 10 k) ...
33
foreach
  • specialized foreach loop provided for collections
    like array
  • reduces risk of indexing error
  • provides read-only access to collection (but
    objects within collection can be modified)

int data 1, 2, 3, 4, 5 int sum
0 foreach (int x in data) sum x
foreach
type
value
collection
34
concepts covered
  • object-oriented concepts
  • classes and objects
  • type safety
  • garbage collection
  • exception handing
  • Visual Studio .NET 2005
  • multi-language applications
  • file extensions
  • design / run / debug modes
  • converting data
  • namespaces
  • parameter passing
  • foreach
  • looping on collections and arrays

35
the end of this PowerPoint file
Hooray!
Write a Comment
User Comments (0)
About PowerShow.com