Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
963 B
1
Indexable
Never
#include <iostream>
using namespace std;

class Time {
private:
  int hours;
  int minutes;
  int seconds;

public:
  // Default constructor
  Time() {
    hours = 0;
    minutes = 0;
    seconds = 0;
  }

  // Parameterized constructor
  Time(int h, int m, int s) {
    hours = h;
    minutes = m;
    seconds = s;
  }

  // Copy constructor
  Time(const Time& t) {
    hours = t.hours;
    minutes = t.minutes;
    seconds = t.seconds;
  }

  // Display method
  void display() {
    cout << hours << ":" << minutes << ":" << seconds << endl;
  }
};

int main() {
  // Create a time object using the default constructor
  Time t1;
  cout << "t1: ";
  t1.display(); // Output: 0:0:0

  // Create a time object using the parameterized constructor
  Time t2(12, 30, 45);
  cout << "t2: ";
  t2.display(); // Output: 12:30:45

  // Create a time object using the copy constructor
  Time t3 = t2;
  cout << "t3: ";
  t3.display(); // Output: 12:30:45

  return 0;
}