Answer to Question #210919 in C++ for Jawad

Question #210919

Write a C++ Program to illustrate default constructor, parameterized constructor and copy constructors.


1
Expert's answer
2021-06-27T14:10:29-0400
#include <iostream>
using namespace std;
class Constructors{
    int value;
    public:
    Constructors():value(0){
        cout<<"Default constructor called...";
    }
    Constructors(int val):value(val){
        cout<<"Parameterized constructor called...";
    }
    Constructors(const Constructors &temp){
        cout<<"Copy constructor called...";
        this->value = temp.value;
    }
    void printValue(){
        cout<<value<<endl;
    }
};
int main(){
    Constructors constructor;
    constructor.printValue();
    constructor = Constructors(2);
    constructor.printValue();
    Constructors constructor3(constructor);
    constructor3.printValue();
    return 0;
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS