-
Notifications
You must be signed in to change notification settings - Fork 14
/
constructors.cpp
63 lines (47 loc) · 901 Bytes
/
constructors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//constructors example
#include<iostream>
using namespace std;
class human
{
public:
human() {
int age=10;
string name="Anish";
cout<<"Constructor is called"<<endl;
}
void introduce()
{
cout<<"Hello I am" <<" " << getName() << " "<<"and my age is" << getage()<<endl;
}
int getage()
{
cin>>age;
return age;
}
string getName()
{
cin>>name;
return name;
}
int age;
string name;
};
class anish : private human {
public:
human::age;
human::name;
anish () {
cout<<"Anish constructor called"<<endl;
}
void intro(int sage,string sname) {
age = sage;
name = sname;
cout<<"Hello I am "<< " " << sname << " " <<"age is :"<< sage <<endl;
}
};
int main() {
// human *obj= new human;//constructor is called as soon as an object is created
// obj->introduce();
anish A;
A.intro(20,"anish");
}