C/C++ 编程代写
当前位置:以往案例 > >c++案例编程:Collection of news items for a ne
2019-01-19

Within this task classes shall be practised using as ple the collection of news items for a news broadcast.
Correspondents contribute news items for the country they are in charge of.
Each broadcast shall have a top news item, a number of further news items, a last minute one as well as the weather forecast at its end.
For each news item the topic, the country and the duration shall be inputted and stored.
At the end the schedule of a news broadcast shall be printed out.




Subtask 1

Define a class with name Duration to store the duration of news items. The class shall have following members:


a private integer attribute seconds


a public standard constructor initialising the duration with value 00:00.


a private constructor with one integer parameter for seconds initialising the attribute with this value


a public constructor with two integer parameters for minutes (mm) and seconds (ss) initialising the duration to value mm:ss (hint: one minute has 60 seconds).



Furthermore the class shall have three friend operators:


a public overloaded output operator << for a reference to an object of this class. Always a duration shall be printed out with five characters in format mm:ss (minutes:seconds), e.g. 00:55, 01:30 or 11:09.


a public overloaded comparison operator <= for two reference parameters of this class comparing two durations and returning true or false.


E.g. comparing durations 01:47 <= 02:25 value true shall be returned, comparing 03:07 <= 02:09 value false.


a public overloaded addition operator + for two parameters of this class adding the two durations and returning a resulting duration object.
E.g. 01:47 + 02:25 shall return a duration object with 04:12.


Subtask 2

Define a constant static object of type Duration with name zero with duration 00:00 as well as a non constant static object with name standard with duration 01:30 (standard duration of a news item).


Subtask 3

Define a class with name Correspondent with following members:

a private attribute name of type C++ string for the name of a correspondent.

a private attribute country of type C++ string for the country.

a private static integer class constant maxCorrespondents with value 200 (countries).

a private static integer class variable numberOfCorrespondents which gets initialised (hint: outside of the class!) with 0.

a private static array correspondent with maxCorrespondents pointers to objects of this class as static class variable which all get initialised at compile time (hint: outside of the class!) with nullptr.

a private constructor with two C++ strings as parameter to initialise the name and the country of the correspondent.

a public static method with name new_correspondent with two C++ strings as parameter for the name and the country of a correspondent without return value.
In the body a new object of this class shall be generated on the heap initialised by the name and the country, its address stored in the next free element of pointer array correspondent and numberOfCorrespondents shall be incremented by 1.
If the array is completely full instead an error message shall be written.

a public static method with name get_correspondent with a C++ string as parameter for the country and a pointer to a correspondent as return value.
In the body the correspondent shall be searched for in the static array correspondent and a pointer to her/him or a null pointer being returned.

· a public overloaded output operator << for a reference to an object of this class.
In the body inside round brackets the country shall be outputted, following a minus sign and the name of the correspondent (see ples in screenshot below).

Subtask 4

For single news items define a class with name Item with following members:

a private attribute with name duration of type Duration.

a private attribute with name topic of type C++ string.

a private pointer attribute with name correspondent of type Correspondent.

a public standard constructor initialising the duration with above defined object zero, the topic with the empty string and the correspondent to a null pointer.

a public constructor with a duration, a string and a pointer to a correspondent to initialise the three attributes.


a public destructor.


two public methods with name get_duration and get_topic without parameter returning a reference to each of the attributes.


a public method with name get_correspondent without parameter returning a pointer to the correspondent.


three public methods with names set_duration, set_topic and set_correspondent each with one parameter assigning the corresponding values of the attributes.


a public method with name input without parameter or return value.
In the body the topic, the duration and the country shall be inputted from keyboard and these values assigned to the three attributes whereas the correspondent for the country shall be searched by calling static member function get_correspondent from class Correspondent.


a public overloaded output operator << for a reference to an object of this class.
In the body the duration shall be outputted, following sign + if it is longer than above defined object standard (use the comparison operator <= defined for Duration), then a tabulator sign, the topic as well as output the object correspondent is pointing to (see ples in screenshot below).


Subtask 5

For news define a class with name News with following members:

a private static constant integer attribute with name n with value 3.

a private attribute with name first of type Item.

a private attribute with name news as an array of n objects of type Item.

a private attribute with name weather of type Item for the weather forecast.

a private pointer attribute with name lastMinute of type Item.

a public standard constructor with following functionality:

After a user guidance send message input to object first.

After a user guidance in a loop send message input to the n objects in array news.

Send object weather a message set_topic with "weather forecast" as argument.

Send object weather a message set_duration with a two minute Duration object as argument.

Assign a new object on heap of type Item to pointer lastMinute.

Send a message set_topic with argument "end of the year is close" to the object lastMinute points to.

Send a message set_duration with above defined object standard as argument to the object lastMinute points to.

a public method print without parameter or return value.
In its body by using the output operator shall be written onto the standard character output stream:

"THE NEWS",

object first,

in a loop all n objects in array news,

the object lastMinute points to as well as

the object weather.

Add (in a loop) the single durations of all news items using above defined operator + (not directly the seconds but the objects of type Duration) and output the overall total duration of the news like shown in ple below.

Subtask 6

Write a C++ function main with following definitions and statements:

define a C++ string variable, successively as ples input three names of corresponents for France, Germany and Great Britain and call new_correspondent from class Correspondent three times respectively.

Define an object with name evening of type News for the evening news (by the standard constructor all items are inputted).

Send object evening a message print.

在线提交订单