move构造函数,move赋值函数

来源:互联网 发布:显示器颜色校准软件 编辑:程序博客网 时间:2024/05/14 15:22

要求:


ContainersIn this workshop, you code a container class that holds notifications and a class that holds separate messages.LEARNING OUTCOMESUpon successful completion of this workshop, you will have demonstrated the abilities to• design and code a composition of objects• read records from a file into a string object• parse a string object into components based on simple rules• reflect on the material learned in this workshopSPECIFICATIONSOverviewThis workshop retrieves messages from a data file and collects them in a notification. Each record in the data file contains a single message and ends with the same delimiting character.The test data file contains:jim Workshop 5 is coolharry @jim working on workshop 5 nowchrisdave what the ^#$%!john @harry I'm doneThe first message consists of a user name followed by a tweet. The second message consists of a user name, a reply name prefaced by an @, and followed by a tweet. Your solution ignores incomplete messages, such as the third message here.SolutionYour complete solution to this workshop consists of three modules:• w5 - the application that collects and displays notifications• Notifications - the module that holds and manages messages• Message - the module that manages the retrieval and display of a single messageThe classes for this workshop are defined in the w5 namespace.ApplicationThe source file that uses your two classes is:// Workshop 5 - Containers// w5.cpp#include <iostream>#include <fstream>#include "Message.h"#include "Notifications.h"const char REC_DELIMITER = '\n';w5::Notifications collect(std::ifstream& in, char recDelim) {w5::Notifications notifications;do {w5::Message message(in, recDelim);if (!message.empty())notifications += std::move(message);} while(in);return notifications;}int main(int argc, char* argv[]) {if (argc == 1) {std::cerr << argv[0] << ": missing file operand\n";return 1;}else if (argc != 2) {std::cerr << argv[0] << ": too many arguments\n";return 2;}std::ifstream file(argv[1]);if (!file) {std::cerr << argv[0] << "\n: Failed to open " << argv[1] << "\n";return 3;}std::cout << "\nNotifications\n=============\n\n";w5::Notifications notifications = collect(file, REC_DELIMITER);notifications.display(std::cout);std::cout << "Press any key to continue ... ";std::cin.get();}Notifications ModuleA Notifications object manages access to a set of up to 10 Message objects. The Notifications object collects copies of the Message objects, owns those copies and destroys them once they are no longer needed.Your design of the Notifications class includes the following member functions:• Notifications() - default constructor - empty• Notifications(const Notifications&) - copy constructor• Notifications& operator=(const Notifications&) - copy assignment operator• Notifications(Notifications&&) - move constructor• Notifications&& operator=(Notifications&&) - move assignment operator• ~Notifications() - destructor• void operator+=(const Message& msg) - adds msg to the set• void display(std::ostream& os) const - inserts the Message objects to the os output streamStore the code for your Notifications module in two source files:• Notifications.h - defines the Notifications class• Notifications.cpp - implements the member functions for the Notifications classMessage ModuleA Message object holds nothing or a single message that has been successfully retrieved from an std::ifstreamfile object. An object that holds nothing is in a safe empty state.Your design of the Message class includes the following member functions:• Message(std::ifstream& in, char c) - constructor retrieves a record from the in file object, parses the record (as described above) and stores its components in the Message object. c is the character that delimits each record• bool empty() const - returns true if the object is in a safe empty state• void display(std::ostream&) const - displays the Message objects within the containerStore the code for your Message module in two source files:• Message.h - defines the Message class• Message.cpp - implements the member functions for the class.ResultsThe results generated by the application using your solution and the test data file are listed below:Notifications=============MessageUser : jimTweet : Workshop 5 is coolMessageUser : harryReply : jimTweet : working on workshop 5 nowMessageUser : daveTweet : what the ^#$%!MessageUser : johnReply : harryTweet : I'm donePress any key to continue ...SUBMISSIONOnce you have completed your lab create a single ZIP file that contains the following information and upload your ZIP file to Blackboard using the lab submission link.All your source code files (*.h and *.cpp)Execution instructions file (if there is anything special I need to know to successfully run your program write them down in a README file)Any input files required (test inputs, etc….)A 2-paragraph “what did I do and learn in this lab” write-up.


#ifndef MESSAGE_H_#define MESSAGE_H_#include <string>#include <fstream>#include <iostream>using namespace std;namespace w5{class Message{public:Message(std::ifstream& in,char c);~Message();bool empty() const;void display(std::ostream&) const;public:char *message;int num;};}#endif

#include "Message.h"using namespace std;char mess[] = "Message";char user[] = "User  : ";char tweet[] = "Tweet : ";char reply[] = "Reply : ";w5::Message::Message(std::ifstream &in, char c){message=new char[100];char buff[100];int flag1 = 0,flag2=0,index=0,index1,count;num=0;char temp;temp=in.get();while(temp!=c &&!in.eof()){if (temp == ' ') flag1 = 1;if (temp == '@') flag2 = 1;buff[index] = temp;index++;temp=in.get();}buff[index] = '\0';if (flag1 == 0) return;for (int i = 0; mess[i] != '\0'; i++)  //user{message[i] = mess[i];num++;}message[num] = '\n';num++;for (count = 0; user[count] != '\0'; count++)  //user{message[count + num] = user[count];}num+=count;count = 0;for (index1 = 0; buff[index1] != ' '; index1++) //user name{count++;message[num + index1] = buff[index1];}num = num + count;message[num] = '\n';num++;if (flag2 == 1){int i = 0;for (; reply[i] != '\0'; i++)      //reply{message[num+i] = reply[i];}num = num + i;index1+=2;for (i = 0; buff[index1] != ' '; index1++)  //reply name{message[num + i] = buff[index1];i++;}num = num + i;message[num] = '\n';num++;}for (index=0; tweet[index] != '\0'; index++){message[num + index] = tweet[index];}num += index;count = 0;index1++;for (; buff[index1] != '\0'; index1++){message[num + count] = buff[index1];count++;}num += count;message[num] = '\n';message[++num] = '\0';}bool w5::Message::empty() const{return num==0;}void w5::Message::display(std::ostream &out) const{out<<message<<endl;}w5::Message::~Message(){delete []message;}

#ifndef NOTIFICATIONS_H_#define NOTIFICATIONS_H_#include "Message.h"namespace w5{class Notifications{public:Notifications();Notifications(const Notifications&);Notifications& operator=(const Notifications&);Notifications(Notifications&&);            //move constructorNotifications& operator=(Notifications&&);~Notifications();void operator+=(const Message&msg);void display(std::ostream& os) const;public:char *noti;int num;};}#endif

#include "Notifications.h"using namespace std;w5::Notifications::Notifications(){noti=new char[1024];num = 0;}w5::Notifications::~Notifications(){if (noti != NULL)delete noti;}w5::Notifications::Notifications(const w5::Notifications &old){noti = new char[1024];num = old.num;for (int i = 0; old.noti[i] != '\0'; i++)noti[i] = old.noti[i];}w5::Notifications& w5::Notifications::operator=(const Notifications&old){if (this == &old)  return *this;if (noti != NULL){delete []noti;noti = NULL;}noti = new char[1024];num = old.num;for (int i = 0; old.noti[i] != '\0'; i++)noti[i] = old.noti[i];return *this;}w5::Notifications::Notifications(Notifications&& old) :noti(old.noti){old.noti = NULL;num = old.num;}w5::Notifications& w5::Notifications::operator=(Notifications&& old){auto noti = this->noti;this->noti = old.noti;old.noti = noti;num = old.num;return *this;}void w5::Notifications::operator+=(const w5::Message&msg){int index=0;if (num > 0){noti[num] = '\n';num++;}for (int i = 0; msg.message[i] != '\0'; i++){noti[num + i] = msg.message[i];}num=num+msg.num;noti[num] = '\0';}void w5::Notifications::display(std::ostream& os) const{//for (int i = 0; noti[i] != '\0'; i++)//os << noti[i] << " ";os <<noti<< endl;}

// Workshop 5 - Containers// w5.cpp#include <iostream>#include <fstream>#include "Message.h"#include "Notifications.h"const char REC_DELIMITER = '\n';w5::Notifications collect(std::ifstream& in, char recDelim) {w5::Notifications notifications;do {w5::Message message(in, recDelim);if (!message.empty())notifications += std::move(message);} while(in);return notifications;}int main(int argc, char* argv[]) {if (argc == 1) {std::cerr << argv[0] << ": missing file operand\n";return 1;}else if (argc != 2) {std::cerr << argv[0] << ": too many arguments\n";return 2;}std::ifstream file(argv[1]);if (!file) {std::cerr << argv[0] << "\n: Failed to open " << argv[1] << "\n";return 3;}std::cout << "\nNotifications\n=============\n\n";w5::Notifications notifications = collect(file, REC_DELIMITER);notifications.display(std::cout);std::cout << "Press any key to continue ... ";std::cin.get();}


0 0