File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments