C + + vs C # Learning: class initialization

Source: Internet
Author: User

Class and Plato's world of Ideas

We know that there are classes everywhere in object-oriented programming, but the class is just a conceptual thing, not an entity, not a memory, and you can't use it until you instantiate it. Only if you instantiate the class as an object, it is a real entity. Occupies memory, Can be used by us. Like Plato's idea of the world, Plato believes that there are two worlds, one is the real world we live in, and the opposite is the concept of the world, what is the idea of the world? It's an immutable world, made up of piles of ideas, like people, horses, birds .... The idea of the person is only a concept, not any actual person. And our real world is based on the idea of the world simulation out of the actual one of the people, a horse. Some people may imitate the more beautiful, so there are handsome beautiful, some people is a failure, so what dinosaur frog is also a large number. As for the simulated action who did it? Plato did not say. Some people think that God is doing this work.

So when we instantiate a class it's kind of like God's work, taking an empty idea from the idea world, and then instantiating the object into a single constructor.

C # class initialization

When we instantiate a class, we typically use a new keyword such as MyClass Arwen = new MyClass (). This is pretty similar to C + +. C + + This can instantiate a class to represent a piece of memory in the heap to hold an object. When you're done, release the memory yourself. But it can also be directly MyClass Weiwen; Indicates that the object is saved in the stack, but the allocation and retraction of the stack is controlled by the system. C # can only use new to open memory in the heap, and the memory in the heap is managed by the CRL, which is used to release it to you, so the heap here is called the managed heap.

when instantiating a class we simply used a new one, but actually did a lot of other things in the background. What exactly did you do? In order to do the following things

1. Initializing static variables in a class

2. Calling a static constructor in a class

3. Initializing class non-static variables

4. Initializing a static variable in the parent class

5. Calling the parent class static constructor

6. Initializing a non-static variable of the parent class

7. Calling the parent class constructor

8. Call your own constructor

Of course, if you don't inherit a parent class, you don't have to take care of the parent class. If the parent inherits a parent class, the parent class inherits the above action. Take a simple example to see. If there is a parent class father, son son

Class Father

{

public static int age; 4. Assignment value is 0

public string name; 6. Assignment is null

Public Father ()//7. Call this constructor

{

Console.WriteLine ("I am a lazy Father Construtor,don ' t do anything.");

}

Public Father (String myName)

{

Console.WriteLine ("I am a diligent father constructor.");

name = MyName;

}

Static Father ()//5. Assignment Age is 110

{

age = 110;

Console.WriteLine ("Father static construtor would never do anyting after setting age with a value 110.");

}

}

Class Son:father

{

public static int age; 1. Initialize age to 0

public string name; 3. Initialize to NULL

Public Son ()

{

Console.WriteLine ("I am a lazy son construtor,don ' t do anything.");

}

Public Son (String myName)//8. Call this constructor

{

Console.WriteLine ("I am a diligent son construtor.");

name = MyName;

}

Static Son ()//2. Assignment Age is 11

{

Age = 11;

Console.WriteLine ("Son static construtor would never do anyting after setting age with a value 11.");

}

}

We instantiate the class son.

Son Arwen = new Son ("Arwen");

The result of the output is

Son static construtor would never do anyting after setting age with a value 11//class static constructor

Father static construtor would never do anyting after setting age with a value 110. Parent class Static Constructors

I am a lazy Father Construtor,don ' t do anything. Parent class Constructors

I am a diligent son construtor//class constructor

The order of execution is based on those 8 steps. But there are some places to be aware of.

1.) static constructs can be defined only as static plus class names, no arguments, no public modifiers, and so on. Only static variables can be assigned in the inside, and no value can be assigned to the General field, and the static constructor is only called once when the class is first instantiated. It is not called until the class is instantiated in the default format. , the call cannot be displayed. In addition to being called when the class is first instantiated, it is also called before a static variable in the class is first used. For example, instead of instantiating the class son, you call directly

Console.WriteLine (Son.age); Prints the result as

Son static construtor would never do anyting after setting age with a value 11

11

In addition, the Const type field is static by default. But if you call the Const field, the static constructor will still not execute.

2.) If there are many constructors in a class, no matter which one you use to instantiate the class, calling the constructor of the parent class will only invoke the one with no arguments. So if you don't have a parameterless constructor, and you have a constructor with parameters, And you are the parent of a class. The compilation will be error-free. If you don't have any constructors, you'll be fine. Because if you do not have any constructors, the system will generate one for you by default. But if you write any of the constructors with or without parameters, the system will not be generated for you by default.

3.) If you want to call the parent class, the parameter constructor must be specified. Use the keyword base. For example, when invoking a parameter constructor in son, you also call the parent class's parameter constructor as follows

Public Son (String myName): Base (MyName)

{

Console.WriteLine ("I am a diligent son construtor.");

name = MyName;

}

In this case, the parameterless constructor of the parent class is not called. And there is a call to the one that has the argument. But even though you're not calling the parameterless constructor, you can't save it. The parameterless constructor must be written to put that.

4.) We know that if a subclass has a function with the same name as the parent class, the function of the parent class is hidden. In fact, a field with the same name will be hidden as well. There are warnings at compile time and will remind you to add a new keyword.

New public static int age;

New public string name;

Change it to the top so there is no warning. But it's weird to look at it. In fact, the same is true without the new effect. The default is to add new, but explicitly add new to the good point.

C + + class initialization

There is no static constructor in C + +. Calls to constructors are in the same order and in the same way as C #. Only a little bit of writing is required to call the constructor of the parent class. Replace base in C # with the class name of the parent class.

In addition, the static variable members of C + + are not the same as C #. The class is not initialized by default. You must initialize yourself outside of the class. member variables in C + + can be assigned at the same time as the const static type, but not the others.

Class Father

{

Public

static int age;

String name;

Father ()

{

cout<< "I am a lazy Father Constructor,don ' t do anything." <<endl;

}

Father (String myName)

{

cout<< "I am a diligent father constructor." <<endl;

name = MyName;

}

};

Class Son:p ublic Father

{

Public

static int age;

String name;

Son ()

{

cout<< "I am a lazy son constructor,don ' t do anything." <<endl;

}

Son (String myName): Father (MyName)

{

cout<< "I am a diligent son constructor." <<endl;

name = MyName;

}

};

int son::age = 110; The static variable can only be initialized outside of the class. You have to add an int in front, but you don't have to add static, and you can't add static.

You can son:age this in other places, and you can change its value directly. For example son::age = 911;

C # vs. C + + initialization

As we can see from the above, the usage of the two constructors is basically the same. except that C + + does not have a static constructor. Another call to the parent class with a parameter constructor is to use the parent class name directly without the base keyword.

The main difference between the two is in initializing the members of the class.

1.) C + + initialization is only responsible for how much memory is allocated based on the type in the class. No initialization assignment is made to the member variable. and the memory of the static variable is not in the class. The static variable is saved in the memory area. And when the class is instantiated, the static variable is not actually allocated memory. Only you allocate memory when you initialize outside of the class.

2.) In addition, if you define a normal public member variable in the class, you will find a value when you do not assign a value to print with cout. But that value is not sure, and it can be a pretty big value. What happened?

In fact, we applied for a piece of memory, it is not blank. There is content in it. For example, you use new to apply for a piece of memory, and then delete, not to say that the memory empty, there is nothing. Actually just tell the system this memory I don't want. And then the other who applied to this memory, There's your content in there. You can erase your content only after it has been re-assigned.

It is naturally dangerous to use it before initializing the member variable. So be sure to initialize, this important work in C + + is left to the constructor, and C # because the default is initialized, the constructor does not appear to be as important as C + +.

Add

The static variable has the same meaning in c#,c++. However, the const variable is far away. The const variable in C # is static by default. And the const variable in C + + is not static. It's actually a bit like the ReadOnly in C #. A const member variable can only be assigned in a constructor. And can only be assigned in the initialization parameter list. If there is a member variable in class son, the const int No is the only way to assign this value

Son (): No (120)

{

}

C + + vs C # Learning: class initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.