SAMPLE PROGRAM CLASSES
SAMPLE 1:
#include<iostream>
using namespace std;
class clockType {
public:
void setTime(int, int, int);
void getTime(int&, int&, int&);
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
};
void clockType::setTime(int hours, int minutes, int seconds)
if (hours >= 0 && hours < 24)
hr = hours;
else
hr = 0;
if (minutes >= 0 && minutes < 60)
min = minutes;
else
min = 0;
incrementHours();
if (seconds >= 0 && seconds < 60)
sec = seconds;
else
sec = 0;
incrementMinutes();
void clockType::getTime(int& hours, int& minutes, int& seconds)
hours = hr;
minutes = min;
seconds = sec;
void clockType::printTime() const
if (hr < 10)
cout << "0";
cout << hr << ":";
if (min < 10)
cout << "0";
cout << min<<":";
if (sec < 10)
cout << "0";
cout << sec;
void clockType::incrementHours()
hr++;
if (hr > 23)
hr = 0;
void clockType::incrementMinutes()
min++;
if (min > 59)
min = 0;
incrementHours();
}
void clockType::incrementSeconds()
sec++;
if (sec > 59)
sec = 0;
incrementMinutes();
bool clockType::equalTime(const clockType& otherClock) const {
return (hr == [Link] && min == [Link] && sec == [Link]);
int main()
clockType myClock;
clockType yourClock;
int x, y, z;
cout << "Hours : ";
cin >> x;
cout << "Minutes : ";
cin >> y;
cout << "Seconds : ";
cin >> z;
cout << "\nOutput...\n";
[Link](x, y, z);
cout << "myClock: ";
[Link]();
cout << endl;
[Link](x, y, z);
cout << "yourClock: ";
[Link]();
cout << endl;
if ([Link](yourClock))
cout << "\nBoth clocks are the same.\n";
[Link]();
cout << endl;
}
SAMPLE 2:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class customer {
private:
string name;
int gallons;
float bill;
ofstream outfile;
ifstream infile;
public:
customer(int x);
void Pay_Bill();
void file_write();
void display_report();
};
void customer::Pay_Bill() {
char ans;
do {
system("cls");
cout << "[1] Pay water bill" << endl << endl;
cout << "Customer name : ";
cin >> name;
do {
cout << "Gallons consumed: ";
cin >> gallons;
} while (!(gallons > 0));
if (gallons <= 1000)
bill = 30.0;
else
bill = 30.0 + ((gallons - 1000) / 1000.0) * 30.0; // Fixed calculation
cout << fixed << setprecision(2);
cout << "Water Bill : " << bill << endl;
file_write();
cout << "Do you want to compute another bill (Y/N)? ";
cin >> ans;
ans = toupper(ans);
} while (ans == 'Y');
customer OtherCustomer(1);
}
void customer::file_write() {
[Link]("[Link]", ios_base::app); // Open the file inside the function
if ([Link]()) {
cout << "Error opening file." << endl;
return;
outfile << name << endl;
outfile << gallons << endl;
outfile << fixed << setprecision(2) << bill << endl;
[Link](); // Close the file after writing
void customer::display_report() {
[Link]("[Link]");
if ([Link]()) {
cout << "Error opening file." << endl;
return;
cout << setw(20) << "Customer name" << setw(30) << "Gallons consumed" << setw(30) <<
"Water Bill" << endl;
while (infile >> name >> gallons >> bill) { // Fixed loop condition
cout << setw(20) << name << setw(30) << gallons << setw(30) << bill << endl;
}
[Link]();
cout << endl;
system("pause"); // For Windows; replace with [Link]() for Linux/Mac
customer OtherCustomer(1);
// Fixed Constructor Implementation
customer::customer(int ch) {
system("cls");
cout << "WATER BILL COMPUTATION" << endl << endl;
cout << "[1] Pay water bill" << endl;
cout << "[2] Display report" << endl;
cout << "[3] Quit the program" << endl << endl;
do {
cout << "Enter choice: ";
cin >> ch;
} while (!(ch >= 1 && ch <= 3));
switch (ch) {
case 1:
Pay_Bill();
break;
case 2:
system("cls");
display_report();
break;
case 3:
cout << endl << "Thank you for using the system" << endl;
exit(1);
break;
default:
cout << "Invalid option" << endl;
customer OtherCustomer(1);
int main() {
customer Customer(1);