Skip to content

Files

Latest commit

f7f58d9 · Feb 20, 2023

History

History

015_Dec22

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 20, 2023

Readme.md

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;
}