After just learning how to cascade multiple mutator function calls by defining those functions with return type "reference to an object of a class C" (i.e., C &setHeight( int ) ) in conjunction with corresponding 'return *this' statements, I would very much like to know if a similar method allows an object's data members to be returned from a single "get" function.
I have a simple "invoice" class, with an object from another class, "Date". This data member object itself contains private data that store the standard date for the "date of purchase" of a hypothetical product (for which the object was created). Although in "Date", three int members store the date components, I would like to somehow cascade calls to the three corresponding get functions, belonging to the Date object, in my "date of purchase" accessor, as some single accessor/get appears to be required, and because this style seems preferable to completing the task with a cout statement. In other words, if some get function must be used to display the full date, I'd rather it were displayed formatted, with a single call within a print statement in the client (function main), than by a function that merely prints a formatted statement when called from main.
//Invoice.h
#ifndef DATE_H
#define DATE_H
#include "Date.h"
...using namespace std. (Assume all other necessary libraries, classes, are included)
class Invoice {
public:
Invoice(); // Default constructor
Invoice( Date &, string );
setDateOfPurchase( int, int, int );
getDateOfPurchase();
private:
Date DateOfPurchase;
string description;
}; // end class definition
#endif
Member function definition below...
//Invoice.cpp
#include "Date.h"
(Again, please assume the necessary inclusions are made here)
Invoice::Invoice(){
DateOfPurchase.month = 1;
DateOfPurchase.day = 1;
DateOfPurchase.year = 1999;
} // end Default constructor definition
Invoice::Invoice( Date &purchaseDate, string desc ):
DateOfPurchase( purchaseDate ),
description( desc )
{} // No body
Invoice &Invoice::setDateOP( int m, int d, int y ) {
// The Date class definition is not explicitly defined; please assume these simply accept
// one int argument each.
DateOfPurchase.setMonth( m );
DateOfPurchase.setDay( d );
DateOfPurchase.setYear( y );
return *this;
} // End mutator definition
The header for my accessor function intended to retrieve the full date stored in object DateOfPurchase looks like the following:
Invoice &Invoice::getDateOrd(){
//gets month, day, and year, possibly through utilizing dereferenced 'this' pointer (*this)
} // end getDateOrd definition
function main below...
In main: // Creation of a Date object, which will be passed by reference to the invoice constructor. Date today( 11, 10, 2014 );
Invoice firstSale( today, A corkscrew: product# 88090 );
// Cascading call to each member function of *member data object* DateOfPurchase
firstSale.setMonth( 11 ).setDay( 12 ).setYear( 2014 );
Hopefully, this demonstrates a basic application of the cascading call with "set" functions. Now for "get" functions... (By the way, I'm using Deitel & Deitel, 2013, 7th edition)
Use << operator overloading in the Invoice class
add the below code into your Invoice class.
You can directly call as
Hope it will solve your problem.