-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposition.cpp
60 lines (46 loc) · 1.35 KB
/
composition.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
#include <iostream>
#include <cstring>
using namespace std;
class Birthday {
int day, month, year;
public:
//constructor
Birthday() : day(0), month(0), year(0) {} //default constructor
Birthday(int day, int month, int year) : day(day), month(month), year(year) {} //paramterized
//member functions
void setDate(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
} //setter
string Display() const {
return to_string(day) + "/" + to_string(month) + "/" + to_string(year);
}
~Birthday() {
cout<<"Birthday destructor called"<<endl;
}
};
class Person {
char* name;
Birthday obj;
public:
Person(const char* name, int y = 0, int m = 0, int d = 0) : name(nullptr) {
this->name = new char[strlen(name)+1]; //creating char array
strcpy(this->name, name); //copying name
obj.setDate(y, m, d); //setting date
}
~Person() {
cout<<"person destructor called"<<endl;
delete[] name;
}
void display() const {
cout << "Name: " << name << endl;
cout << "Birthdate: " << obj.Display() << endl;
}
};
int main() {
//we can call them independently
Person obj("ali", 2002, 2, 2);
obj.display();
return 0;
}