CHAPTER 07:EX 01

/*
1.Create a Text class that contains a string object to hold the text of a file. 
Give it two constructors: a default constructor and a constructor that takes a 
string argument that is the name of the file to open. When the second constructor 
is used, open the file and read the contents into the string member object. Add 
a member function contents( ) to return the string so (for example) it can be 
printed. In main( ), open a file using Text and print the contents.
*/

#include
< iostream >
#include
< string >
#include
< fstream >
using   namespace  std;

class  Text  {
    
string article;
public:
    Text();
    Text(
string fname);
    
void contents();
}
;

Text::Text() 
{ }

Text::Text(
string  fname)  {
    ifstream file((
char*)fname.c_str());
    
string line;
    
while (getline(file, line)) {
        article 
+= line + ' ';        
    }

}


void  Text::contents()  {
    cout 
<< article << endl;
}


int  main()  {
    Text a(
"solution01.cpp");
    a.contents();
}

你可能感兴趣的:(CHAPTER 07:EX 01)