how to create own header files in cpp? and inside, header files how to create functions and objects and also use them?




Preview what we going to learn:
created header file



This is also the solution to this assignment you can download this file.

Here 3 files are created date.hpp, date.cpp, and main.cpp:


date.hpp

#include <string>
#include <ctime>
using namespace std;
 
#ifndef DATE_H // This definition  protect the header against
#define DATE_H // multiple inclusions.
 
class Date // date class
{
private:           // Data fields
    int day = 0;   //day
    int month = 0; //month
    int year = 0;  // year
 
public:
    // constructor
    Date();
    // constructor for seting value at declartion time in d/m/y form.
    Date(int dint mint y);
 
    // data seting mathod for Date to d/m/y.
    void DateSet(int dint mint y);
 
    int DateDay();   // accessing mathod for Day.
    int DateMonth(); // accessing mathod for Month.
    int DateYear();  // accessing mathod for Year.
 
    string ToString(); // returns string form of date.
    void DatePrint();  // prints Date.
};
 
Date DateNow(); // accessing todays date.
 
#endif

#ifndef DATE_H 

#define DATE_H 

// our code.

#endif 

  1. Using In this method we can protect the header against multiple inclusions.

  2. date.hpp file only contain declarations.


Declarations in Date class

class Date // date class
{
private:           // Data fields
    int day = 0;   //day
    int month = 0; //month
    int year = 0;  // year

public:
    // constructor
    Date();
    // constructor for seting value at declartion time in d/m/y form.
    Date(int dint mint y);

    // data seting mathod for Date to d/m/y.
    void DateSet(int dint mint y);

    int DateDay();   // accessing mathod for Day.
    int DateMonth(); // accessing mathod for Month.
    int DateYear();  // accessing mathod for Year.

    string ToString(); // returns string form of date.
    void DatePrint();  // prints Date.
};


We declare a function DateNow() out of class

Date DateNow(); // accessing todays date.


date.cpp

#include <iostream>
#include <string>
#include <ctime> // header for accessing current time.
#include "date.hpp"
using namespace std;
 
//All implimentation of "date.hpp" is here:
 
// function to convert date in to string form
string Date::ToString()
{
    string s = "";
    s += to_string(day);
    s += '/' + to_string(month);
    s += '/' + to_string(year);
    return s;
}
 
// Default constructor.
Date::Date()
{
    DateSet(000);
}
 
// constructor for setting value at declartion time.
Date::Date(int d, int m, int y)
{
    DateSet(d, m, y);
}
 
// method for setting value of date object.
void Date::DateSet(int dint mint y)
{
    day = d;
    month = m;
    year = y;
}
 
// mathod for access day.
int Date::DateDay()
{
    return day;
}
 
// mathod for access month.
int Date::DateMonth()
{
    return month;
}
 

// mathod for access year.
int Date::DateYear()
{
    return year;
}
 
// mathod for printing date.
void Date::DatePrint()
{
    cout << ToString() << endl;
}
 
// function for creating date object of today's date.
Date DateNow()
{
    // Default value.
    Date d(111970);
 
    time_t t = time(0L); // Stores the current time in t.
 
    if (t == (time_t)(-1))
    {
        return d;
    }
 
    tm *tp;             // In C this would have been struct tm
    tp = localtime(&t); // Turns t into a tm structure
 
    // setting date.
    // Perversely, months are 0-11
    // The tm structure uses 2-digit years
    d.DateSet(tp->tm_mdaytp->tm_mon + 1tp->tm_year + 1900);
 
    // return object
    return d;
}
 
 
  1. All setter and getter methods of class Date are implemented here.

  2. You can see comments on the code for details.

More articles on cpp

  1. All Sorting Algorithms Implementation In c++
  2. Prefix sums
  3. algos

main.cpp

#include <iostream>
#include <string>
#include "date.hpp"
#include "date.cpp"
#include "date.hpp"
using namespace std;
 
// driver code
int main()
{
    // testing constructor.
    Date d(2062021);// d object is created. 
 
    // testing setter method.
    Date v;
    v.DateSet(1962021);
 
    // testing of DatePrint();
    cout << "\nd:";
    d.DatePrint();
 
    cout << "v:";
    v.DatePrint();
 
    // testing getter mathod.
    cout << "\nValues of date v :" << endl;
    cout << "v.day  :" << v.DateDay() << endl;
    cout << "v.month:" << v.DateMonth() << endl;
    cout << "v.year :" << v.DateYear() << endl;
 
    // testing ToString() mathod.
    cout << "\nString form of date v :" << v.ToString() << endl;
 
    // testing DateNow() function which gives todays date.
    Date today = DateNow();
    cout << "\nTodays Date :" << today.ToString() << endl;
    return 0;
}

  1. Testing of all methods of an object, Date is done here.


Output

how to create own header files in cpp

all code Images:

data.hpp
data.hpp header


data.cpp

data.cpp implementation
main.cpp
main.cpp testing






Post a Comment

0 Comments