In: Computer Science
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
Date.h:
#ifndef DATE_H
#define DATE_H
class Date{
public:
Date(int m,int d,int y);
void display();
private:
int month;
int day;
int year;
};
#endif
Date.cpp:
#include "Date.h"
#include <iostream>
using namespace std;
Date :: Date(int m,int d, int y):month(m),day(d),year(y){
}
void Date :: display(){
cout << month << '/' << day << '/' <<
year << endl;
}
Time.h:
#ifndef TIME_H
#define TIME_H
class Time{
public:
Time(int h,int m);
void display();
private:
int hour;
int minute;
};
#endif
Time.cpp:
#include "Time.h"
#include <iostream>
#include <iomanip>
using namespace std;
Time :: Time(int h,int m):hour(h),minute(m){
}
void Time :: display(){
cout << hour << ':' << setw(2) <<
setfill('0') << minute << endl;
setfill(' ');
}
Report.h:
#ifndef REPORT_H
#define REPORT_H
#include "Date.h"
#include "Time.h"
#include <iostream>
#include <string>
using namespace std;
// Report class
class Report{
public:
// six argument constructor
Report(int month,int day,int year,int hour,int minute,string
description);
// display the report
void display();
private:
// attributes of Report
string rep_desc;
Date rep_date;
Time rep_time;
};
#endif
Report.cpp:
#include "Report.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// six constructor argument
Report :: Report(int month,int day,int year,int hour,int
minute,string description):
rep_date(month,day,year),rep_time(hour,
minute),rep_desc(description){
}
// display method of report
void Report :: display(){
cout << "Report date: ";
// calling the date display method
rep_date.display();
cout << "Report time: ";
// calling the time display method
rep_time.display();
// display the description of Report
cout << "Report desc: " << rep_desc <<
endl;
}
main.cpp:
#include "Date.h"
#include "Time.h"
#include "Report.h"
#include <iostream>
using namespace std;
int main()
{
Report r(7,4,1776,10,15,"Test Data");
r.display();
return 0;
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
****************************************************************************************