Skip to content

Commit 882d5eb

Browse files
committed
default constructor example
1 parent 26b1e0e commit 882d5eb

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

constructorOverloading.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class human
1313
cout<<"Default Constructor is called"<<endl;
1414
}
1515

16-
human(int iage,string iname) {
16+
human(int iage=19,string iname="Mrinal") {
1717
age=iage;
1818
name=iname;
1919
cout<<"overloaded constructor called"<<endl;
@@ -51,7 +51,7 @@ int main() {
5151
//default constructor called
5252
obj.introduce();
5353

54-
human obj2(20,"mrinal");//arguments passed to the object
54+
human obj2;//arguments passed to the object
5555
//constructor overloaded
5656

5757
obj2.introduce();

defaultConstructor.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//constructors example
2+
#include<iostream>
3+
4+
using namespace std;
5+
6+
7+
class human
8+
{
9+
public:
10+
11+
//default values of the constructor params
12+
human(int iage=19,string iname="Mrinal") {
13+
age=iage;
14+
name=iname;
15+
cout<<"Object created with default constructor values"<<endl;
16+
}
17+
18+
void introduce()
19+
{
20+
cout<<"Hello I am" <<" " << name << " "<<"and my age is" << age<<endl;
21+
}
22+
23+
/* /int getage()
24+
{
25+
cin>>age;
26+
return age;
27+
28+
}
29+
string getName()
30+
{
31+
32+
cin>>name;
33+
return name;
34+
35+
}*/
36+
37+
private:
38+
int age;
39+
string name;
40+
41+
42+
};
43+
44+
45+
int main() {
46+
human obj;//constructor is called as soon as an object is created
47+
obj.introduce();
48+
49+
50+
}

0 commit comments

Comments
 (0)