Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
can someone explain to me what classes are for and what is the use of them in the code and why is it important to have classes, i mean any code can work good without classes right? maybe an example would be good to me any example that you find will be good for me to learn because i have reading and searching for week now untill i found this page that i get so much help from
Posted
Updated 1-Jun-13 9:04am
v2
Comments
AspDotNetDev 1-Jun-13 14:41pm    
The title of your question is way too long, and you haven't explained very clearly at all what difficulties you are having. The link I pointed you to earlier (in a different question) showed exactly what classes are and how to use them in code. If you want any sort of valuable help, you are going to have to try harder to ask clearer questions.
AspDotNetDev 1-Jun-13 14:41pm    
Also, since you may be new to the site, keep in mind that you can edit your question to modify it. I recommend you start by editing your title, and then you also edit the body of your question to be more clear.
walanw 1-Jun-13 15:05pm    
what do you think now? is it good ?

So, basically, you are wondering what the value of classes are when you can accomplish pretty much all programming without classes?

Let me tell you a story. A long time ago in a galaxy not unlike our own, there were programming languages much less developed than C#. One of these languages was called assembly language. This assembly language was as close to the processor as you could get without writing in machine code, which is just a bunch of 0's and 1's. Assembly language allows you to do anything you want with a computer, as it gives you access to the full instruction set that a processor can compute.

However, there is one problem with assembly language. It's so simple, that many common tasks are very hard to accomplish, and there is a lot of unnecessary repeat typing. For example, if you want to create a function, you have to initiate the instruction to jump to it, you have to save register values to the stack, and when the function call is over, you have to jump back to where it was called from, and undo all the steps you did when you did the initial jump.

Modern high level programming languages make these common tasks much easier, as they have shortcuts built in. For example, in C#, that complicated assembly language task of calling a function is done like this:
C#
namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int y = Add(5, 6);
        }

        static int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}

Now, I only wanted to demonstrate the Add function and the call to it, but I also include the namespace and class and main function for completeness sake. Here are the only important parts that I was talking about:
C#
int y = Add(5, 6);
static int Add(int x1, int x2)
{
  return x1 + x2;
}

Very simple. Having a function like this and being able to easily call that function in many locations means that you have increased the reusability of that code. That is a very good thing, as reusability means you have to type less, and so it lets you program faster. The same types of principles apply with classes. As you saw above, I had a class called Program with two functions in it. However, putting all of your functions into a single class can get out of hand. I have worked in code bases with millions of lines of code. By splitting your code into different functions, classes, and namespaces, you can make better sense of the code with something called encapsulation. Encapsulation basically means writing classes and functions that do a bunch of work, but that the caller of the function/class doesn't have to know about. To give you an example, I have created a new class and moved our Add function into it:
C#
namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Maths m = new Maths();
            int y = m.Add(5, 6);
            int z = m.Add(8, 9);
        }
    }
    class Maths
    {
        public int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}

If we wanted, we could move the Maths class into a different file. Right now, all of the above code is in the Program.cs file, but we could create a new file called Maths.cs and put the Maths class in it. This is what that file would contain:
C#
namespace MyConsoleApplication
{
    class Maths
    {
        public int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}

If we look at our Program.cs file again, we see that it is very simple:
C#
namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Maths m = new Maths();
            int y = m.Add(5, 6);
            int z = m.Add(8, 9);
        }
    }
}

Write a few million lines of code with a few hundred coworkers, and the value of classes becomes much more clear. They allow you to encapsulate functionality and data to simplify the code base and allow for greater reusability.

I am not going to teach a full Computer Science course here, but to give you a taste of why else classes are useful, they can be used to: store data, create structure for serialization, allow for inheritance and interface implementation, and much more. You will come across all of this over time. For now, just trust that classes do have their uses and you will definitely make use of them greatly over your career.
 
Share this answer
 
Comments
walanw 1-Jun-13 15:54pm    
look i will not exaggerate if i say this is the best lesson i have ever had, this moment i learned much more than i have learned on high school in 2 years, thank a lot for your help thats exactly the answer i needed for my question, maybe you should take this and make a video about it on youtube there is many people out there need this, my teacher is not that good that why the most of my class mates failed, maybe i didnt understand everything but i will read this again and again untill i stand it, maybe its not a big deal for you but for me as a beginner it is thank alot again i hope i can find you if i need help again this website is awesome!!
I'd like to add my own spin on this as I don't think the solution really shows you what the value of a class is...

First, the background. Yes, in days of assembly it was difficult to program, just as Aspdotnetdev said, however as higher level languages emerged such as Fortran, Cobol, etc, and software got more complex there was a need for object oriented programming.

Back in the early years of programming, languages were simply statement lists that ran through from top to bottom. Some languages had function calls or subroutines that could break up complicated code dumps into more readable versions, but there wasn't anything object oriented. Programmers needed to take real world systems and turn them into programs and it was difficult to maintain much less add functionality to non-object oriented programming (OOP).

Lets take for example that you wanted to program a Car, in linear programming you would have a loop and a bunch of functions, if statements, switches, etc to handle all the features of the car. OOP introduces a new concept of thinking when programming, if you wanted to do a car, you might have something like this:

C#
public class Car
{
    public void StartEngine() { }
    public void StopEngine() { }
    public void Accelerate() { }
    public void Decelerate() { }
    public void OpenDriversDoor() { }
    public void OpenPassengerDoor() { }
}


You implement the routines and your software runs great. A year down the line your boss comes to you and says "we need to add a new kind of car, an SUV that has 4-wheel drive, but we can't break exising code". Easy you say, you do something like this:

C#
public class Car
{
    public void StartEngine() { }
    public void StopEngine() { }
    public void Accelerate() { }
    public void Decelerate() { }
    public void OpenDriversDoor() { }
    public void OpenPassengerDoor() { }
}

public class SUV : Car
{
    public void Enable4x4() { }
    public void Disable4x4() { }
}


Now SUV has all the functionality of Car (because it derives from it), and it has 4x4. You've added a new set of features to your software without the need to modify existing code, this reduces bugs, code churn, errors, etc.

The example above can be extended, lets say you wanted to create a flying car, you'd add another class, derive from Car, and implement the flying routines.

The way of thinking about these programs changed with OOP principles, programs were no longer linear approximations of something, but broken down into the individual systems and wired together so that they more mimic how they would be implemented in the real world. If you extend this out, you can find principles like Single Responsibility, Dependency Injection, etc that can really make coding very powerful, easy to maintain, and easy to extend.

Here are some references for you:

https://en.wikipedia.org/wiki/Object-oriented_programming[^]

https://en.wikipedia.org/wiki/Solid_(object-oriented_design)[^]

https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms[^]
 
Share this answer
 
Comments
walanw 2-Jun-13 4:14am    
thanks alot great example am starting now to get the idea and the value of classes.
Some people have given good, in depth answers but, seriously, buy a book or read some articles for high level 'I have no clue what I am doing' questions.
 
Share this answer
 
Hello there,
in my opinion, classes are more like instruments that can be used to identify some sort of data or be used as code library or even can be used like code management tool.

take a look at this link: http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx[^]

try to follow the examples in the link.
Good Luck,
z3ngew
 
Share this answer
 
v2
Comments
walanw 1-Jun-13 15:27pm    
everyone recomnd this site but i dont find it helpfull, have another one?
z3ngew 1-Jun-13 15:30pm    
maybe you can try search on youtube for video tutorials.
good luck,
z3ngew

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900