Using constructors is a strange doubt

I read about designers

When an object is created for a class, c'tors (if explicitly written or by default) are the starting points for execution. My doubts:

  • is c'tor more like main() in C
  • Yes, I understand that you can set all the defaults using c'tor. I can also imitate by writing a custom method. Then why c'tor?

Example:

 //The code below is written in C#. public class Manipulate { public static int Main(string[] args) { Provide provide = new Provide(); provide.Number(8); provide.Square(); Console.ReadKey(); return 0; } } public class Provide { uint num; public void Number(uint number) { num = number; } public void Square() { num *= num; Console.WriteLine("{0}", num); } } 

I'm learning to program on my own, so depending on the community of developers, you can also offer me a good OOP resource to better understand. If I'm off topic, forgive me.

+7
constructor oop
source share
9 answers

Head First OOA & D will be a good start.

Do not assume that calling a function to set each member variable of your class is a bit overhead.

Using the constructor, you can initialize all member variables in one go. Isn’t this reason enough to create constructors.

+3
source share

The functionality of the constructor and destructor can be emulated using conventional methods. However, what makes these two types of methods unique is that the language treats them in a special way.

They are automatically called when an object is created or destroyed. This is a single tool for handling the most delicate operations that must be performed during these two critical periods of an object’s life. This allows the end users of the class to forget to call them at the appropriate times.

In addition, advanced OO features, such as inheritance, require uniformity to even work.

+2
source share

First of all, most of the answers will depend at least on what you use. Reasons of great importance in one language do not necessarily have direct analogues in other languages. For example, in C ++ there are quite a few situations where temporary objects are created automatically. Ctor is called as part of this process, but for most practical purposes it is not possible to explicitly refer to other member functions in the process. This does not necessarily apply to other OO languages, although some of them will not create temporary objects implicitly.

+2
source share

Generally, you should do all your initialization in the constructor. The constructor is the first thing that is called when an instance of your class is created, so you should set the default values ​​here.

+1
source share

I think a good way to learn is to compare OOP between languages, it is like seeing the same picture from different angles.

Go to Google:

Why designers? The main difference between a simple function (which can also have functions inside) and an Object is the way that an object can be placed inside a "variable", with everything that it functions inside, and which can also fully respond to another "variable" with the same "object" inside. The way to make them have the same structure with different behavior depends on the arguments you gave the class. So here is a lazy example:

car () is now a class.

c1 = car ()

c2 = car ()

¿C1 - exactly c2? Yes.

c1 = car (volkswagen)

c2 = car (lamborghini)

C1 has the same functions as C2, but they are completely different types of cars () The volkswagen and lamborghini variables were passed directly to the constructor. Why is it a constructor? why not any other function? Answer: order.

This is my best shot, man, in those late hours. I hope I somehow helped.

+1
source share

You cannot emulate a constructor in a user method, because the user method is not called when the object is created. Only the constructor is called. Well, of course, you can call your own method after creating the object, but this is not a convention, and other people using your object will not be able to do this.

A constructor is simply an agreement that is agreed upon as a way to customize your object after it is created.

+1
source share

One of the reasons we need a constructor is "encapsulation", the code makes the initialization something invisible

0
source share

You also cannot force variables without using a constructor. If you want to instantiate an object only if you passed an int to it, you can set the default constructor as private and make your int constructor. Thus, it is not possible to create an object of this class without using int.

0
source share

Subconstructors will be initialized in the constructor. In languages ​​like C ++, where entity objects exist inside an object-object (and not as separate objects connected via pointers or descriptors), the constructor is your only chance to pass parameters to the sub-object constructors. Even in Java and C #, any base class is directly contained, so the parameters of its constructor must be provided by your constructor.

Finally, any member constant (or in C #, readonly ) can only be specified from the constructor. Even helper functions called from the constructor cannot change them.

0
source share

All Articles