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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#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;
- Copy constructor
- Default constructor
- Default constructor
C++ Program of Copy constructor & parameterized constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// 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; } |