answersLogoWhite

0

Need of constructor in c plus plus?

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor.

A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

You call a constructor automatically whenever you instantiate an object, either by declaring a reference to the object type, or by using the new keyword to create a pointer to an object type.

Some examples:

// Instantiate a reference to an instance of CString:

CString myRef; // Calls the default constructor.

myRef = "Hello world"; // Calls the assignment operator.

// Declare a pointer to a CString object:

CString *myPointer; // Does not call constructor; no instance is created.

// Point to an existing instance of CString:

myPointer = &myRef; // Does not call constructor; instance already exists.

// Copy an instance of CString:

myPointer = new CString( myRef ); // Calls copy constructor.

delete( myPointer); // Call destructor.

// Point to a new instance of CString:

myPointer = new CString(); // Call default constructor.

*myPointer = myRef; // Call the assignment operator.

delete( myPointer ); // Call destructor.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The constructor is the first function that is called by default when you create an object of a class.

The importance of constructor is as follows:

1. It lets you initialize the members of the class or default memory allocation for the class

2. It helps you to perform certain operations that you would like to be done when an object of the class is created.

Also based on the types of constructor the need varies, like

Default constructor is in built constructor that would define the member variable without any initialization value.

A Parameter constructor allows the user to pass arguments to the constructor and hence perform certain operations as per user requirement.

A copy constructor would allow you to copy an object of a class with another and hence avoiding the need of explicit copying the elements.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Need of constructor in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.

Related questions

Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


Constructor and destructor invocation in c?

Not possible in C.


Why can you not copy the address of a constructor in c plus plus?

You cannot copy constructor addresses because they are not functions. They have no return type and you cannot call them directly, so it makes no sense whatsoever to point at them. They are invoked automatically whenever you instantiate an object of the type, and that's all they will ever need to do. Pointing at them would serve no purpose, so the language simply does not allow it.


What do you mean by initialisation of objects in c plus plus?

Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.


Can you declare constructor as a private in c plus plus?

Yes. However, like any other private member, a private constructor can only be accessed by the class itself (typically via a static member function) or by one of its friends. There are very few cases where private constructors are appropriate, one of the most common being the need to suppress the compiler-generated copy construction of a base class. However, since C++11, suppressed constructors can simply be deleted, thus making error messages much more meaningful to users of your class. For example, instead of the following: class A { public: A (); // default constructor private: A (const A&); // suppress copy constructor (can still be invoked by the class and its friends) // ... }; You'd now use the following: class A { public: A (); // default constructor A (const A&) =delete; // suppress copy constructor (cannot be invoked at all) // ... };


What is conustrutor?

A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.