开发者

C++: Using a single get function to return the variables related to an object

开发者 https://www.devze.com 2023-03-06 03:42 出处:网络
I am a novice programmer working on a coding project for school. The problem domain is as follows: Write a C++ program related to a clock

I am a novice programmer working on a coding project for school.

The problem domain is as follows:

Write a C++ program related to a clock

  • Define a class called Clock , saved in a separate file, that includes the following members:
    • three member variables related to the hours, minutes and seconds
    • one accessor function that returns the values of the three variables (hint: use pointers)
    • one mutator function that modifies all variables (hint: use three arguments)
    • functions to increment each variable with 1 (hint: three function)
    • overloading functions to increment the variables with a value entered by the user (hint: the value is the argument of the function)
    • one default constructor that sets the clock to 0 hours, 0 minutes and 0 seconds
    • an overloading constructor that sets the clock to a given time (hr, min, sec)
  • Define a second class, saved in a separate file, that includes

    • a static member variable
    • a static member function
  • Include in the previously defined files C++ code that insures that a class will not be loaded twice in the main program
  • Define a main program, saved in a separate file, that
    • instantiates three objects, one with the default constructor of Clock, one with the overloaded constructor of Clock, and one based on the second class
    • uses the objects to call all the functions defined in the two classes:
    • use several ”cin” statement to read from the user the desired time and use the values as parameters for the corresponding functions that required
    • call each function only once, using one of the defined objects
    • use the accessor function to print the time after each function call that modifies the variables of the Clock class
    • includes code that uses the static members of th开发者_如何学Pythone second class

we have done many examples in class where we are able to use separate get\set functions but this is the first time we are trying to do this with a single get function and pointers. Quite frankly, I am lost

Here is the contents of my header file:

// default class definition
#ifndef CLOCK1_H
#define CLOCK1_H

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

class Clock
{
private:
    int hours;
    int minutes;
    int seconds;

public:
    int Clock::getInitialTime();
    int Clock::setClockTime();
    Clock::Clock();             // default constructor
    Clock::~Clock();                // default destructor
};
#endif

Here is the contents of my source code file:

// function declarations \ main program
//

#include "stdafx.h"
#include "Clock1.h"
#include <iostream>
#include <conio.h>
using namespace std;

int Clock::getInitialTime()
{
    return hours, minutes, seconds;
}

//void Clock::setClockTime(int hr,min,sec)
//{
//  hours=hr;
//  minutes=min;
//  seconds=sec;
//}


// default constructor
Clock::Clock()
{
    hours=0;
    minutes=0;
    seconds=0;
}

// default destructor
Clock::~Clock()
{
    cin.get();
    cin.get();
}

int main()
{
    Clock defaultObj;
    defaultObj.getInitialTime();
    cout << "The initial time is " << defaultObj.getInitialTime() << endl;
    return 0;
}

I am trying to take this in small steps, the first goal being to be able to output the initial values for the hours, minutes, seconds. once this is done, I can add additional constructor(s) with additional arguments.

My first guess is that I need to add the following:

Constructor: add the appropriate arguments to the default constructor

Clock::Clock(int *hourPrt, int *minutePtr, int *secondPrt)

create the pointers

Clock *hourPtr;
Clock *minutePtr;
Clock *secondPtr;

associate them with the object's attributes

hourPrt=defaultObj.hours
minutePtr=defaultObj.minutes
secondPrt=defaultObj.seconds

getInitialTime function modify it some how

function call in main modify it some how

Can anyone help me with this?

Thanks


The constructor is not one of the functions you need to change. Start by writing void Clock::setClockTime(int hours, int minutes, int seconds), it's the easiest.


Please also note that you do not prefix member functions with the class name when inside the class.

class Clock
{
public:
    Clock::Clock();             // WRONG
    Clock(); // RIGHT way to declare constructor
};


You could either create a struct which contains the three values and return that, or you could return a pointer to an array of 3 ints -- make the element [0] be hours, [1] be minutes, etc. Note that if you're going to go that way in real C++ code you'd want to use something like vector so that the client doesn't have to call delete [] on the returned array later. :)


To "return" more than one argument, you have to use pointers:

void get_values(int *hours, int *minutes, int *seconds) {
  *hours = this->hours;
  *minutes = this->minutes;
  *seconds = this->seconds;
}

Usage:

int hours, minutes, seconds;
clock.get_values(&hours, &minutes, &seconds);


For a first step, I would make the following changes:

  • Change getInitialTime() to return a Clock type
  • Add a method to display the value of a Clock object. (This could eventually be changed to be an ostream output operator).

    void Display(const Clock& clock)
    {
        cout << clock.Hour << ":" << clock.Minutes << ":" clock.Seconds << endl;
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消