D
D
dmitriyprog2019-03-10 23:05:06
C++ / C#
dmitriyprog, 2019-03-10 23:05:06

How to compile 3 files in c++?

Linking problem
date.hpp file

#include <string>
#ifndef __CCLASS_H__
#define __CCLASS_H__
using namespace std;

enum dayOfWeek {MON, TUE, WEN, THU, FRI, SAT, SUN};


/**
* The class  describes the date represents as day,month and year
* Each Date can be additioned or subsructed with a integer number
*
*/
class Date
{
    private:
        int year;
        int month;
        int day;

   public:

      // FRIEND_TEST(Date, getMonth);
       Date(int day, int month, int year);

       static const int MaxDayInMonth[13];
       /**
       * Checks if the set year leap is
         return true if thee year leap is.
       */
       inline bool Leap(int year);
       /**
       * Gets year value;
       */
       int getYear() /*override */;
        /**
        * Gets month as a number.
        *
        * return the month as a nuber.
        */
       int getMonth() /*override */;
       /**
        Gets day as a number
       */
       int getDay()  /*override */;
       /**
       * Adds the number to current date;
       *
       * return new date
       */
       Date operator + (int k);
       /**
       * Substracts the number from current date;
       *
       * return new date
       */
       Date operator - (int k);
       /**
       * Represents the date as a string
       *
       * return string in format yyyy-mm-dd
       */
       string toString() /*override */;
       /**
        * Prints the date on console.
       */

       void printDate() /*override */;

       dayOfWeek day_of_week();

};

#endif

date.cpp file
#include <iostream>
#include <string>
#include <sstream>
#include "date.h"
#include <gtest/gtest.h>

using namespace std;

const int Date::MaxDayInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};


Date::Date(int day, int month, int year)
{
 try
    {
     if (month < 1 || month >12)
         throw 'm';
     if (year < 1970 || year > 2019)
         throw 'y';
     this->day = day;
     this->month = month;
     this->year = year;
    }
    catch(char c)
    {
      if (c == 'm')
        cout << "The month is wrong. Try again, please." <<endl;
       if (c == 'y')
        cout << "The year is wrong. Try again, please." <<endl;
    }

}
inline bool Date::Leap(int year)
{
    return year % 4 ==  0 && (year % 100 != 0 || year % 400 == 0);
}
 int Date::getYear() /*override*/
{
    return year;
}
int Date::getMonth() /*override*/
{
    return month;
}

int Date::getDay() /*override*/
{
    return day;
}

Date Date::operator + (int k)
{
    int maxDayInMonth = MaxDayInMonth[month];
    int newDays = day +  k;
    if (month == 2)
    {
        maxDayInMonth = (Leap(year) ? maxDayInMonth + 1 : maxDayInMonth);
    }
    if ((day +  k) >  maxDayInMonth)
    {
        newDays = (day +  k ) % maxDayInMonth ;
        month ++;
        if (month > 12)
        {
          month = 1;
          year++;
        }
    }

    return Date(newDays,this->month,this->year);
}

Date Date::operator - (int k)
{
    int newDays = (this->day - k);
    if (newDays < 1)
    {
      month--;
      k = k - day;
      month = (month < 1 ? 12 : month);
      if (month == 12)
      {
        year--;
      }
      int maxDayInMonth = MaxDayInMonth[month];
      if (month == 2)
      {
        maxDayInMonth = (Leap(year) ? maxDayInMonth + 1 : maxDayInMonth);
      }
      day = maxDayInMonth - k;
    }
    return Date(this->day,this->month,this->year);
}

string Date::toString() /*override*/
{
  std::stringstream output;
  output << year << "-" << month << "-" << day;
  return output.str();
}

void Date::printDate() /*override*/
{
  cout << this->toString() << endl;
}



TEST(DateTest, firstTest)
{
    Date d(12,12,2005);
    ASSERT_EQ(12, d.getMonth());
  //  ASSERT_EQ("2005-12-12", d.toString());
}



TEST(DateTest, stringTest)
{
    Date d(12,12,2005);
    ASSERT_EQ("2005-12-12", d.toString());
    Date d1 = d + 5;
    ASSERT_EQ("2005-12-17", d1.toString());
    Date d2 = d - 1;
    ASSERT_EQ("2005-12-11", d2.toString());
    Date d

}

Файл main.cpp
<code lang="cpp">
int main(int, char **)
{
    Date date(1,12,2017);
    Date date2(26,1,2005);
    cout << date.getMonth() << endl;
    Date d = date - 6;
    d.printDate();
    return 0;
}


</code>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Melnikov, 2019-03-11
@BacCM

Use any project builder. If Windows, then the project in VS
If Linux, then the easiest way is probably to put Qt Creator.
You can simply list what you compile .cpp files in.
Although the most correct thing is to study CMake or something else like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question