Introduction to Classes and Objects Mix

A ten minute or so introduction to creating classes in C#. A few review questions at the end to see if you were paying attention.

ObjectsclassesC SharpProgramming
1.0x

Introduction to Classes and Objects Mix

Created 2 years ago

Duration 0:10:19
lesson view count 28
A ten minute or so introduction to creating classes in C#. A few review questions at the end to see if you were paying attention.
Select the file type you wish to download
Slide Content
  1. Introduction to Classes & Objects

    Slide 1 - Introduction to Classes & Objects

    • Creating Classes Using C#
  2. Objects vs Classes

    Slide 2 - Objects vs Classes

    • Classes are abstract definitions of what an object can do and values it can hold
    • Classes are a mix of code and data
    • An object is an instance of a class
    • Classes are general and Objects are specific examples
  3. Data Hiding

    Slide 3 - Data Hiding

    • Class data is private
    • Protected from unsafe manipulation
    • Modified only by class specific methods
    • Methods may be public or private
    • Private methods for internal class use only
    • Public methods are defined interface to the class’ data
  4. Declaring the data

    Slide 4 - Declaring the data

    • class Die
    • {
    • private int _sides;
    • private int _value;
    • Variables are declared private
    • Variable names start with underscore (_) to highlight that they are private (optional)
    • Variables declared as private
  5. Constructors

    Slide 5 - Constructors

    • Constructors are methods in a class that set the initial state of an object
    • Constructors:
    • Are public
    • Have the same name as the class
    • Do not have a specified type
    • Initialize private values in a class
  6. Sample Constructors

    Slide 6 - Sample Constructors

    • public Die()
    • {
    • _sides = 6;
    • ChangeValue();
    • }
    • public Die(int sides)
    • {
    • _sides = sides;
    • ChangeValue();
    • }
    • Default constructor
    • No parameter
    • Non default constructor
    • Parameter(s)
    • Default value for number of sides
    • Number of sides set to value passed to the constructor
  7. Accessing the Data

    Slide 7 - Accessing the Data

    • Class data should be accessed using methods or properties
    • Changes to data should be validated carefully inside the class if allowed at all
    • Some data should not be allowed to be changed after the constructor
  8. Changing Data - Method

    Slide 8 - Changing Data - Method

    • private void ChangeValue()
    • {
    • _value = r.Next(0, _sides) + 1;
    • }
    • Declared private
    • Used only in the class
    • Code to set value is “hidden”
  9. returning data - Method

    Slide 9 - returning data - Method

    • public int Roll()
    • {
    • ChangeValue();
    • return _value;
    • }
    • _value changed in a private method
    • _value returned by method not by direct access
  10. Getting/Setting data - property

    Slide 10 - Getting/Setting data - property

    • public int Value
    • {
    • get { return _value; }
    • set {
    • if (value > 0 && value < _sides+1)
    • _value = value; }
    • }
    • _value returned by method not by direct access
    • _value set in a protected fashion
  11. Declaring an Object

    Slide 11 - Declaring an Object

    • Die dSix = new Die();
    • Die d12 = new Die(12);
    • Default constructor
    • Six sides are default
    • Non default specifies the number of sides
  12. Review

    Slide 12 - Review

    • An instance of a class is an object
    • One or more constructors to set initial values
    • Constructors have the same name as the class
    • Access to data is limited and protected