Skip to content

Latest commit

 

History

History

015_Dec22

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

December 22

Type of Contructor

  1. Default
  2. Parameterized
  3. Copy

1. Default Constructor

Default Constructor is also known as zero argument constructor, as it doesn't take any parameter. It can be defined by the user if not, then the compiler creates it on its own.

Syntax

class_name{
//body 
}

Example

#include <iostream>
using namespace std;

class student{
        int roll;
        string name;

        public:
                student() { //default constructor
                    cout << "Enter the roll no: ";
                    cin >> roll;

                    cout << "Enter the name: ";
                    cin >> name;
                };
                void display() {
                    cout << "Roll no = " << roll << endl;
                    cout << "Name = " << name;
                };
};

int main() {
    student std;
    std.display();

    return 0;
}