Question

In: Computer Science

Copy Constructor and Operator Overloading. You are given a Date class that stores the date as...

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.

  1. Need for copy constructor
  2. Ability to do operator overloading
  3. Need to release dynamic memory – using destructor

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 &copyDate);// 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

Solutions

Expert Solution

// 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 &copyDate);
// 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 &copyDate)
{
// 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

-------------------------------------------------------------------------------------------------


Related Solutions

White the class Trace with a copy constructor and an assignment operator, printing a short message...
White the class Trace with a copy constructor and an assignment operator, printing a short message in each. Use this class to demonstrate the difference between initialization Trace t("abc"); Trace u = t; and assignment. Trace t("abc"); Trace u("xyz"); u = t; the fact that all constructed objects are automatically destroyed. the fact that the copy constructor is invoked if an object is passed by value to a function. the fact that the copy constructor is not invoked when a...
c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that use these symbols but have a parameter list that includes a class ____. object address reference data type Flag this Question Question 210 pts When overloading the insertion operator to process a Complex object, it’s important to understand that you’re overloading an operator in the ____ class. istream operator Complex ostream Flag this Question Question 310 pts ____ class variables are allocated memory locations...
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as...
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as a member of the class clockType. Write the definition of the function to overload the post-increment operator for the class clockType as defined in the step above. Main.cpp //Program that uses the class clockType. #include <iostream> #include "newClock.h" using namespace std; int main() { clockType myClock(5, 6, 23); clockType yourClock; cout << "Line 3: myClock = " << myClock << endl; cout << "Line...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo...
Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo with a main( ) method that creates 3 tasks and writes them to a binary file named "TaskList.dat". We will then add Java code that reads a binary file (of Task objects) into our program and displays each object to the console. More details to be provided in class. Here is a sample transaction with the user (first time the code is run): Previously...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT