Wednesday, April 30, 2014

Introduction_To_Class_Type


namespace Introduction_To_Class_Type
{

    /*
        * Introduction to object-oriented programming
        *********************************************
        *The class Type
        The class type allows you to define the characteristics or
        fields of an object.
        It also allows you to encapsulate all the data (fields) of
        an object within a single entity.
        */
    //The class Student defines the characteristics of a student: 
    //that is: firstname, lastname and email.
    //The class Student is just a blueprint or template that defines
    //what a student is. (You can add as many fields as needed by 
    //an application.
    //===========================================
    public partial class Form1 : Form
    {
       //Show how to:
        //  Create an object
        //  Initialize its fields
        //  Display the object

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateDisplayStudentObject_Click(object sender, EventArgs e)
        {
            //Create a Student object
            //Use the keyword new to create an object
            //(same way you've done with arrays)
            Student stu1 = new Student();
            //Student is a class type, therefore it is
            //a data type (shown in light blue)
            //the keyword new tells the compiler to 
            //create an object on the HEAP

            ///initialize its fields
            ///Access the fields of the object stu1.
            ///Use the (.) to access the fields
            stu1.firstname = "John";
            stu1.lastname = "Willams";
            stu1.email = "jWilliams.rtc.edu";

            //display
            richTextBox1.Text =
                "first name : " + stu1.firstname + "\n" +
                "last name : " + stu1.lastname + "\n" +
                "email : " + stu1.email;
        }

        private void btnDog_Click(object sender, EventArgs e)
        {
            //Create/Initialize Dog object, using a constructor
            Dog d1 = new Dog("killer", "Husky", "Male", 150, 3);
            Dog d2 = new Dog("Julie", "Beagle", "Female", 100, 3);
            //create another Dog
            //Display
            richTextBox1.Text = "name: " + d1.Name + "\n" + "breed: " + d1.Breed + "\n"+
                                "gender: " + d1.Gender+"\n" + "weight: "+d1.Weight +"\n"+
                                "age: " + d1.Age + "\n";
            //Display use AppendText
           richTextBox1.AppendText("\n****************\n" +
               "Name: " + d2.Name + "\nBreed: " + d2.Breed + "\nGender: " + d2.Gender + "\nWeight: " + d2.Weight + "\nAge: " + d2.Age);
        }

        private void btnCar_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            Car c1 = new Car("Toyota", "Camry", 0, 25000);
            Car c2 = new Car("Honda", "Accord", 90000, 5000);
            Car c3 = new Car("Honda", "Civic", 0, 18000);

            richTextBox1.AppendText("Make: " + c1.Make + "\nModel" + c1.Model + "\nMileage: " + c1.Mileage + "\nPrice: " + c1.Price);
            richTextBox1.AppendText("\n*********\nMake: " + c2.Make + "\nModel" + c2.Model + "\nMileage: " + c2.Mileage + "\nPrice: " + c2.Price);
            richTextBox1.AppendText("\n*********\nMake: " + c3.Make + "\nModel" + c3.Model + "\nMileage: " + c3.Mileage + "\nPrice: " + c3.Price);
        }
    }

    public class Student
    {
        //class fields:
        public string firstname;
        public string lastname;
        public string email;
    }
    //------------------------------
    public class Dog
    {
        //private fields: are not accessible outside
        //of the class.
        private string _name;
        private string _breed;
        private string _gender;
        private int _weight;
        private int _age;

        //define a constructor to initialize the 
        //private fields of a class
        ///Constructor is a special method.
        ///Syntax:Mostly public, has NO return type,
        ///       Must have same name as the class.
        ///       Defines as many parameters as there
        ///       fields to initialize
        public Dog(string name, string breed, string gender, int weight, int age)
        {
            //initialize the private fieds with these parameters
            _name = name;
            _breed = breed;
            _gender = gender;
            _weight = weight;
            _age = age;
        }

        ///properties
        ///Are aliases to private fields.
        ///There purpose is to provide access to private fields
        
        ///You should have a property for every private field
        ///that you want to provide access to.
        ///
        ///A property must be public, 
        ///must return the same type as the field
        ///Its name should sound the same as that of the field
        ///should capitalize the first letter.
        ///property behaves like a variable
        ///
        //property for the _name field
        public string Name
        {
            get
            {
                return _name;
            }
        }
        public string Breed
        { get { return _breed; } }

        public string Gender
        { get { return _gender; } }

        public int Weight
        { get { return _weight; } }

        public int Age
        { get { return _age; } }
     
    }
    public class Car
    {
        //private fields:
        private string _make;
        private string _model;
        private int _mileage;
        private int _price;

        //Constructor to initialize the private fields of class
        public Car(string make, string model, int mileage, int price)
        {
            _make = make;
            _model = model;
            _mileage = mileage;
            _price = price;
        }

        //property for the _name field
        public string Make
        {
            get
            {
                return _make;
            }
        }
        public string Model
        { get { return _model; } }

        public int Mileage
        { get { return _mileage; } }

        public int Price
        { get { return _price; } }
    }
}
///USE THE CAR CLASS.
///DEFINE ITS FIELDS PRIVATE
///ADD CONSTRUCTOR
///ADD PROPERTIES
///IN FORM1: CREATE 3 CAR OBJECTS,THEN DISPLALY THEM.

No comments:

Post a Comment