Design and code a class named
Textthat manages a dynamically allocated array of strings. Upon instantiation, aTextobject receives nothing or a reference to an unmodifiable string. The string holds the name of the text file that contains the records to be stored in an object of this class. If the file does not exist, theTextobject assumes a safe empty state. If the file exists, the one-argument constructor allocates memory for the number of records contained in the file and copies them into memory. To review the syntax for reading from a text file using anifstreamobject see the chapter in your OOP244 notes entitled Custom File Operators. See also cplusplus.com.Your design also includes the following member functions:
- a copy constructor
- a copy assignment operator
- a move constructor
- a move assignment operator
- a destructor
- a member function named
size_t size() constthat returns the number of records of text dataDefine your class and its implementation in
namespace w3.
Here is my code:
//Text.h
namespace w3{
class Text{
std::string file_name;
std::string* handler;
int no_of_rec=0;
public:
Text();
Text(char*);
size_t size() const;
Text(const Text& );
Text& operator=(const Text&);
Text(Text&&);
Text&& operator=(Text &&);
~Text();
}; // class Text
} //namespace w3
//Text.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include "Text.h"
namespace w3{
Text::Text(){
file_name="";
handler = nullptr;
no_of_rec = 0;
}
Text::Text(char* fname){
file_name = fname;
if (fname[0]='\0'){
file_name=" ";
std::cout << "can not find file name !!" <<std::endl;
}else{
std::fstream f(file_name);
std::string line;
if (f.is_open()){
while (std::getline(f,line,'\n')){
no_of_rec++;
}
}else{
std::cout << "file not open !!" <<std::endl;
}
//std::cout << "number of records" << size() << std::endl;
std::string* handle = new std::string[size()];
f.clear();
f.seekg(0,std::ios::beg);
int counter = 0;
while (std::getline(f,line,'\n')){
if (counter != no_of_rec){
handle[counter]=line;
counter++;
}
}
//std::cout << handle [1] <<std::endl;
}
}
size_t Text::size() const{
return (size_t)no_of_rec;
}
//-------------------Special Member Functions-----------
Text::Text(const Text& src){ //copy constructor
std::string file_name = src.file_name;
int no_of_rec = src.no_of_rec;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler = src.handler;
//std::cout << handler[123]<<std::endl;
}else{
handler = nullptr;
}
}
Text& Text::operator=(const Text& src){
if (this != &src){
int no_of_rec = src.no_of_rec;
std::string file_name = src.file_name;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler=src.handler;
}else{
handler = nullptr;
}
}
return *this;
}
Text::Text(Text&& src){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
Text&& Text::operator=(Text&& src){
if (&src != this){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
return std::move(*this);
}
Text::~Text(){
//delete [] handler;
}
}
//main.cpp
#include <iostream>
#include <iomanip>
#include <utility>
#include <ctime>
// #include "Text.h"
#include "Text.cpp"
#define TIME(start, end) double((end) - (start)) / CLOCKS_PER_SEC
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::clock_t cs, ce;
{
std::cout << std::fixed << std::setprecision(3);
cs = std::clock();
w3::Text a;
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text b(argv[1]);
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - b.size = " << b.size() << std::endl;
cs = std::clock();
a = b;
ce = std::clock();
std::cout << "Copy Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
a = std::move(b);
ce = std::clock();
std::cout << "Move Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text c = a;
ce = std::clock();
std::cout << "Copy Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - c.size = " << c.size() << std::endl;
cs = std::clock();
w3::Text d = std::move(a);
ce = std::clock();
std::cout << "Move Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - d.size = " << d.size() << std::endl;
cs = std::clock();
}
ce = std::clock();
std::cout << "Destructor " << TIME(cs, ce) << " seconds\n";
}
I am having error that there is multiple definition of Text in Text.cpp
I know there is problem with my namespace but i dont know how to fix it.
Aucun commentaire:
Enregistrer un commentaire