C# Tutorial for Beginner : Chapter 5 Class and Instance

INTRODUCTION

 A class resembles a blueprint of particular object. In reality, each object has some shading, shape and functionalities. For instance, the extravagance car Ferrari. Ferrari is an object of the extravagance car compose. The extravagance car is a class that indicate certain trademark like speed, shading, shape, inside and so forth. So any organization that makes a car that meet those necessities is an object of the extravagance car write. For instance, each and every car of BMW, Honda, Porsch are an object of the class called 'Extravagance Car'. Here, 'Extravagance Car' is a class and each and every physical car is an object of the extravagance car class.
Image 1. Component of class

Class consists of class variables, constructors, and methods. Back again to the example of the car. Every car must have a name like BMW or Honda. In class, this car's name is a variable of the class. The car performs a function that has been designed from its factory such as driving, stopping, slowing, and accelerating. Method is a function that belongs to the class, in this case the car function categorized in the class method. A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor has exactly the same name as that of class and it does not have any return type. You can make variable inside method and constructor but that variable can only be access or used in that method itself. As for variable created in class which called global variable can be used and called in many places whether method or constructor.

CODE

to understand the use of the class and its instance you pay attention to the code example below.

using System;
using System.Collections.Generic;
using System.IO;
class Person {
    public int age;     
  public Person(int initialAge) {
        // Add some more code to run some checks on initialAge
        age = initialAge;
        if (age<0)
        {
            Console.WriteLine("Age is not valid, setting age to 0.");
        }
     }
     public void amIOld() {
        // Do some computations in here and print out the correct statement to the console 
         string msg="You are ", young="young.", teen="a teenager.", old="old.";
         if(age < 13)
         {
             msg+=young;
         }
         else if (age>=13 && age<18)
         {
             msg+=teen;
         }
         else
         {
             msg+=old;
         }
         Console.WriteLine(msg);
     }

     public void yearPasses() {
        // Increment the age of the person in here
         age++;
     }
     static void Main(String[] args) {
        int T=int.Parse(Console.In.ReadLine());
        for (int i = 0; i < T; i++) {
            int age=int.Parse(Console.In.ReadLine());
            Person p=new Person(age);
             p.amIOld();
                for (int j = 0; j < 3; j++) {
                  p.yearPasses();
                }
                p.amIOld();
                Console.WriteLine();
        }
    }
}


After looking at the code above let's analyze the code through the flowchart below :


  • A class was created with the name "Person". As the name implies this class is related to humans and is closely related to age.  
  • In this class, there are 1 constructor, 3 method, and 1 variable. Global variables in this class are named "age".  
  • The function of the "age" variable is to store the age of the object instance to be created.  
  • The "amIOld ()" method is used to check whether the "age" variable matches the rule set in this method. 
  • The "yearPasses ()" method only works to add the "age" variable by 1. 
  • The "Main" method is a must-have method for a program. The "main" method starts the first time the program is executed.
  • starting from the main method, the variable T holds input of the console and converts it into an integer. This variable works for how many proicess that we want. 
  • With loop "for" then starts the process one by one up until value of variable "T"
  • Declare and initialize integer variable "age" with your input in console. Remember this "age" variable is not a global variable. This variable can only be called or used inside Main method. If you want to used or call "age" global variable you can you use this command "p.age". 
  • Then make instance of Person class by "new Person(age)" command. Every time we make new instance we will call constructor immediately. If you don't want to change default constructor then you don't have to write constructor. But sometimes we need to custom constructor due to different cases or circumstances
  • Inside constructor, I make conditional statement if age less that 0 then it will give warning. 
  • "p.amIOld()" is called. This method called "age" global variable to do selection if "age" is less than 13 then display young; if "age" is greater than or equal to 13 and less than 18 then display teen; and if "age" is greater than or equal to 20 then display old.
  • Entering inner looping, call "Person.yearPasses()" three times. 
  • Then call "Person.amIOld()" again.

RESULT

After comprehend the example code, it is time to run program above and see the result : 


Comments

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL