What is differences between private and constructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is differences between private and constructor?

7th Jul 2019, 12:28 PM
Ananya Seghal
Ananya Seghal - avatar
6 Answers
+ 5
Constructor is a special member function of the class which get automatically called on object creation. While private section in c++ is used to declare and define variables,member fuctions which are accessible only within the class,not to any other class and main function as well.
7th Jul 2019, 3:06 PM
Mukul Roy
Mukul Roy - avatar
+ 4
Seb TheS are you saying that all constructors are private?
8th Jul 2019, 3:23 AM
Sonic
Sonic - avatar
+ 3
Constructor is a magic method, and magic methods are all private. Private is only an attribute of a name, private members of a class: Are not strictly accessible from outside from the class. Will not be strictly inherited by the class' objects. Will not be strictly inherited to the childclasses.
7th Jul 2019, 1:45 PM
Seb TheS
Seb TheS - avatar
+ 2
Private in C# and some other languages is a way to make a method available or not to another method or object... a constructor, in C# is a method that runs, when you instantiate an object of a class. Example: there is this class named qwerty, and there is this method that is also named querty. Both of them are outside the Main class. In the main class, you can instantiate an object of the qwerty class, wich will also call the qwerty method(that is the constructor). This method will run first and than the class.
7th Jul 2019, 10:26 PM
Milena Patrocinio
Milena Patrocinio - avatar
+ 1
Haha, 4 sure, that is not what Im saying. Im just saying they are two whole different things
8th Jul 2019, 3:24 AM
Milena Patrocinio
Milena Patrocinio - avatar
0
Private is access specifier. Private data members can not accessible outside the class. A constructor is a special member function of a class whose name is same as a class name and which is automatically called when an object is called.
8th Jul 2019, 7:05 PM
OMKAR A. POWAR
OMKAR A. POWAR - avatar