-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_1.cpp
68 lines (63 loc) · 2.89 KB
/
6_1.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
64
65
66
67
68
/*Write a C++ program having class time with data members: hr,
min and sec. Define following member functions.
1) getdata() to enter hour, minute and second values
2) putdata() to print the time in the format 11:59:59
3) default constructor
4) parameterized constructor
Use 52 as default value for sec in parameterized constructor.
5) copy constructor
6) Destructor.
Use the concepts of default constructor, parameterized constructor,
Copy constructor, constructor with default arguments and
destructor.
*/
//This program is performed by 22CS051_DARSH
#include<iostream>
using namespace std;
class Time{
private:
int hr, min, sec; // private member variables to store hours, minutes, and seconds
public:
Time() { // Default constructor
hr = min = sec = 0; // initializes all member variables to 0
}
Time(int h, int m, int s=52) { // Parameterized constructor with default value for seconds
hr = h; // initializes hr with value of h
min = m; // initializes min with value of m
sec = s; // initializes sec with value of s or default value of 52
}
Time(const Time &t) { // Copy constructor
hr = t.hr; // initializes hr with value of hr from t
min = t.min; // initializes min with value of min from t
sec = t.sec; // initializes sec with value of sec from t
}
~Time() { // Destructor
cout << "Object destroyed" << endl; // displays a message when object is destroyed
}
void getdata() { // Member function to get time values from user
cout << "Enter time values (hours, minutes, seconds): ";
cin >> hr >> min >> sec; // reads values from user input and stores in hr, min, sec respectively
}
void putdata() { // Member function to display time values
cout << hr << ":" << min << ":" << sec << endl; // displays hr, min, sec separated by colons
}
};
int main() {
Time t1; // creates an object t1 using default constructor
cout << "Default constructor output: ";
t1.putdata(); // displays time values of t1
Time t2(12, 30); // creates an object t2 using parameterized constructor with values 12, 30
cout << "Parameterized constructor output: ";
t2.putdata(); // displays time values of t2
Time t3(t2); // creates an object t3 using copy constructor with values from t2
cout << "Copy constructor output: ";
t3.putdata(); // displays time values of t3
Time t4; // creates an object t4 using default constructor
t4.getdata(); // gets time values from user for t4
cout << "User input time: ";
t4.putdata(); // displays time values of t4
Time t5; // creates an object t5 using default constructor
t5.putdata(); // displays time values of t5
cout << "This program is performed by 22CS051_DARSH" << endl;
return 0;
}