answersLogoWhite

0

A constructor is a code segment that is invoked automatically whenever you instantiate an instance of a class. All non-primitive objects have at least one constructor but class designers can create as many constructors as required, giving consumers the freedom to construct objects more efficiently. If you don't declare any constructors, the compiler will generate both a default constructor (one that has no arguments) and a copy constructor for you.

The purpose of a constructor is simply to initialise the class member variables. This is usually done via the class constructor initialisation list. Constructors also have a body (much like a function) which can be used to call methods of the class to perform more complex initialisations. However the initialisation list is the most efficient method, as it is not unlike initialising a primitive variable at the point of instantiation, rather than instantiating and initialising variables in two stages as you would with C.

Constructors are also subject to access specifiers, but will usually be declared public unless the class is an abstract base class in which case they may be declared protected. Private access is typically used when declaring singleton classes (where construction is achieved via a static member method that instantiates the one and only instance of the class, which is itself declared a static member variable of the class).

Note that copy constructors are invoked automatically whenever you pass objects to functions by value. Copying a derived object automatically invokes its base class copy constructors, while copying containers automatically invokes the embedded object copy constructors. This is why pass by reference (or via pointer) is the preferred method of passing objects into functions, as there is no need to reconstruct the object being passed.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Why c plus plus is called as oops?

OOP means "object oriented programming" this means that you can make objects instead of C where you use procedural programming (it's advance try to google it :) )


Is 'c' is a oops language?

If you meant 'C++' then yes; otherwise no.


Why c plus plus is partially oops and obs?

C++ is not 100% OOP because it inherits from C (a non-OOP language) and therefore supports all primitive C types which are strictly non-object-oriented. C# and Java are 100% object oriented as all "primitives" are object-based.


Can you use C as OOPS?

No, it is a programming language.


Is cpp complete oops?

No, C++ is not a strict OOP language as like Java and C#. C++ supports all OOPS concept like Encapsulation, Polymorphism, Inheritance etc. But C++ provides a way to expose the private data to outside world using friend functions, which is a violation of OOPS.


Why construtor is not inherited in c sharp?

C#'s cocntructors ARE Inherited, thus I cannot answer why it is NOT inherited. It is like I asked "Why the female is easier to have prostate cancel than male?" (only male have prostate, female don't)


What is difference between c and oops?

C is a programming language, oops is what you say when you realize you were wrong in something. Note: Some programming languages are known as object-orient languages, C is not one of them, but some derivatives of it (C++, C#, Java) are.


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What is c plus c plus 2c plus c plus c equal?

c + c + 2c + c + c = 6c


B plus b plus b plus c plus c plus c plus c equals?

b + b + b + c + c + c + c = 3b + 4c


Symplify c plus c plus c plus c?

4c


What is c plus c plus c plus c plus c?

c + c + c + c + c = 5 * c.