Moving from procedural to OO

I have been coding in C for upwards of 4 years now and a friend of mine has brought to my attention that a lot of employers now want their programmers to use OO instead of procedural. I have gotten a book on Java and 2 on C# (one on the basics and one on network programming), but I must say this OO thing isn't really catching with me. I am still in high school, so I don't have much time on my hands, but do any of you have any good resources on how to relate OO to procedural, or to just make it a bit understandable

Thanks


Answer this question

Moving from procedural to OO

  • solutions

    Ok, I still didn't understand any of this until I read the link provided. But it seems to me that something is still amiss or I need more info.

    I believe according to the tutorial that the setter/getter should have the same name as the field it's a property of

    Shouldn't it have the same name but with a starting capital letter

    Or is that not always necessary

    Thanks again.


  • Pati123

    I am trying to put this together and run it to see how it works while debugging lines.

    I'll let ya know what questions I have. =)


  • hashmi

    Thanks for the feedback, much appreciated.

    OK well the car will have a radio object so we add properties to it ....

    private CarRadio theRadio = null;

    //public property, get and set car radio (since we can change the model for example

    public CarRadio TheCarRadio

    {

    get { return this.theRadio; }

    set { this.theRadio = value; }

    }

    //constructor

    public Car()

    {

    this.theRadio = new CarRadio();

    }

    a radio class may look like.....

    public class CarRadio

    {

    private int volumeLevel = 0;

    private bool cdInserted = false;

    public int TheVolume

    {

    get { return this.volumeLevel; }

    }

    public bool IsCDInserted

    {

    get { return this.cdInserted; }

    }

    //default constructor:

    public CarRadio()

    {

    }

    public bool DoInsertCD()

    {

    if (this.cdInserted)

    { return false; }

    else { this.cdInserted = true;

    return true; }

    }

    public void DoIncreaseVolume()

    {

    if (this.theVolume < 100) {

    this.volumeLevel ++; }

    }

    }

    small example. So a car radio has a volume control which you can get and set volume and a CD indicator property to see if a CD has been inserted and a method to insert a CD. if the CD insertion method is called, checks to see if the CD has been inserted first, if so, it will return false in failure as there is a CD already in the player so you need to take it out before inserting it. You could argue that you can take out the CD first then insert another but for the sake of simplicity its done this way :-)

    so....

    Car theCar = new Car();

    MessageBox.Show("Current volume: " + theCar.CarRadio.TheVolume.ToString());

    theCar.CarRadio.DoIncreaseVolume();

    MessageBox.Show("Current volume: " + theCar.CarRadio.TheVolume.ToString());

    should now return 1 since we increased the volume.

    does this help



  • rsnera

    hi,

    this page has few lessons which will help you to understand OO better personaly i started with it. i guess you will better understand it quicker than i did , because you have background about coding  http://programmersheaven.com/2/Les_CSharp_0

    the oops generaly depend on classes

    when you need a new type (car , bank account... etc as what ahmedilyas said )you create a class

    when you need to add functionality to this type you add methods /functions,

    when you want to allow your type to be variant from each other you add properties and fields(like red car , yellow car all of the same type but variant in color) or when you want to change its case

    the word that i like most about OO is that " type what you want to do, create a class for each name you has, and a method for every verb you has" for example "i need a car has 4 doors which can open and close"

    class car

    {

    private door[] doors= new door[4];

    public void OpenDoor(int DoorNumber)

    {

    doors[DoorNumber].IsOpen = true;

    }

    public void CloseDoor(int DoorNumber)

    {

    doors[DoorNumber].IsOpen = false;

    }

    }

    class door

    {

    public bool IsOpen = false;

    }

    there are many other concepts you will find them in the link

    hope this helps



  • Kendal Walton

    Yes! Thank you, that makes it a lot easier to understand.

  • Tbom7

    its not always necessary but make it meaning and short and sweet. like if we have a private member (variable) called "name" then the property should be "TheName" or "Name".

    The choice is yours but the rule of thumb is to make it meaningful but short and useful



  • lee d

    its alot to cover and there are many quick tuts on the net which you can find. Here is just a quick rundown.

    OO (Object Orientated) is a design/approached used for us really to understand better on how objects are created, what type of objects there will be, what an object can do and represent.

    Take a car for example. Its an object. A car has many properties, such as the number of doors, number of wheels, number of windows etc..

    it can have many methods (methods/functions that do a task) such as opening a door, closing a door, locking the car, switching on and off the engine, opening the boot, drive, reverse, park etc....

    so we create a class called Car. This will be our "Car object"

     

    public class Car

    {

       //private properties for the car, so we know what is in a car

       public const int numberOfWheels = 4;

       public int numberOfDoors = 4;

       public bool isEngineOn = false;

     

       public Car()

       {

       }

    }

     

    a class generally has a constructor, in fact it can have more than 1 constructor but for the sake of simplicity, this will have one. This is so we can create a car object and access its properties/methods etc...

     

    Car theCarObject = new Car();

    MessageBox.Show("This car has: " + theCarObject.numberOfDoors.ToString() + " doors");

     

    this will tell us that the car object in question, which we created, has 4 number of doors, since this is the value we set as default in the car object.

     

    Lets take another example.

     

    Bank

    BankAccounts

     

    a bank has many bank accounts. Each bankaccount will have:

  • name of account holder

  • account number

  • pin number

     

    these are say, the essential items/properties of a bank account right

     

    public class BankAccount

    {

       public string accountHolder = String.Empty;

       private int accountNumber = -1;

       private int pinNumber = -1;

     

       public int TheAccountNumber

       {

          get { return this.accountNumber; }

       }

       public int ThePinNumber

       {

          get { return this.pinNumber; }

       }

     

       public BankAccount(string accountHolderName, int pinChosen)

       {

          this.accountHolder = accountHolderName;

          this.pinNumber = pinChosen;

          this.accountNumber = this.DoGenAccountNumber();

       }

    }

     

     

    ok simple example this. When we create a bank account, we usually give things like the account holder name and pin number right Well, this is what the constructor does. It will set for us, in this account instance/object, the account holder name and pin number you chose. This then is our object we can use to do whatever we want, like withdraw money from our account or change the pin number or whatever.

     

    BankAccount myAccount = new BankAccount("Bob", 1234);

    MessageBox.Show(myAccount.accountHolder);

     

    this will show us "Bob" since we create a bank account and its name/account holder name is Bob. We can then use this object to say, delete the account or add money to the account or whatever we like to do.

    OO basically takes real life objects and puts them into formats we can understand for us to develop for example

     

    does this help/make some sense



  • JeremyCW

    lol

    i found your answer as soon as i posted , Good Typing speed

    you are welcome



  • yazad_gandhi

    glad I could help! Sorry, I Was typing more (edited) so you can read more of it now if you like

  • Super_user

    private CarRadio theRadio = null;

    Ok, this creates a new object, theRadio and sets it's value to null, which is kinda like empty, but not empty.

    This is done in Main. It can't be done anywhere else could it It has to be in a method right, so for this exercise; Main.

    //public property, get and set car radio (since we can change the model for example

    public CarRadio TheCarRadio

    {

    get { return this.theRadio; }

    set { this.theRadio = value; }

    }

    Now this part, I don't think I've ever seen used this way. It looks like a constructor but at the same time looks like a method to me. Could you break down what public CarRadio TheCarRadio is doing I usually see constructors like CarRadio(int twoKnobs, int fourBttns)

    and methods usuall y look like void CarRadio(int numbBttns)
    { int b = numbBttns; }


    The rest of the contructors that follow, I think I get. But until I understand the above, I won't be able to get this to work.

  • Ciprian

    good find Shaka - programmersheaven.com - that site has been online for many many years, wow. :-)

  • Newbie Kam

    nice response Shaka! :-)

  • Jiajia

    hi,

    this is not constractor this is called property, the property is used as a special form (shortcut) for 2 methods , in other language if you want to change the value of a class member you have to write 2 methods getter and setter for example

    class car

    {

    //class private member

    private Color _carColor;

    public Color ColorGetter()

    {

    return _carColor;

    }

    public void ColorSetter(Color value)

    {

    _carColor = value;

    }

    }

    because one of OO requirements is data encapsulation and those 2 methods will be written alot so in .net they provided the Property as short cut for those 2 methods and make value as keyword

    class Car{

    private Color _carColor;

    Public Color CarColor

    {

    get{return _carColor; }

    set{_carColor = value}

    }

    }

    encapsulation is a great thing in OO for many reasons, for example you want to change the car color but the car color could have other dependencies like seat colors , mirror color ... etc so when you change the car color your code is aware to change the other dependencies as well or to make a mathimatical operation of the data which entered to this method .... etc

    hope this helps



  • Kannan.B

    This is a great example of creating an object and it's properties and methods. Thank you for taking the time to post.

    I have a question about these objects. Like for the car class that has a number of doors. What about if the car class had other objects inside of it. Lets say radio objects that have different properties such as the stations that they play.

    You have a car object and the car object has a radio, the radios have different stations like am or fm or satellite or cd player or tape player.

    How would that look


  • Moving from procedural to OO