Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   put method, main and member access specifiers separately (http://www.programmingforums.org/showthread.php?t=13470)

cairo Jul 4th, 2007 5:39 AM

put method, main and member access specifiers separately
 
is it possible to put methods, main and member access specifiers separately? i mean example like:

a.cpp = methods
test.cpp=main
type.hh=member access specifiers

put whole in one folder to compile, i failed to do so in dev c++ 4.9.9.2
gave me error

Quote:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.cpp" -o "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.exe" -ansi -pedantic -Wall -fexceptions -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x131): In function `main':

C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:10: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x13c):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:11: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x15f):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:18: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x17e):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:21: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x1b1):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:25: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x1e8):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:29: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x207):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:32: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x22d):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:36: undefined reference to `clockType::equalTime(clockType const&) const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x308):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:50: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x327):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:53: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x346):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:57: undefined reference to `clockType::incrementSeconds()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x375):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:61: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x3a9):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:66: undefined reference to `clockType::getTime(int&, int&, int&) const'
collect2: ld returned 1 exit status

Execution terminated

and it work fine if i put methods and main together in one file.

DaWei Jul 4th, 2007 8:44 AM

You can distribute code over multiple .cpp files. You just have to make sure that each of those files has access to the necessary declarations. That's what your .h file is for. You should show a brief example of your files, including the #include statements.

cairo Jul 4th, 2007 8:53 AM

member access specifiers

Quote:

//clockType.h, the specification file for the class clockType

class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The function checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec

void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.

void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one
// second.
// If the before-increment time is 23:59:59, the
// time is reset to 00:00:00.

void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one
// minute.
// If the before-increment time is 23:59:53,
// the time is reset to 00:00:53.

void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one
// hour.
// If the before-increment time is 23:45:53, the
// time is reset to 00:45:53.

bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is
// equal to otherClock; otherwise,
// returns false.

clockType(int hours, int minutes, int seconds);
//constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The constructor checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

clockType();
//default constructor with parameters
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};



methods

Quote:

//Implementation File for the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = 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();
}
}

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

bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}


tester
Quote:


//The user program that uses the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

int main()
{
clockType myClock;
clockType yourClock;

int hours;
int minutes;
int seconds;

//set the time of myClock
myClock.setTime(5, 4, 30); //Line 1

cout << "Line 2: myClock: "; //Line 2
myClock.printTime(); //print the time of myClock //Line 3
cout << endl; //Line 4

cout << "Line 5: yourClock: "; //Line 5
yourClock.printTime(); //print the time of yourClock Line 6
cout << endl; //Line 7

//set the time of yourClock
yourClock.setTime(5, 45, 16); //Line 8

cout << "Line 9: After setting, yourClock: "; //Line 9
yourClock.printTime(); //print the time of yourClock Line 10
cout << endl; //Line 11

//compare myClock and yourClock
if (myClock.equalTime(yourClock)) //Line 12
cout << "Line 13: Both times are equal."
<< endl; //Line 13
else //Line 14
cout << "Line 15: The two times are not equal."
<< endl; //Line 15

cout << "Line 16: Enter the hours, minutes, and "
<< "seconds: "; //Line 16
cin >> hours >> minutes >> seconds; //Line 17
cout << endl; //Line 18

//set the time of myClock using the value of the
//variables hours, minutes, and seconds
myClock.setTime(hours, minutes, seconds); //Line 19

cout << "Line 20: New myClock: "; //Line 20
myClock.printTime(); //print the time of myClock //Line 21
cout << endl; //Line 22

//increment the time of myClock by one second
myClock.incrementSeconds(); //Line 23

cout << "Line 24: After incrementing myClock by "
<< "one second, myClock: "; //Line 24
myClock.printTime(); //print the time of myClock //Line 25
cout << endl; //Line 26

//retrieve the hours, minutes, and seconds of the
//object myClock
myClock.getTime(hours, minutes, seconds); //Line 27

//output the value of hours, minutes, and seconds
cout << "Line 28: hours = " << hours
<< ", minutes = " << minutes
<< ", seconds = " << seconds << endl; //Line 28

system("pause");
return 0;
}//end main

example like this program, dev c++ cant work if i arrange the files like above

DaWei Jul 4th, 2007 9:06 AM

Hard to read that, since it's in quote tags and not code tags. You say in one place that the include file is named "type.hh", but you include "clockType.h". That's either a problem in your code or inaccurate posting.

cairo Jul 4th, 2007 11:49 AM

errr.. im sorry.. i re-post again, i must be out of my mind -_-!


clockType.h is the member access specifiers i state just now
:

class clockType
{
public:
    void setTime(int hours, int minutes, int seconds);
      //Function to set the time.
      //The time is set according to the parameters.
      //Postcondition: hr = hours; min = minutes;
      //              sec = seconds
      //  The function checks whether the values of
      //  hours, minutes, and seconds are valid. If a
      //  value is invalid, the default value 0 is
      //  assigned.

    void getTime(int& hours, int& minutes, int& seconds) const;
      //Function to return the time.
      //Postcondition: hours = hr; minutes = min;
      //              seconds = sec

    void printTime() const;
      //Function to print the time.
      //Postcondition: The time is printed in the form
      //              hh:mm:ss.

    void incrementSeconds();
      //Function to increment the time by one second.
      //Postcondition: The time is incremented by one
      //              second.
      //  If the before-increment time is 23:59:59, the
      //  time is reset to 00:00:00.

    void incrementMinutes();
      //Function to increment the time by one minute.
      //Postcondition: The time is incremented by one
      //              minute.
      //  If the before-increment time is 23:59:53,
      //  the time is reset to 00:00:53.

    void incrementHours();
      //Function to increment the time by one hour.
      //Postcondition: The time is incremented by one
      //              hour.
      //  If the before-increment time is 23:45:53, the
      //  time is reset to 00:45:53.

    bool equalTime(const clockType& otherClock) const;
      //Function to compare the two times.
      //Postcondition: Returns true if this time is
      //              equal to otherClock; otherwise,
      //              returns false.

    clockType(int hours, int minutes, int seconds);
      //constructor with parameters
      //The time is set according to the parameters.
      //Postcondition: hr = hours; min = minutes;
      //              sec = seconds
      //  The constructor checks whether the values of
      //  hours, minutes, and seconds are valid. If a
      //  value is invalid, the default value 0 is
      //  assigned.

    clockType();
      //default constructor with parameters
      //The time is set to 00:00:00.
      //Postcondition: hr = 0; min = 0; sec = 0

private:
    int hr;  //variable to store the hours
    int min; //variable to store the minutes
    int sec; //variable to store the seconds
};



clockTypeImp.cpp is the methods i mean just now
:

//Implementation File for the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
        if (0 <= hours && hours < 24)
            hr = hours;
        else
            hr = 0;

        if (0 <= minutes && minutes < 60)
            min = minutes;
        else
            min = 0;

        if (0 <= seconds && seconds < 60)
            sec = seconds;
        else
            sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
        hours = hr;
        minutes = min;
        seconds = 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();
        }
}

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

bool clockType::equalTime(const clockType& otherClock) const
{
        return (hr == otherClock.hr
                    && min == otherClock.min
                    && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
        if (0 <= hours && hours < 24)
                hr = hours;
        else
                hr = 0;

        if (0 <= minutes && minutes < 60)
                min = minutes;
        else
                min = 0;

        if (0 <= seconds && seconds < 60)
                sec = seconds;
        else
                sec = 0;
}

clockType::clockType()  //default constructor
{
        hr = 0;
        min = 0;
        sec = 0;
}



testClockClass.cpp is the main program
:


//The user program that uses the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

int main()
{
    clockType myClock;
    clockType yourClock; 

    int hours;
    int minutes;
    int seconds;

      //set the time of myClock
    myClock.setTime(5, 4, 30);                          //Line 1

    cout << "Line 2: myClock: ";                        //Line 2
    myClock.printTime();    //print the time of myClock //Line 3
    cout << endl;                                      //Line 4

    cout << "Line 5: yourClock: ";                      //Line 5
    yourClock.printTime(); //print the time of yourClock  Line 6
    cout << endl;                                      //Line 7

      //set the time of yourClock
    yourClock.setTime(5, 45, 16);                      //Line 8

    cout << "Line 9: After setting, yourClock: ";      //Line 9
    yourClock.printTime(); //print the time of yourClock  Line 10
    cout << endl;                                      //Line 11

      //compare myClock and yourClock
    if (myClock.equalTime(yourClock))                  //Line 12
        cout << "Line 13: Both times are equal."
            << endl;                                  //Line 13
    else                                                //Line 14
        cout << "Line 15: The two times are not equal."
            << endl;                                  //Line 15

    cout << "Line 16: Enter the hours, minutes, and "
        << "seconds: ";                                //Line 16
    cin >> hours >> minutes >> seconds;                //Line 17
    cout << endl;                                      //Line 18

      //set the time of myClock using the value of the
      //variables hours, minutes, and seconds
    myClock.setTime(hours, minutes, seconds);          //Line 19

    cout << "Line 20: New myClock: ";                  //Line 20
    myClock.printTime();    //print the time of myClock //Line 21
    cout << endl;                                      //Line 22

      //increment the time of myClock by one second
    myClock.incrementSeconds();                        //Line 23

    cout << "Line 24: After incrementing myClock by "
        << "one second, myClock: ";                    //Line 24
    myClock.printTime();    //print the time of myClock //Line 25
    cout << endl;                                      //Line 26

      //retrieve the hours, minutes, and seconds of the
      //object myClock
    myClock.getTime(hours, minutes, seconds);          //Line 27

      //output the value of hours, minutes, and seconds
    cout << "Line 28: hours = " << hours
        << ", minutes = " << minutes
        << ", seconds = " << seconds << endl;          //Line 28

system("pause");
    return 0;
}//end main


i take these three files to compile just now by using Dev c++, it didn't work and gave me error

Quote:


Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.cpp" -o "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.exe" -ansi -pedantic -Wall -fexceptions -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x131): In function `main':

C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:10: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x13c):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:11: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x15f):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:18: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x17e):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:21: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x1b1):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:25: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x1e8):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:29: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x207):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:32: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x22d):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:36: undefined reference to `clockType::equalTime(clockType const&) const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x308):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:50: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x327):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:53: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x346):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:57: undefined reference to `clockType::incrementSeconds()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x375):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:61: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x3a9):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:66: undefined reference to `clockType::getTime(int&, int&, int&) const'
collect2: ld returned 1 exit status

Execution terminated

i try again by putting the all the methods of this program which is clockTypeImp.cpp inside tester file which is testClockClass.cpp and its work

pegasus001 Jul 4th, 2007 1:47 PM

It would be better to include some #ifndef`s to the clockType.h to avoid multiple inlusions.

DaWei Jul 4th, 2007 3:54 PM

This code compiles and runs for me. Note the additions in red. Always test your input for success. Suppose your user enters, for time, "A". Hard to stash that in an integer, cin won't like you anymore. Also note that the input test is not exhaustive. Suppose that the user enters fewer than the number of expected values, or adds colons, or something. Your program is broken. But those additions are YOUR job.

Note that a grouchier respondent than I would probably want to beat you to death for forcing them to reformat because you didn't use code tags until after I had copied and pasted your code.

clockType.h
:

class clockType
{
    public:
    void setTime(int hours, int minutes, int seconds);
    void getTime(int& hours, int& minutes, int& seconds) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;
    clockType (int hours, int minutes, int seconds);
    clockType();
private:
    int hr;
    int min;
    int sec;
};

clockType.cpp
:

#include <iostream>
#include "clockType.h"

using std::cout;
using std::cin;
using std::endl;

void clockType::setTime (int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24) hr = hours;
    else hr = 0;

    if (0 <= minutes && minutes < 60) min = minutes;
    else min = 0;

    if (0 <= seconds && seconds < 60) sec = seconds;
    else sec = 0;
}

void clockType::getTime (int& hours, int& minutes, int& seconds) const
{
    hours = hr;
    minutes = min;
    seconds = 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();
    }
}

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

bool clockType::equalTime (const clockType& otherClock) const
{
    return (hr == otherClock.hr
        && min == otherClock.min
        && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24) hr = hours;
    else hr = 0;

    if (0 <= minutes && minutes < 60) min = minutes;
    else min = 0;

    if (0 <= seconds && seconds < 60) sec = seconds;
    else sec = 0;
}

clockType::clockType() //default constructor
{
    hr = 0;
    min = 0;
    sec = 0;
}

main.cpp
:

#include <iostream>
#include "clockType.h"
#include <string>

using std::cout;
using std::cin;
using std::cerr;
using std::endl;

int ohHell (std::string trouble)
{
    cerr << "Trouble in River City: " << trouble << endl;
    return 1;
}
int main ()
{
    clockType myClock;
    clockType yourClock;

    int hours;
    int minutes;
    int seconds;

    myClock.setTime (5, 4, 30);
    cout << "myClock: ";
    myClock.printTime ();
    cout << endl;

    cout << "yourClock: ";
    yourClock.printTime ();
    cout << endl;

    yourClock.setTime (5, 45, 16);

    cout << "After setting, yourClock: ";
    yourClock.printTime ();
    cout << endl;

    if (myClock.equalTime (yourClock)) cout << "Equal." << endl;
    else cout << "Not equal." << endl;

    cout << "Enter the hours, minutes, and seconds: ";
    cin >> hours >> minutes >> seconds;
    if (!cin.good ()) return ohHell ("input failed");
    cout << endl;

    myClock.setTime (hours, minutes, seconds);

    cout << "New myClock: ";
    myClock.printTime ();
    cout << endl;

    myClock.incrementSeconds ();

    cout << "After incrementing myClock by one second, myClock: ";
    myClock.printTime ();
    cout << endl;

    myClock.getTime (hours, minutes, seconds);

    cout << "Hours = " << hours
    << ", minutes = " << minutes
    << ", seconds = " << seconds << endl;

    return 0;
}


The Dark Jul 4th, 2007 6:40 PM

You need to compile each of your .cpp programs and then link them together. Currently you are just compiling the main .cpp without linking in the class .cpp.

It should be as simple as putting both .cpp file names at the start of the compile command line.

Ben.Dougall Jul 5th, 2007 10:14 AM

From what i notice, It doesn't read the clockTypeImp.cpp file ( you never tell it to look at it )

For seperate class files: one .h/.hpp and .cpp with the same name. Example Clock.hpp & Clock.cpp.

Notice how Dawei had the three files: clocktype.cpp clocktype.h main.cpp

I haven;t seen a header file go to a code file with a different name before.

Hope that helps... I suck at explaining things.

The Dark Jul 6th, 2007 12:28 AM

Ben, source file names don't matter at all when using C/C++. That is your Java background showing :)


All times are GMT -5. The time now is 2:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC