Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. Clock.h Fife:
  2.  
  3. #ifndef ASSIGNMENT_4_CLOCK_H
  4. #define ASSIGNMENT_4_CLOCK_H
  5. #pragma once
  6.  
  7. class Clock {
  8.     private:
  9.         int hours, mins, secs;
  10.  
  11.     public:
  12.         Clock(void);//Default Constructor
  13.  
  14.         Clock(int, int, int);//Overloaded Constructor
  15.  
  16.         void setClock(int, int, int);//Setter
  17.         void getClock (int *hours, int *mins, int *secs);//Getter
  18.  
  19.         void incrementHours();
  20.         void incrementMins();
  21.         void incrementSecs();
  22.  
  23.         //Overloading Functions
  24.         void incrementHours(int);
  25.         void incrementMins(int);
  26.         void incrementSecs(int);
  27. };
  28.  
  29. #endif
  30.  
  31. =======================================================================================================================================
  32.  
  33. Clock.cpp File:
  34.  
  35. #include "Clock.h"
  36. #include <iostream>
  37. #include <cstdlib> //system()
  38.  
  39. Clock::Clock(void){ //Default Constructor
  40.     hours = 0;
  41.     mins = 0;
  42.     secs = 0;
  43. }
  44.  
  45. Clock::Clock(int h, int m, int s){ //Overloaded Constructor
  46.     hours = h;
  47.     mins = m;
  48.     secs = s;
  49.  
  50.     //Function Call Increment Values
  51.     incrementHours();
  52.     incrementMins();
  53.     incrementSecs();
  54. }
  55.  
  56. void Clock::setClock(int h, int m, int s){ //Sets values for the variables
  57.     if(0<h && h<24)
  58.         hours = h;
  59.     if(0<m && m<60)
  60.         mins = m;
  61.     if(0<s && s<60)
  62.         secs = s;
  63. }
  64.  
  65. void Clock::getClock(int *h, int *m, int *s){ //Pointers. Gets the values after user enters them
  66.     *h = hours, *m = mins, *s = secs;
  67. }
  68.  
  69. //Incrementing default values by one
  70.  
  71. void Clock::incrementHours(){
  72.     hours++;
  73. }
  74.  
  75. void Clock::incrementMins(){
  76.     mins++;
  77. }
  78.  
  79. void Clock::incrementSecs(){
  80.     secs++;
  81. }
  82.  
  83. //Overloading Functions ***Same Name, Different # of Arguments***
  84.  
  85. void Clock::incrementHours(int user_h){
  86.     hours = hours + user_h; //Increment by value entered by the user
  87. }
  88.  
  89. void Clock::incrementMins(int user_m){
  90.     mins = mins + user_m;
  91. }
  92.  
  93. void Clock::incrementSecs(int user_s){
  94.     secs = secs + user_s;
  95. }
  96.  
  97. =======================================================================================================================================
  98.  
  99. Box.h File:
  100.  
  101. #ifndef ASSIGNMENT_4_BOX_H
  102. #define ASSIGNMENT_4_BOX_H
  103. #pragma once
  104.  
  105. class Box {
  106.     public:
  107.         static int objectCount;
  108.  
  109.         Box(double, double, double); //Constructor
  110.  
  111.         double Volume();
  112.  
  113.         static int getObjectCount(); // Static Function
  114.  
  115.     private:
  116.         double length, width, heigt;
  117. };
  118.  
  119. int Box::objectCount = 0;
  120.  
  121. #endif
  122.  
  123. =======================================================================================================================================
  124.  
  125. Box.cpp File:
  126.  
  127. #include "Box.h"
  128.  
  129. Box::Box(double l=2.0, double w=2.0, double h=2.0) { //Constructor
  130.     length = l;
  131.     width = w;
  132.     heigt = h;
  133.  
  134.     // Increase every time object is created
  135.     objectCount++;
  136. }
  137.  
  138. double Box::Volume() {
  139.     return heigt*length*width;
  140. }
  141.  
  142. int Box::getObjectCount() { // Static Function
  143.     return objectCount;
  144. }
  145.  
  146. =======================================================================================================================================
  147.  
  148. Q1.cpp File:
  149.  
  150. #include "Clock.h"
  151. #include "Box.h"
  152. #include <iostream>
  153. #include <cstdlib> //system()
  154.  
  155. using namespace std;
  156.  
  157. int main(){
  158.  
  159.     Clock clock_1; // Create clock_1 object
  160.  
  161.     int h_1, m_1, s_1; // Variables for the user input
  162.  
  163.     //User Input
  164.     cout << "Enter hours: ";
  165.     cin >> h_1;
  166.     clock_1.incrementHours(h_1); //Increment by value entered by the user
  167.  
  168.     cout << "Enter minutes: ";
  169.     cin >> m_1;
  170.     clock_1.incrementMins(m_1);
  171.  
  172.     cout << "Enter seconds: ";
  173.     cin >> s_1;
  174.     clock_1.incrementSecs(s_1);
  175.  
  176.     clock_1.setClock(h_1, m_1, s_1); // Setter
  177.     clock_1.getClock(&h_1, &m_1, &s_1); // Getter
  178.  
  179.     Clock clock_2 (h_1, m_1, s_1);
  180.  
  181.     clock_2.setClock(h_1, m_1, s_1);
  182.     clock_2.getClock(&h_1, &m_1, &s_1);
  183.  
  184.     cout << "The time is: " << h_1 << ":" << m_1 << ":" << s_1 << endl;
  185.  
  186. /*====================================================================================================================*/
  187.  
  188.     // Print total number of objects before creating object.
  189.     cout << "Inital Stage Count: " << Box::getObjectCount() << endl;
  190.  
  191.     Box Box1(3.3, 1.2, 1.5);    // Declare box1
  192.     Box Box2(8.5, 6.0, 2.0);    // Declare box2
  193.  
  194.     // Print total number of objects after creating object.
  195.     cout << "Final Stage Count: " << Box::getObjectCount() << endl;
  196.  
  197.  
  198.     system("PAUSE");
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement