SAMPLE PROGRAM CLASSES
SAMPLE PROGRAM CLASSES
SAMPLE 1:
#include<iostream>
class clockType {
public:
void incrementSeconds();
void incrementMinutes();
void incrementHours();
private:
int hr;
int min;
int sec;
};
hr = hours;
else
hr = 0;
if (minutes >= 0 && minutes < 60)
min = minutes;
else
min = 0;
incrementHours();
sec = seconds;
else
sec = 0;
incrementMinutes();
hours = hr;
minutes = min;
seconds = sec;
void clockType::incrementHours()
hr++;
hr = 0;
void clockType::incrementMinutes()
min++;
min = 0;
incrementHours();
}
void clockType::incrementSeconds()
sec++;
sec = 0;
incrementMinutes();
int main()
clockType myClock;
clockType yourClock;
int x, y, z;
cin >> x;
cin >> y;
cin >> z;
cout << "\nOutput...\n";
myClock.setTime(x, y, z);
myClock.printTime();
yourClock.setTime(x, y, z);
yourClock.printTime();
if (myClock.equalTime(yourClock))
yourClock.printTime();
}
SAMPLE 2:
#include <iostream>
#include <fstream>
#include <iomanip>
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;
do {
bill = 30.0;
else
file_write();
cout << "Do you want to compute another bill (Y/N)? ";
ans = toupper(ans);
customer OtherCustomer(1);
}
void customer::file_write() {
if (outfile.fail()) {
return;
void customer::display_report() {
infile.open("waters.txt");
if (infile.fail()) {
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;
}
infile.close();
customer OtherCustomer(1);
customer::customer(int ch) {
system("cls");
cout << "[3] Quit the program" << endl << endl;
do {
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:
customer OtherCustomer(1);
int main() {
customer Customer(1);