c++
1. Although our Date class has the advance_day( ) function (default is 1 day), programmers new to the project want to be able to just ++ a Date object instead of calling the method. Overload operator ++ to increment the day of a Date object by one. Show both pre- and post-fix forms.
open answer
2. Rather than use traditional accessors/mutators, you decide to use operator [ ] for your 2D Point class. Don't forget your error checking! (Hint: How many overloads are there again?)
open answer
3.
Given the following enumeration:
enum Fruit { apple, orange, grapes };
Overload operator << to print one of its values to an output stream.
open answer
(1)
#include<iostream>
using namespace std;
class Date{
int month;
int day;
int year;
public:
//pre increament
Date operator ++(){
int
days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year% 4 ==0 && year%100
!= 0 || year%400 == 0)days_in_month[2] = 29;
else days_in_month[2] = 28;
day++;
if(day>days_in_month[month]){
if(month==12)year+=1;
day=1;
month%=12;
month+=1;
}
return *this;
}
//Post increment
Date operator ++(int){
int
days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
Date date;
if(year% 4 ==0 && year%100
!= 0 || year%400 == 0)days_in_month[2] = 29;
else days_in_month[2] = 28;
date.day = day;
date.month = month;
date.year = year;
day++;
if(day>days_in_month[month]){
if(month==12)year+=1;
day=1;
month%=12;
month+=1;
}
return date;
}
};
(3)
enum Fruit { apple, orange, grapes };
ostream & operator << (ostream& out , const enum
Fruit fruit){
if(fruit == apple){
out<<"apple";
}
else if(fruit == orange){
out<<"orange";
}
else if(fruit == grapes){
out<<"grapes";
}
return out;
}
c++ 1. Although our Date class has the advance_day( ) function (default is 1 day), programmers...
This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...
Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...