Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Clock.h Fife:
- #ifndef ASSIGNMENT_4_CLOCK_H
- #define ASSIGNMENT_4_CLOCK_H
- #pragma once
- class Clock {
- private:
- int hours, mins, secs;
- public:
- Clock(void);//Default Constructor
- Clock(int, int, int);//Overloaded Constructor
- void setClock(int, int, int);//Setter
- void getClock (int *hours, int *mins, int *secs);//Getter
- void incrementHours();
- void incrementMins();
- void incrementSecs();
- //Overloading Functions
- void incrementHours(int);
- void incrementMins(int);
- void incrementSecs(int);
- };
- #endif
- =======================================================================================================================================
- Clock.cpp File:
- #include "Clock.h"
- #include <iostream>
- #include <cstdlib> //system()
- Clock::Clock(void){ //Default Constructor
- hours = 0;
- mins = 0;
- secs = 0;
- }
- Clock::Clock(int h, int m, int s){ //Overloaded Constructor
- hours = h;
- mins = m;
- secs = s;
- //Function Call Increment Values
- incrementHours();
- incrementMins();
- incrementSecs();
- }
- void Clock::setClock(int h, int m, int s){ //Sets values for the variables
- if(0<h && h<24)
- hours = h;
- if(0<m && m<60)
- mins = m;
- if(0<s && s<60)
- secs = s;
- }
- void Clock::getClock(int *h, int *m, int *s){ //Pointers. Gets the values after user enters them
- *h = hours, *m = mins, *s = secs;
- }
- //Incrementing default values by one
- void Clock::incrementHours(){
- hours++;
- }
- void Clock::incrementMins(){
- mins++;
- }
- void Clock::incrementSecs(){
- secs++;
- }
- //Overloading Functions ***Same Name, Different # of Arguments***
- void Clock::incrementHours(int user_h){
- hours = hours + user_h; //Increment by value entered by the user
- }
- void Clock::incrementMins(int user_m){
- mins = mins + user_m;
- }
- void Clock::incrementSecs(int user_s){
- secs = secs + user_s;
- }
- =======================================================================================================================================
- Box.h File:
- #ifndef ASSIGNMENT_4_BOX_H
- #define ASSIGNMENT_4_BOX_H
- #pragma once
- class Box {
- public:
- static int objectCount;
- Box(double, double, double); //Constructor
- double Volume();
- static int getObjectCount(); // Static Function
- private:
- double length, width, heigt;
- };
- int Box::objectCount = 0;
- #endif
- =======================================================================================================================================
- Box.cpp File:
- #include "Box.h"
- Box::Box(double l=2.0, double w=2.0, double h=2.0) { //Constructor
- length = l;
- width = w;
- heigt = h;
- // Increase every time object is created
- objectCount++;
- }
- double Box::Volume() {
- return heigt*length*width;
- }
- int Box::getObjectCount() { // Static Function
- return objectCount;
- }
- =======================================================================================================================================
- Q1.cpp File:
- #include "Clock.h"
- #include "Box.h"
- #include <iostream>
- #include <cstdlib> //system()
- using namespace std;
- int main(){
- Clock clock_1; // Create clock_1 object
- int h_1, m_1, s_1; // Variables for the user input
- //User Input
- cout << "Enter hours: ";
- cin >> h_1;
- clock_1.incrementHours(h_1); //Increment by value entered by the user
- cout << "Enter minutes: ";
- cin >> m_1;
- clock_1.incrementMins(m_1);
- cout << "Enter seconds: ";
- cin >> s_1;
- clock_1.incrementSecs(s_1);
- clock_1.setClock(h_1, m_1, s_1); // Setter
- clock_1.getClock(&h_1, &m_1, &s_1); // Getter
- Clock clock_2 (h_1, m_1, s_1);
- clock_2.setClock(h_1, m_1, s_1);
- clock_2.getClock(&h_1, &m_1, &s_1);
- cout << "The time is: " << h_1 << ":" << m_1 << ":" << s_1 << endl;
- /*====================================================================================================================*/
- // Print total number of objects before creating object.
- cout << "Inital Stage Count: " << Box::getObjectCount() << endl;
- Box Box1(3.3, 1.2, 1.5); // Declare box1
- Box Box2(8.5, 6.0, 2.0); // Declare box2
- // Print total number of objects after creating object.
- cout << "Final Stage Count: " << Box::getObjectCount() << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement