Skip to content

Commit 9a91845

Browse files
Added the code for Default Constructor in C++
1 parent 2bd7bed commit 9a91845

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// C++ program to demonstrate Default Constructor
2+
3+
// Default Constructor - A constructor which has no argument is known as Default Constructor. It is invoked
4+
// at the time of creating object.
5+
6+
// Note : Even if we do not define any constructor explicitly, the compiler will automatically provide a
7+
// Default Constructor implicitly.
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
// class with Default Constructor defined
13+
class Person1{
14+
public:
15+
string name1;
16+
int age1;
17+
Person1(){
18+
name1="abc";
19+
age1=10;
20+
}
21+
};
22+
23+
// class with no Default Constructor defined
24+
class Person2{
25+
public:
26+
string name2;
27+
int age2;
28+
};
29+
30+
int main(){
31+
// Creating an object of class Person1
32+
Person1 p1;
33+
cout<<"Name1 : "<<p1.name1<<endl;
34+
cout<<" Age1 : "<<p1.age1<<endl;
35+
36+
// Creating an object of class Person2
37+
Person2 p2;
38+
cout<<"Name2 : "<<p2.name2<<endl;
39+
cout<<" Age2 : "<<p2.age2<<endl;
40+
return 0;
41+
}

0 commit comments

Comments
 (0)