0% found this document useful (0 votes)
4 views10 pages

SAMPLE PROGRAM CLASSES

The document contains two sample C++ programs. The first program defines a class 'clockType' for managing and displaying time, allowing for time manipulation and comparison. The second program defines a class 'customer' for managing water bill payments, including functionalities for bill calculation, file writing, and report display.

Uploaded by

Monette Loy-a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

SAMPLE PROGRAM CLASSES

The document contains two sample C++ programs. The first program defines a class 'clockType' for managing and displaying time, allowing for time manipulation and comparison. The second program defines a class 'customer' for managing water bill payments, including functionalities for bill calculation, file writing, and report display.

Uploaded by

Monette Loy-a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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 == otherClock.hr && min == otherClock.min && sec == otherClock.sec);

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";

myClock.setTime(x, y, z);

cout << "myClock: ";

myClock.printTime();

cout << endl;

yourClock.setTime(x, y, z);

cout << "yourClock: ";

yourClock.printTime();

cout << endl;

if (myClock.equalTime(yourClock))

cout << "\nBoth clocks are the same.\n";

yourClock.printTime();

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() {

outfile.open("waters.txt", ios_base::app); // Open the file inside the function

if (outfile.fail()) {

cout << "Error opening file." << endl;

return;

outfile << name << endl;

outfile << gallons << endl;

outfile << fixed << setprecision(2) << bill << endl;

outfile.close(); // Close the file after writing

void customer::display_report() {

infile.open("waters.txt");

if (infile.fail()) {

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;
}

infile.close();

cout << endl;

system("pause"); // For Windows; replace with cin.get() 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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy