How C++ uses inheritance, polymorphism object orientation, encapsulation, local classes, and nested classes

(Last Updated On: May 15, 2010)

C++ interview question: Copy Constructor vs assignment operator with const
Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?

No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.
“Why do u need a copy constructor ?”

“Why do u need a copy constructor ?”

when you will provide your own copy constructor, assignment operator. give example.

What is constructor or ctor?
Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class.
What is copy constructor?
Constructor which initializes the it’s object member variables ( by shallow copying) with another object of the same class. If you don’t implement one in your class then compiler implements one for you.
for example:
Boo Obj1(10); // calling Boo constructor
Boo Obj2(Obj1); // calling boo copy constructor
Boo Obj2 = Obj1;// calling boo copy constructor
When are copy constructors called?
Copy constructors are called in following cases:
a) when a function returns an object of that class by value
b) when the object of that class is passed by value as an argument to a function
c) when you construct an object based on another object of the same class
d) When compiler generates a temporary object

What is conversion constructor?
constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.
for example:
class Boo
Boo( int i );
Boo BooObject = 10 ; // assigning int 10 Boo object

Structures and classes are the same. The only difference is that structures are defaulted at being public while classes are defaulted at being private. Structures are a C feature so they do not have any C++ features like inheritance.
Copy constructors can overload the assignment operator to allow deep copying instead of default shallow coping. This invoked when one object is initialized to make another.
Both copy constructor and assignment operator are the same where they perform shallow copies by default. As said, you can override the default copy constructor or overload the assignment for deep copies. An KEY DIFFERENCE sample is:
sample x1=x2; //call copy constructor
sample x1;
x1=x2l //calls assignment operator
Copy constructors need a const so that the constructor does not change the state of where it is copying from. This means it cannot be altered within the function. If a function attempts to change the value, the program will not compile. It is advised to use a const in very constructor parameter. Be advised of this potential compiler error:
Foo::Foo(Foo&) {} /constructor without const
const Foo a;
Foo b(a);// error
In a nutshell remember,
1. Copy-Constructor without ‘const’ compiles.
2. Copy-Constructor without ‘const’ is called.
3. ‘const’ in CC prevents passd object from being modified.

Copy constructors have no need to check itself or clean it self up while a copy assignment needs to do this.
There are numerous reasons why you should never call a virtual method from a derived class constructor. This loses its meaning as it will case its base implementation instead.

Also, you should note order of execution in this code:
using namespace std;

class Base{
Base(){cout << "Base()" << endl;} Base(const Base&){cout << "Base(const Base&)" << endl;} ~Base(){cout << "~Base()" << endl;} }; Base func(Base b){return b;} void test1(){ Base a; func(a); } void test2(){ Base a; Base b = func(a); } int main(){ test1(); test2(); return 0; } Output is: Test1 Base() Base(const Base&) Base(const Base&) ~Base() ~Base() ~Base() Test2 Base() Base(const Base&) Base(const Base&) ~Base() ~Base() ~Base() Reasons are: Test1 Step-1 Default constructor will be called at Base a; Step-2 Copy constructor will be called when passing a as formal parameter i.e func(a). Step-3 Copy constructor will be called for return object. Step-4 Destructor will be called for actual parameter of func. Step-5 Now this return obj will act as nameless obj in test1() and will get destroyed after line func(a) calling destructor one more time. Step-6 Finally Destructor will be called for a. You can see same for test2(); Other notes about constructors: For classes with static initialization, dynamic array, or STL containers like vector or lists, you need to define a default constructor. Also, if you have a one argument contructor, you need to provide a default constructor as well. Static constructors do not exist like in Java. Just remember constructors cannot be defined as virtual, static, volatile, or const volatile. Also constructors cannot have return types. There are a few ways to create objects. This includes a few ways like: 1: TestClass objects[10] = {1,2,3,...,10}; 2: TestClass* objects[500]; Now you have 500 pointer to the TestClass objects. now initialize the pointer like - while (i<500) { objects[i] = new TestClass(0); i++; } 3: TestClass **ptr; ptr = new TestClass *[500]; for each i ptr[i] = new TestClass("value"); Need default constructor if another constructor exists with an argument. #include
using namespace std;

class A {
string s;
A(string str) {s=str;} //constructors must be public
int x;
A() {} //this is needed because of the 1 argument constructor and has to be public

int main() {
string str=”hello”;
A a1;
A *b=new A(a1);
return 0;

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Subscribe For Latest Updates

Sign up to best of business news, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.


Check NEW site on stock forex and ETF analysis and automation

Scroll to Top