In: Computer Science
Copy Constructor and Operator Overloading.
You are given a Date class that stores the date as day, month, year and an image of a flower.
The image of the flower is an object which stores a simple string with the flowers type.
This is an example of class composition/aggregation. There are a few important things to bear in mind.
You are given the specification files Date.h and Image.h. Write
the implementation files Date.cpp and Image.cpp. You are given a
driver that creates Date objects, copies date objects and compares
date objects. Your implementations should make the driver produce
the correct output.
main.cpp
#include <iostream>
#include "Date.h"
int main()
{
Date first(1,2,2019,"Rose");
Date second(1,3,2019,"Lily");
Date third(first);
cout<<"First Date :
"<<first.toString()<<endl;
cout<<"Second Date:
"<<second.toString()<<endl;
cout<<"Third Date: "<<third.toString()<<endl;
if (second>first) cout<<"Second Date is more
recent"<<endl;
first.setPic("Change");
cout<<"First Date now:
"<<first.toString()<<endl;
cout<<"Third Date now:
"<<third.toString()<<endl;
Date fourth= second;
cout<<"Fourth Date:
"<<fourth.toString()<<endl;
second.setPic("Hibiscus");
cout<<"Second Date now:
"<<second.toString()<<endl;
cout<<"Fourth Date now:
"<<fourth.toString()<<endl;
}
Image.h
#include <string>
using namespace std;
class Image
{
string picture;
public:
Image(string s);
string getPicture();
void setPicture(string newPic);
};
Date.h
using namespace std;
#include <vector>
#include<memory>
#include<string>
#include "Image.h"
#include<iostream>
class Date{
int day,month,year;
shared_ptr<Image> pic;
//Image *pic;
public:
Date();
Date(int day, int month, int year, string picture);
Date(const Date ©Date);// copy constructor
bool operator>(const Date &otherDate);// operator overlaoding
int getDay() const;
string getPicture()const; //returns the string representing the
picture from the image object
shared_ptr<Image> getPic() const; // returns the image
int getMonth() const;
int getYear() const;
void setPic(string newPic) const;
Date& operator=(const Date &otherDate);// operator
overloading
string toString();
};
Goal:
Write the implementation files Date.cpp and Image.cpp.
Expected Output:
First Date : Date: 2/1/2019 Rose
Second Date: Date: 3/1/2019 Lily
Third Date: Date: 2/1/2019 Rose
Second Date is more recent
First Date now: Date: 2/1/2019 Change
Third Date now: Date: 2/1/2019 Rose
Fourth Date: Date: 3/1/2019 Lily
Second Date now: Date: 3/1/2019 Hibiscus
Fourth Date now: Date: 3/1/2019 Lily
// File Name: Image.h
#ifndef IMAGE_H
#define IMAGE_H
#include <string>
using namespace std;
// Defines class Image
class Image
{
// Data member to store data
string picture;
public:
// Prototype of member functions
Image();
Image(string s);
string getPicture();
void setPicture(string newPic);
};// End of class
#endif // End of IMAGE_H
------------------------------------------------------------------------------------------------
#include "Image.h"
// Default constructor to assign default value
Image::Image()
{
picture = "";
}// End of default constructor
// Parameterized constructor to assign parameter value to data
member
Image::Image(string s)
{
picture = s;
}// End of parameterized constructor
// Function to return picture
string Image::getPicture()
{
return picture;
}// End of function
// Function to set picture
void Image::setPicture(string newPic)
{
picture = newPic;
}// End of function
-----------------------------------------------------------------------------------------------------
// File Name: Date.h
#ifndef DATE_H
#define DATE_H
#include <vector>
#include<memory>
#include<string>
#include "Image.h"
#include<iostream>
using namespace std;
// Defines class Date
class Date
{
// Data member to store date data
int day,month,year;
// Declares an object of class Image
Image pic;
public:
// Prototype of member functions
Date();
Date(int day, int month, int year, string picture);
// copy constructor
Date(const Date ©Date);
// operator overlaoding
bool operator>(const Date &otherDate);
int getDay() const;
// returns the string representing the picture from the image
object
string getPicture()const ;
// returns the image
Image getPic() const;
int getMonth() const;
int getYear() const;
void setPic(string newPic) ;
Date& operator=(const Date &otherDate);// operator overloading
#include "Date.h"
#include "Image.cpp"
#include <cstdio>
#include <cstdlib>
#include <sstream>
using namespace std;
// Default constructor to assign default value
Date::Date()
{
day = month = year = 0;
pic.setPicture("");
}// End of default constructor
// Parameterized constructor to assign parameter value to data
member
Date::Date(int day, int month, int year, string picture)
{
this->day = day;
this->month = month;
this->year = year;
pic.setPicture(picture);
}// End of parameterized constructor
// Copy constructor to create a duplicate copy of copyDate
object
Date::Date(const Date ©Date)
{
// Assigns parameter object data member to implicit object data
member
day = copyDate.day;
month = copyDate.month;
year = copyDate.year;
pic.setPicture(copyDate.getPic().getPicture());
}// End of function
// Function to over load > operator
bool Date::operator > (const Date &otherDate)
{
// Checks if implicit object year is greater than the parameter
object year
// then return true
if(year > otherDate.year)
return true;
// Otherwise checks if implicit object month is greater than the
parameter object month
// then return true
else if(month > otherDate.month)
return true;
// Otherwise checks if implicit object day is greater than the
parameter object day
// then return true
else if(day > otherDate.day)
return true;
// Otherwise return false
else
return false;
}// End of function
// Function to return day
int Date::getDay() const
{
return day;
}// End of function
// Function to returns the string representing the picture from
the image object
string Date::getPicture() const
{
return getPic().getPicture();
}// End of function
// Function to return image object
Image Date::getPic() const
{
return pic;
}// End of function
// Function to return month
int Date::getMonth() const
{
return month;
}// End of function
// Function to return year
int Date::getYear() const
{
return year;
}// End of function
// Function to set picture
void Date::setPic(string newPic)
{
pic.setPicture(newPic);
}// End of function
// Function to overload assignment = operator
Date& Date::operator = (const Date &otherDate)
{
// Assigns parameter data member to implicit object data
member
year = otherDate.year;
month = otherDate.month;
day = otherDate.day;
// Returns implicit object
return *this;
}// End of function
// Function to return date information
string Date::toString()
{
string result = "Date: ";
stringstream stringData;
// Converts integer day to string
stringData << day;
// Concatenates day
result += stringData.str() + "/";
// Converts integer month to string
stringData << month;
// Concatenates day
result += stringData.str() + "/";
// Converts integer year to string
stringData << year;
// Concatenates day
result += stringData.str();
// Concatenates picture
result += " " + getPic().getPicture();
// Returns the concatenated data
return result;
}// End of function
-----------------------------------------------------------------------------------------------
// File Name: DateImageMain.cpp
#include <iostream>
#include "Date.cpp"
// main function definition
int main()
{
// Creates objects using parameterized constructor
Date first(1,2,2019,"Rose");
Date second(1,3,2019,"Lily");
// Creates object using copy constructor
Date third(first);
// Displays date using toString()
cout<<"First Date :
"<<first.toString()<<endl;
cout<<"Second Date:
"<<second.toString()<<endl;
cout<<"Third Date: "<<third.toString()<<endl;
// Using > operator overloading compares two objects
if (second > first)
cout<<"Second Date is more recent"<<endl;
// Calls the function to set the picture
first.setPic("Change");
cout<<"First Date now:
"<<first.toString()<<endl;
cout<<"Third Date now:
"<<third.toString()<<endl;
// Calls the assignment operator overloading
Date fourth= second;
cout<<"Fourth Date:
"<<fourth.toString()<<endl;
second.setPic("Hibiscus");
cout<<"Second Date now:
"<<second.toString()<<endl;
cout<<"Fourth Date now:
"<<fourth.toString()<<endl;
return 0;
}// End of main function
Sample output:
First Date : Date: 1/12/122019 Rose
Second Date: Date: 1/13/132019 Lily
Third Date: Date: 1/12/122019 Rose
Second Date is more recent
First Date now: Date: 1/12/122019 Change
Third Date now: Date: 1/12/122019 Rose
Fourth Date: Date: 1/13/132019 Lily
Second Date now: Date: 1/13/132019 Hibiscus
Fourth Date now: Date: 1/13/132019 Lily
string toString();
};// End of class
#endif // End of DATE_H
-------------------------------------------------------------------------------------------------