A constructor has the same …………….. as that of class.
Which of the followings is/are automatically added to every class, if we do not write our own.
1 Crore+ students have signed up on EduRev. Have you? Download the App |
A constructor that accepts no parameters is called the …………….
Output of following program?
#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1;
return 0;
}
Predict the output of following C++ program
#include <iostream>
using namespace std;
int i;
class A
{
public:
~A()
{
i=10;
}
};
int foo()
{
i=3;
A ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
State whether the following statements about the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.
Output of following program?
#include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Normal Constructor calledn"; }
Point(const Point &t) { cout << "Copy constructor calledn"; }
};
int main()
{
Point *t1, *t2;
t1 = new Point();
t2 = new Point(*t1);
Point t3 = *t1;
Point t4;
t4 = t3;
return 0;
}
Like constructors, can there be more than one destructors in a class?
When an object is created and initialized at the same time, a ………………. gets called.
What is the output of following program?
#include <iostream>
using namespace std;
class Point
{
int x, y;
public:
Point(const Point &p) { x = p.x; y = p.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1;
Point p2 = p1;
cout << "x = " << p2.getX() << " y = " << p2.getY();
return 0;
}
What is the output of following program?
#include <iostream>
using namespace std;
class A
{
int id;
static int count;
public:
A() {
count++;
id = count;
cout << "constructor for id " << id << endl;
}
~A() {
cout << "destructor for id " << id << endl;
}
};
int A::count = 0;
int main() {
A a[3];
return 0;
}
…………….. constructor will not do anything and defined just to satisfy the compiler
Predict the output of following program.
#include<iostream>
#include<stdlib.h>
using namespace std;
class Test
{
public:
Test()
{ cout << "Constructor called"; }
};
int main()
{
Test *t = (Test *) malloc(sizeof(Test));
return 0;
}
73 videos|7 docs|23 tests
|