Hi @mehmood tekfirst ,
I modified the code you uploaded, and now the code works as expected, please read it carefully. And I added an example of writing data to a file in the main function, you could refer to this method.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ostream>
#include <fstream>
constexpr size_t MAX_STR{ 50 };
class Name {
char* fname{};
char* Lname{};
public:
Name() : fname(new char[MAX_STR]), Lname(new char[MAX_STR]) {}
Name(const char* fN, const char* IN) : Name() {
setfn(fN);
setIn(IN);
}
~Name() {
delete[] fname;
delete[] Lname;
}
Name(const Name& obj) : Name(obj.fname, obj.Lname) {}
Name& operator=(const Name& rhs) {
setfn(rhs.fname);
setfn(rhs.Lname);
}
void setfn(const char* fN) {
std::snprintf(fname, MAX_STR, "%s", fN);
}
void setIn(const char* IN) {
std::snprintf(Lname, MAX_STR, "%s", IN);
}
char* getfn() const {
return fname;
}
char* getIn() const {
return Lname;
}
friend std::ostream& operator<<(std::ostream& out, const Name& n);
};
std::ostream& operator<<(std::ostream& out, const Name& n) {
return out << n.fname << ' ' << n.Lname;
}
class Date {
unsigned day{}, month{}, year{};
public:
Date() {}
Date(unsigned d, unsigned m, unsigned y) : year(y) {
if (m > 0 && m < 13) {
month = m;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
day = (d > 0 && d < 32) ? d : 0;
else if (m == 2)
day = (d > 0 && d < 29) ? d : 0;
else
day = (d > 0 && d < 31) ? d : 0;
}
else
month = 0;
}
bool setd(unsigned d) {
if (d > 0 && d < 32) {
day = d;
return true;
}
return false;
}
bool setm(unsigned m) {
if (m > 0 && m < 13) {
month = m;
return true;
}
return false;
}
void sety(unsigned y) {
year = y;
}
unsigned getd() const {
return day;
}
unsigned getm() const {
return month;
}
unsigned gety() const {
return year;
}
friend std::ostream& operator<<(std::ostream& out, const Date& d);
};
std::ostream& operator<<(std::ostream& out, const Date& d) {
return out << d.day << '/' << d.month << '/' << d.year;
}
class mTime {
unsigned hour{}, min{}, sec{};
public:
mTime() {}
mTime(unsigned h, unsigned m, unsigned s) : hour(h), min(m), sec(s) {}
bool seth(unsigned h) {
if (h >= 0 && h < 24) {
hour = h;
return true;
}
return false;
}
bool setm(unsigned m) {
if (m >= 0 && m < 61) {
min = m;
return true;
}
return false;
}
bool sets(unsigned s) {
if (s >= 0 && s < 61) {
sec = s;
return true;
}
return false;
}
unsigned geth() const {
return hour;
}
unsigned getm() const {
return min;
}
unsigned gets() const {
return sec;
}
friend std::ostream& operator<<(std::ostream& out, const mTime& m1);
};
std::ostream& operator<<(std::ostream& out, const mTime& m1) {
return out << m1.hour << ':' << m1.min << ':' << m1.sec;
}
constexpr size_t MAX_STR_SRC{ 150 };
class Service :public Name{
char* source{}, * destination{};
float distance{};
Date bookingDate;
mTime bookingtime;
bool status{};
float fuelrate{};
int cID{}, dID{}, vID{};
public:
Service(const char* src, const char* dest,
float dist, Date bookDte, mTime bookTime, bool lstatus, float lfuelRate,
int lcid, int ldid, int lvid)
:source(new char[MAX_STR_SRC]), destination(new char[MAX_STR_SRC]), distance(dist),
bookingDate(bookDte), bookingtime(bookTime), status(lstatus), fuelrate(lfuelRate),
cID(lcid), dID(ldid), vID(lvid) {
setSrc(src);
setDest(dest);
}
/* Service(const char *src, const char *dest,
float dist, Date bookDte, mTime bookTime, bool lstatus, float lfuelRate,
int lcid, int ldid, int lvid) :
Service(dist, bookDte, bookTime, lstatus, lfuelRate,
lcid, ldid, lvid) {
setfn(src);
setIn(dest);
} */
~Service() {
delete[] source;
delete[] destination;
}
Service(const Service& obj) : source(obj.source), destination(obj.destination){};
Service& operator=(const Service& rhs) {
setfn(rhs.source);
setfn(rhs.destination);
}
void setSrc(const char* src) {
std::snprintf(source, MAX_STR_SRC, "%s", src);
}
void setDest(const char* dest) {
std::snprintf(destination, MAX_STR_SRC, "%s", dest);
}
char* getSrc() const {
return source;
}
char* getDest() const {
return destination;
}
};
class Person {
protected:
Name pName;
Date DOB;
unsigned age{}, Nid{};
public:
Person() {}
Person(Name n, Date d, unsigned a, unsigned id) : pName(n), DOB(d), age(a), Nid(id) {}
Person(const Person& obj) : Person(obj.pName, obj.DOB, obj.age, obj.Nid) {}
void setN(const Name& n) {
pName = n;
}
void setdob(const Date& d) {
DOB = d;
}
void setage(unsigned a) {
age = a;
}
void setid(unsigned id) {
Nid = id;
}
unsigned getage() const {
return age;
}
unsigned getid() const {
return Nid;
}
Name getn() const {
return pName;
}
Date getdob() const {
return DOB;
}
friend std::ostream& operator<<(std::ostream out, const Person& p1);
};
std::ostream& operator<<(std::ostream out, const Person& p1) {
return out << "Name of Person is " << p1.pName << "\nDate of birth is " << p1.DOB << "\nAge is: " << p1.age << "\nID is " << p1.Nid;
}
class Customer : public Person {
int cId{};
Service** bookingHistory;
public:
using Person::Person;
Customer() {}
Customer(const Name& p, const Date& dob, unsigned a, unsigned id, unsigned cid, Service** bH) : Person(p, dob, a, id), cId(cid), bookingHistory(bH) {}
~Customer() { }
Customer(const Customer& b1) : Person(b1.pName, b1.DOB, b1.age, b1.Nid), cId(b1.cId), bookingHistory(b1.bookingHistory) {}
void setcusid(unsigned a) {
cId = a;
}
void setbookinghis(Service** bh) {
bookingHistory = bh;
}
unsigned getcusid() const {
return cId;
}
Service** getbookinghis() const {
return bookingHistory;
}
friend std::ostream& operator <<(std::ostream& out, const Customer&);
};
std::ostream& operator <<(std::ostream& out, const Customer& b1) {
return out << "Customer ID is: " << b1.cId;
}
class Driver :public Person
{
int dId, nooflicences, experience, status;
float overallRanking, salary;
char** Licenselist;
Service** serviceHistory;
public:
using Person::Person;
Driver()
{
dId = 0; nooflicences = 0; experience = 0; status = 0;
overallRanking = 0.0; salary = 0.0;
Licenselist = 0;
serviceHistory = 0;
}
Driver(const Name& p, const Date& dob, unsigned a, unsigned id, unsigned d, unsigned n, unsigned ex, unsigned s, float r, float sal, char** A, Service** sh) :Person(p, dob, a, id)
{
dId = d; nooflicences = n; experience = ex; status = s;
overallRanking = r; salary = sal;
Licenselist = A;
serviceHistory = sh;
}
~Driver() //delete servicehis
{
for (int i = 0; i < nooflicences; i++)
{
delete[]Licenselist;
}
}
void seti(unsigned a, unsigned b, unsigned c, unsigned d)
{
dId = a;
nooflicences = b;
experience = c;
status = d;
}
void setfl(float r, float s)
{
overallRanking = r;
salary = s;
}
void setpo(char** l, Service** sh)
{
Licenselist = l;
serviceHistory = sh;
}
int getdid()
{
return dId;
}
int getnumliscense()
{
return nooflicences;
}
int getexp()
{
return experience;
}
int getstat()
{
return status;
}
float getrank()
{
return overallRanking;
}
float getsal()
{
return salary;
}
char** getlislist()
{
return Licenselist;
}
Service** getservhis()
{
return serviceHistory;
}
Driver(const Driver& d1)
{
dId = d1.dId; nooflicences = d1.nooflicences; experience = d1.experience; status = d1.status;
overallRanking = d1.overallRanking; salary = d1.salary;
Licenselist = d1.Licenselist;
serviceHistory = d1.serviceHistory;
}
friend std::ostream& operator <<(std::ostream& out, const Driver&);
};
std::ostream& operator <<(std::ostream& out, const Driver& d1) //printing service history??
{
std::cout << "The id of driver is " << d1.dId << std::endl << "The number of licenses are: " << d1.nooflicences << std::endl;
std::cout << "The experience of driver is " << d1.experience << std::endl << "The status of driver is " << d1.status << std::endl;
std::cout << "The Overall Ranking of driver is " << d1.overallRanking << std::endl << "The Salaryof driver is: " << d1.salary << std::endl;
std::cout << "List of licenses: " << std::endl;
for (int i = 0; i < d1.nooflicences; i++)
{
for (int j = 0; d1.Licenselist[i][j] != '\0'; j++)
{
std::cout << d1.Licenselist[i][j];
}
std::cout << std::endl;
}
}
class feature
{
int fId;
char* description;
float cost;
public:
feature()
{
fId = 0;
cost = 0;
description = new char;
}
feature(int f, char* A, float c)
{
fId = f;
description = new char;
description = A;
cost = c;
}
void setfeature(int id, char* d, float c)
{
fId = id;
description = new char;
description = d;
cost = c;
}
int getfeatid()
{
return fId;
}
char* getdescrip()
{
return description;
}
float getcost()
{
return cost;
}
};
class Vehicle
{
int vId, registrationNo;
float avgMileage, overallRanking;
char* Licensetype, * fueltype;
bool status;
Date manufacturingDate;
feature* list;
Service** serviceHistory;
public:
};
int main() {
Service serObj("source 1", "destination 1", 2.5, Date(27, 05, 2008), mTime(14, 25, 33), true, 125, 2, 2, 2);
Service* serObj1 = &serObj;
Service** serObj2 = &serObj1;
//Service ** serHis = Service
Customer cust(Name("Ali", "Baabaa"), Date(23, 05, 2001), 1, 1, 1, serObj2);
//Person *aptr = &cust;
std::ofstream myfile;
myfile.open("C:\\Users\\Admin\\Desktop\\out.txt");
myfile << cust;
std::cout << cust.getcusid() << '\n' << cust.getn() << '\n' << cust.getdob() << '\n';
std::cout << cust.getbookinghis() << '\n';
}
The result is as follows:
I think bookHistory and serviceHistory are used to store historical data, and both are Service**
, about **
you could refer to this link.
Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.