Site icon T4Tutorials.com

Which of the following is not a type of constructor?

Which of the following is not a type of constructor?
a. Friend constructor
b. Copy constructor
c. Default constructor
d. Parameterized constructor
Answer:
Friend constructor


Friend constructor is not a constructor but the Friend function exists in C++.

C++ Program of Friend function

#include <iostream>    
using namespace std;    
class Room    
{    
    private:    
        int width;    
    public:    
        Room(): width(0) { }    
        friend int printWidth(Room); //friend function    
};    
int printWidth(Room b)    
{    
   b.width += 10;    
    return b.width;    
}    
int main()    
{    
    Room b;    
    cout<<"Width of Room: "<< printWidth(b)<<endl;    
    return 0;    
}    

 

Types of constructors in C++

C++ has 3 types of constructors;

  1. Copy constructor
  2. Default constructor
  3. Default constructor

C++ Program of Copy constructor & parameterized constructor

#include <iostream>  
using namespace std;  
class t4tutorials  
{  
   public:  
    int num;  
    t4tutorials (int a)                // parameterized constructor.  
    {  
      num =a;  
    }  
t4tutorials (t4tutorials &i)               // copy constructor  
    {  
        num = i. num;  
    }  
};  
int main()  
{  
  t4tutorials abc(44);               // Calling the parameterized constructor.  
 t4tutorials xyz(abc);                //  Calling the copy constructor.  
 cout<<xyz. num;  
  return 0;  
}  

C++ Program of Copy constructor & parameterized constructor

// Cpp program to show Default Constructor 
#include <iostream> 
using namespace std; 
  
class BSCS { 
public: 
    int x, y; 
  
    // Default Constructor 
    BSCS() 
    { 
        x = 44; 
        y = 55; 
    } 
}; 
  
int main() 
{ 
    // Default constructor called automatically 
    // when the object is created 
    BSCS t4tutorials; 
    cout << "x: " << t4tutorials.x << endl 
         << "y: " << t4tutorials.y; 
    return 1; 
}

1000+ Programming C Plus Plus MCQs

Exit mobile version