Program help

PART I
Put all the code in a .doc file and include a UML diagram for the Airplane class. You can use Microsoft Visio or another drawing tool. The code must conform to the Google C++ style sheet for order and indentation etc. http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Class_Format

1. /*Write a class for an airplane class with constructors, interface (mutators and accessors) and a test driver (main)which should test everything.It should be able to change altitude (up,down) and speed. The constructor will set the altitude to 0 and speed to zero and longitude and latitude to Boston, Massachusetts. The overloaded constructor will set longitude and latitude to some numbers. If the altutude ever falls below 0 the change altutude function should should announce the plane had crashed and the program exit. */

2. /* Rewrite DayOfYear (see attached document) to use constructor to set day and month as well as a default constructor ( set to 0,0). The day and month member variables are now private and the set and display functions and constructors are public. Add a private check_date member function that gets called in the public set function and overloaded consturctor. */
//DISPLAY 10.3 Class with a Member Function//Program to demonstrate a very simple example of a class.
//A better version of the class DayOfYear will be given in Display 10.4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>using namespace std;
class DayOfYear
{public: void output( ); 
int month; 
int day;};
int main( )
{ DayOfYear today, birthday;
cout << "Enter today's date:\n"; 
cout << "Enter month as a number: "; 
cin >> today.month; 
cout << "Enter the day of the month: "; 
cin >> today.day; 
cout << "Enter your birthday:\n"; 
cout << "Enter month as a number: "; 
cin >> birthday.month; 
cout << "Enter the day of the month: "; 
cin >> birthday.day;
cout << "Today's date is "; 
today.output( ); 
cout << "Your birthday is "; 
birthday.output( );
if (today.month == birthday.month && today.day == birthday.day) 
cout << "Happy Birthday!\n"; 
else 
cout << "Happy Unbirthday!\n";
return 0;}
//Uses iostream:
void DayOfYear::output( )
{ cout << "month = " << month << ", day = " << day << endl;}


PART II
Use code from DayOfYear() from above to implement the friend functions below:
Add these functions as "friends"

bool equal(DayOfYear& date1, DayOfYear& date2);
bool lessThan(DayOfYear& date1, DayOfYear& date2) // date1 less than date 2 etc.
bool greaterThan(DayOfYear& date1, DayOfYear& date2)
bool greaterThan_or_Equal(DayOfYear& date1, DayOfYear& date2)
bool lessThan_or_Equal(DayOfYear& date1, DayOfYear& date2)

Write the main() drive to completely test these.

PART III
Write a better DayOfYear class.
Add a year variable.
Rewrite check_date so that it really works, even for leap year.
In other words. October can have 31 days but November only 30.
You can flag leap year by testing the year input as follows: A year divisible with no remainder by 400 is a leap year. A year divisable by 4 with no remainder and NOT divisible by 100 ( again no remainder) is also a leap year. As you can see, 2012 ( divide by 4 and no 100 and no remainder) is a leap year. 2013 is not. The next leap year ( February has 29 days rather than the usual 28) is 2016. If the year entered is a leap year it can have 29 days. Otherwise only 28. Check a calendar for other lengths.

The default DayOfYear constructor sets the date to 1/1/1970.
There is an overloaded constructor that takes input for year, month and day.
Besides writing the above, write an overloaded << operator that outputs: 11/2/2012 ( for example).
Write an overloaded == operator as well.
Write the public accessors and mutators for year, month and day.
Write a driver to test the overloaded operators (etc.) by creating several DayOfYear objects using both constructors and the mutators ( for the object created by default destructor), outputting their values with cout as well as with the accessors, and testing == for objects with same date and objects with different dates with some meaningful output for the test.
Last edited on
This is the third time you've posted the same thing. Spamming the forum isn't going to make people want to help you.

When you're posting code, please use the '<>' button to the right of the edit window to enclose it in code tags, so that it's more easily readable.

Please show us what you've done so far. We're not going to do your homework for you, but we will help you problems you have with your code.
sorry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
include "stdafx.h"
#include <iostream>
using namespace std;
class DayOfYear
{
public:
void input();
void output();
void set(int new_month, int new_day);
int get_month();
int get_day();

private:
void check_date();
int month; 
int day;
};
int main()
{
DayOfYear today, birthday;
cout << "Enter today's date: \n";
today.input();
cout << "Today's date is ";
today.output();
cout << "Enter your birthday: \n";
birthday.input();
cout << "Your birthday is ";
birthday.output();
if( today.get_month() == birthday.get_month() && today.get_day() == birthday.get_day())
cout << "Happy Birthday! \n";
else
cout <<"Happy Unbirthday! \n";
return 0;


}
void DayOfYear::input()
{
cout << "Enter the month as a number : ";
cin >> month;
cout << "Enter the day of the month: ";
cin >> day;
check_date();

}
void DayOfYear::output( )
{ cout << "month = " << month << ", day = " << day << endl;

}
void DayOfYear::set(int new_month, int new_day)
{
month= new_month;
day = new_day;
check_date();
}
void DayOfYear::check_date()
{
if ((month< 1) || (month > 12) ||(day < 1) || (day> 31))
{ 
cout << "Illegal date. Please try again. \n";
main();
}
}

int DayOfYear::get_month()

{
return month;

}
int DayOfYear::get_day()
{
return day;
}
And what is the problem you are having with it?
Parts one and two I managed to figure out and complete. Part three is where I'm not sure how to do. The code I have above needs to be modified to the specifications of part three but I don't know how.
The stuff about modifying check_date() to check the day is appropriate for the month should be straightforward. Just put in some additional logic on top of what's already there, to make sure that the maximum valid value of day depends on which month it is. I'd recommend writing a function that takes month as an argument and returns the number of days in that month. You can then use the return value from that function to check whether the value of day is valid.

Presumably, you've been taught about constructors? Or is part of the assignment to go and research them yourself? How about operator overloading?
I've touched upon constructors but not really knowledgeable about it yet. It is suggested that you are already know alot about them but i don't. there isnt suppose to be research and operator overloading i think i'm learning next.
1
2
3
4
5
6
7
8
void DayOfYear::check_date()
{
    if ((month< 1) || (month > 12) ||(day < 1) || (day> 31))
    { 
        cout << "Illegal date. Please try again. \n";
        main();
    }
}


Don't ever call main(). Use a loop.
It seems weird that you would be given an assignment to write constructors before you've been taught about them. Basically, a constructor is a method that has the same name as the class, and which is called automatically when an object of that class is instantiated. You use it to set up the initial state of the object - initialising data members, acquiring necessary resources, or anything else.

A default constructor is one that takes no arguments. If you don't write any constructors for your class, the compiler will create an empty one anyway.

For more info, I'm sure your textbooks will be as good a source of information as anything I can type.
Lodger: its just what i was told to use but i do agree with the use of a loop.

MikeyBoy: the only thing that i can think of is that this may have been given to test our knowledge of how to do this. either way i appreciate your help.
its just what i was told to use but i do agree with the use of a loop.

Seriously? Someone told you to call main from within a function?
thanks for this huge discussion .... i need it..
Wanna know whats funny? After I completed this the teacher changed his mind about main. Hahaha thanks guys.
I'm going to take a wild guess that writing C++ - or teaching it - isn't this person's main career.
Last edited on
It actually is.
Topic archived. No new replies allowed.