#include<iostream>
using namespace std;
class C{
private:
int value;
public:
C(){
value = 0;
cout<<"default constructor"<<endl;
}
C(const C& c){
value = c.value;
cout<<"copy constructor"<<endl;
}
};
int main(){
C c1;
C c2 = C();
}
Output:
default constructor
default constructor
Question:
For C c1;
default constructor will be called obviously, for C c2 = C();
I thought default constructor will be called to initialize a temporary object, then copy constructor will be call to initialize c2, It seems that I am wrong. why?
C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is a subset of C++.
Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.
Although they sound alike, C, C++, and C# are different programming languages. Let us understand the difference between two of the most widely used programming languages, C++ and C#.
C language supports procedural programming. Whereas C# supports object oriented programming.
This is an example of copy elision - basically the compiler is allowed to optimize away the copy. Described here: http://en.cppreference.com/w/cpp/language/copy_elision
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With