C# language: 6. Classes

in #programming7 years ago

CC.png

Recently we have learnt the basics, it’s time for real object programming.

Object-oriented programming involves creating classes and then instances of these classes or objects (hence the name, Object-oriented programming), what’s the point? I am already saying …
Suppose that we want to make a cake, the cake will be the object, but we do not make the dough without a recipe, this recipe is the class, is information what ingredients to add to the cake, how much time to bake them, in the same class we create only what it has to be in a later created object.

Let’s do an example, but before we will do it, we need to add our class, so we click on the tab on the right Solution Explorer, right click on the name of our project and choose the option at the bottom of the Class

klasa.png

And we call it cake because we want to create a cake recipe 🙂

ciasto.png

And what will we write in this class? Suppose we enter there AmountOfSalt variable, I do not know what this cake will be, I don’t know nothing about doing cake, but it does not matter, enter there variables so that it looks more or less like a recipe for cake, what variables we will enter there? Maybe for example BakingTime, NumberOfEggs, HowMuchMilk and HowMuchFlour I think it’s enough, the whole thing looks like this:

class Cake
{
    public double AmountOfSalt;
    public int BakingTime;
    public int NumberOfEggs;
    public int HowMuchMilk;
    public int HowMuchFlour;
}

What with the word public? This is the so-called access modifier, I will explain it in more detail, let’s just make our cake and write out the ingredients for its preparation on the screen 🙂

We have to write something like this in the main function:

static void Main(string[] args)
{
    Cake cake = new Cake();
    Console.WriteLine("To produce this cake it's necessery to pour in " + cake.AmountOfSalt
     +" g salt, to pour in "+ cake.HowMuchMilk +"l milk add "+ cake.NumberOfEggs+"eggs, to pour in "
    +cake.HowMuchFlour+"kg flour and put in the oven on " + cake.BakingTime+"min");
 
    Console.ReadKey();
}

This is how the objects are created, first we write the name of the class on the basis of which we want to create an object, the cake object we called the cake and created a cake object using the line new Cake(), we made the cake, applause for us! Below I also wrote how to get to these variables in the class, just write the name of the class and then the name of the variable.

I still have had to explain what is going on with these access modifiers, there are three main modifiers:

public – as the name suggests public access, that is, we will be able to read any data from this class from anywhere

private – if we give variables, this modifier will pop up an error because we can use the variable with the private specifier only within the class.

protected – works on almost the same principle as private, with the difference that we will be able to use variables with this modifier in inheriting classes (about inheritance later in the next lessons).

There are also such modifiers as eg internal, sealed or protected internal, but we will can only talk about them later, but you will usually only use public, protected and private

This content also you can find on my blog http://devman.pl/csharplan/c-language-6-classes/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

Well, I think that you already know what are the classes and objects, in the next lesson we will tell ourselves a little about the fields and properties, see you!

Sort:  

Hey @slawas, great post! I enjoyed your content. Keep up the good work! It's always nice to see good content here on Steemit! Cheers :)

thanks @exxodus ! I'm trying make good posts, thanks for appreciate :)