Cpp constructor with arguments

Constructors in C++

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors.

• Constructor is a member function of a class, whose name is same as the class name.
• Constructor is a special type of member function that is used to initialize the data members for an object of a class automatically, when an object of the same class is created.
• Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
• Constructor do not return value, hence they do not have a return type.

The prototype of the constructor looks like (list-of-parameters);
Constructor can be defined inside the class declaration or outside the class declaration a. Syntax for defining the constructor within the class (list-of-parameters) < //constructor definition >b. Syntax for defining the constructor outside the class : :(list-of-parameters) < //constructor definition >

C++

C++

Characteristics of constructor

• The name of the constructor is same as its class name. • Constructors are mostly declared in the public section of the class though it can be declared in the private section of the class. • Constructors do not return values; hence they do not have a return type. • A constructor gets called automatically when we create the object of the class. • Constructors can be overloaded. • Constructor can not be declared virtual.
• Default constructor • Parameterized constructor • Overloaded constructor • Constructor with default value • Copy constructor • Inline constructor

Constructor does not have a return value, hence they do not have a return type.

Читайте также:  Telegram bot html markup

The prototype of Constructors is as follows:

Constructors can be defined inside or outside the class declaration:-

The syntax for defining the constructor within the class:

The syntax for defining the constructor outside the class:

C++

Enter the RollNo:Enter the Name:Enter the Fee: 0 6.95303e-310

C++

Enter the RollNo: 30 Enter the Name: ram Enter the Fee: 20000 30 ram 20000

How constructors are different from a normal member function?

C++

A constructor is different from normal functions in following ways:

  • Constructor has same name as the class itself
  • Default Constructors don’t have input argument however, Copy and Parameterized Constructors have input arguments
  • Constructors don’t have return type
  • A constructor is automatically called when an object is created.
  • It must be placed in public section of class.
  • If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).

Let us understand the types of constructors in C++ by taking a real-world example. Suppose you went to a shop to buy a marker. When you want to buy a marker, what are the options. The first one you go to a shop and say give me a marker. So just saying give me a marker mean that you did not set which brand name and which color, you didn’t mention anything just say you want a marker. So when we said just I want a marker so whatever the frequently sold marker is there in the market or in his shop he will simply hand over that. And this is what a default constructor is! The second method is you go to a shop and say I want a marker a red in color and XYZ brand. So you are mentioning this and he will give you that marker. So in this case you have given the parameters. And this is what a parameterized constructor is! Then the third one you go to a shop and say I want a marker like this(a physical marker on your hand). So the shopkeeper will see that marker. Okay, and he will give a new marker for you. So copy of that marker. And that’s what a copy constructor is!

Читайте также:  Python function pass by value

Characteristics of the constructor:

  • The name of the constructor is the same as its class name.
  • Constructors are mostly declared in the public section of the class though it can be declared in the private section of the class.
  • Constructors do not return values; hence they do not have a return type.
  • A constructor gets called automatically when we create the object of the class.
  • Constructors can be overloaded.
  • Constructor can not be declared virtual.
  • Constructor cannot be inherited.
  • Addresses of Constructor cannot be referred.
  • Constructor make implicit calls to new and delete operators during memory allocation.

Types of Constructors

1. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor.

Источник

Оцените статью