Thursday, August 26, 2021

constructors in c++ with examples


Constructors:

Special function

Without return type

Name of class and constructor are same

Default constructors:

This constructor automatically created when we create an object

No need to call

This constructor has no argument pass

#include<iostream>
using namespace std;
class employee
{
    string name;
    int employee_id;
    int selary;
    public:
    employee()
    {
        cout<<"enter name"<<endl;
        cin>>name;
        cout<<"enter employee id"<<endl;
        cin>>employee_id;
        cout<<"enter selary"<<endl;
        cin>>selary;
    }
    void display()
    {
        cout<<"-----------------------"<<endl;
        cout<<"name             "<<name<<endl;
        cout<<"employee  "<<employee_id<<endl;
        cout<<"selary         "<<selary<<endl;
        cout<<"-----------------------"<<endl;
    }
};
int main()
{
    employee s2;
    s2.display();
    return 0;
}

Parameterized constructor:

This type of constructor has argument pass in the constructor

We call it and add parameters

#include<iostream>
using namespace std;
class employee
{
    string s_name;
    int roll_no;
    int s_marks;
    public:
    employee(string name,int roll ,int marks)
    {
        s_name=name;
        roll_no=roll;
        s_marks=marks;
    }
    void display()
    {
        cout<<"------------------------------"<<endl;
        cout<<"name       "<<s_name<<endl;
        cout<<"roll no    "<<roll_no<<endl;
        cout<<"marks      "<<s_marks<<endl;
        cout<<"------------------------------"<<endl;
    }
};
int main()
{
    employee s("waqas",17,487);
    s.display();
;
    return 0;
}

Copy constructors:

it used to copy of one object to another

initialization of an object with another object called copy constructor

We also need to call it for use

Syntax:

Class name (const class name & object name)

#include<iostream>
using namespace std;
class student
{
    string s_name;
    int roll_no;
    int s_marks;
    public:
    student (string name, int roll, int marks)
    {
        s_name=name;
        roll_no=roll;
        s_marks=marks;
    }
    student(student &s)
    {
        s_name=s.s_name;
        roll_no=s.roll_no;
        s_marks=s.s_marks;
    }
    void display()
    {
        cout<<"-----------------------------------"<<endl;
        cout<<"name                         "<<s_name<<endl;
        cout<<"roll no                      "<<roll_no<<endl;
        cout<<"marks                        "<<s_marks<<endl;
        cout<<"-----------------------------------"<<endl;
    }
};
int main()
{
    student s1("waqas",17,487);
    student s3(s1);
    cout<<"parameterized constuctor"<<endl;
    s1.display();
    cout<<"copy constutor"<<endl;
    s3.display();
    return 0;
}

No comments:

Post a Comment

How to apply for Canada visa?

  Applying for a Canada Visa: A Comprehensive Guide Explore the Beauty of Canada - Your Step-by-Step Visa Application Guide Introduction Can...