Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:05:40+00:00 2026-06-01T11:05:40+00:00

The exercise says: Create a Text class that contains a string object to hold

  • 0

The exercise says:

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.

This is the class that I wrote:

class Text {
    string fcontent;

    public:
        Text();
        Text(string fname);
        ~Text();

        string contents();
};

I haven’t understood everything of this exercise. It asks to create a function contents(), that returns a string, but it doesn’t says what the function has to do…
Neither what the default constructor has to do.
Could someone help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:05:41+00:00Added an answer on June 1, 2026 at 11:05 am

    The function has to return the contents of the file, which is stored (in your case) in fcontents.

        string Text::contents()
        {
          return fcontent;
        }
    

    The default constructor doesn’t have to do anything in this case.

        Text::Text(){}
    

    EDIT:
    Seeing how many comments there are below with new problems, I’m going to recap and answer the rest of the questions here.

    in Text.h you have:

        #ifndef TEXT_HH
        #define TEXT_HH
    
        #include <string> //[1] 
    
        class Text {
            std::string fcontent;//[2]
        public:
            Text();
            Text(std::string fname);
            ~Text();
    
            std::string contents();
        };
    
        #endif
    

    and Text.cpp has

        // Text.cpp
    
        #include "Text.h"
        #include <fstream>
        #include <iostream>
        #include <sstream>
        #include <string>
    
        using namespace std;
    
        Text::Text() {}
    
        Text::Text(string fname) {
            fstream f;
            f.open(fname.c_str(), ios::in);//[3]
    
            //[4]
            std::stringstream stream;
            while(true)
            {
                char buffer[1000];
                f.getline(buffer, 1000);
                if(f.good())
                {
                    //This actually adds an extra newline at the end
                    stream << buffer << '\n';
                }
                else
                {
                    break;
                }
            }
            fcontent = stream.str();
            //remove extra newline
            fcontent.erase(fcontent.begin() + fcontent.size() - 1);
            f.close();//This is technically unnecessary, but not bad either
        }
    
        string Text::contents() {
            return fcontent;
        }
    
        Text::~Text() {}//[5]
    

    Point 1: The header file <string> contains the class definition for std::string, the C++ string. This should not be confused with <cstring> which contains functions for manipulating C strings (const char *, const char[], etc).

    Point 2: The string class exists in the ::std namespace, which means we have to either use std::string every time we want that class or use using namespace std; to pull this class into the global scope. In the header file we prefer the former method because the using declaration doesn’t go away, which means that the namespace will be changed for every header and source file that includes this one, which we want to avoid in general (ie. always). In the cpp file however, there is no problem using the using declaration and we do so.

    Point 3: fstreams take a C string as the filename parameter, we can get the corresponding C string from a C++ string with the call c_str(). This returns a const char *.

    Point 4: To read the whole text file into a string is less obvious than it seems because the way streams deal with eof (end-of-file) and state-checking stuff. In short it will read one more time than you want it to (I know, wanting is subjective, but is close enough I think) before setting the eof flag. That’s why the state is checked after calling get and before adding what’s been read to our stringstream. Streams are a fairly elaborate topic so I won’t go into it in more detail here.

    Point 5: Destructors on objects (non-pointers, like our fcontents is) are called automatically, so we don’t need to do anything to make sure that our fcontents string is destroyed when our Text object is destroyed. When we allocate something dynamically with new that’s when we have to worry about calling delete on it when we want to destroy it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The exercise says Create a function with two parameters a and b which are
Basically the exercise says Create a function which takes as an argument an array
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
I'm doing a book exercise that says to write a program that generates psuedorandom
The exercise says Make a function with parameters two int arrays and k which
I'm trying to create a simple prototype in JavaScript that removes a character from
class Exercise < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :exercises end
I am studying the SCJP, and while studying I have found an exercise that
As a training exercise, I am creating a counter that runs in a background
As an exercise with accessibility and a personal challenge to myself I decided that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.