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

  • Home
  • SEARCH
  • 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 8380619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:30:13+00:00 2026-06-09T16:30:13+00:00

I thought I was good at this programming thing, until I hit this devil

  • 0

I thought I was good at this programming thing, until I hit this devil of a walkthrough that is (conveniently) absent from my textbook.

My guess is something about a copy constructor.

It involves two classes, Paragraph and Word. There is a dynamic array of Word objects inside each Paragraph class. The trouble is when the Paragraph class += operator overload method is called in main() – a professor comments that it calls the Word constructor, then the Word = operator.

Correct me if I’m wrong, but shouldn’t there be another operator overload to handle a Word object on the left and a string on the right??

#include <iostream>
 using namespace std;
 #define WIDTH 8

 class Word {
     char* text;
     int nletters;
   public:
     Word() {
         text = NULL;
         nletters = 0;
         cout << 'W';
     }
     Word(const char* s) {
         nletters = 0;
         while(s[nletters] != '\0')
             nletters++;
         text = new char[nletters];
         for (int i = 0; i < nletters; i++)
             text[i] = s[i];
         cout << 'X';
     }
     Word(const Word& p) {
         text = NULL;
         *this = p;  // calls assignment operator below
         cout << 'Y';
     }
     ~Word() {
         if (text != NULL) delete [] text;
         cout << "~W" << nletters;
     }
     Word& operator=(const Word& p) {
         if (this != &p) {
             if (text != NULL) delete [] text;
             nletters = 0;
             if (p.text != NULL) {
                 text = new char[p.nletters];
                 while(nletters < p.nletters) {
                     text[nletters] = p.text[nletters];
                     nletters++;
                 }
             }
             else
                 text = NULL;
         }
         cout << 'V';
         return *this;
     }
     int nLetters() const { return nletters; }
     friend ostream& operator<<(ostream& os, const Word& w) {
         if (w.text != NULL)
             for (int i = 0; i < w.nletters; i++)
                 os << w.text[i];
         else
             os << "***";
         os << ' ';
         return os;
     }
 };

 class Paragraph {
     Word* word;
     int mwords;
     int nwords;
     int width;
   public:
     Paragraph() {
         word  = NULL;
         width = WIDTH;
         nwords = 0;
         mwords = 0;
         cout << 'P' << endl;
     }
     Paragraph(const Paragraph& c) {
         word  = NULL;
         *this = c;  // calls assignment operator below
         cout << "cP";
     }
     Paragraph& operator=(const Paragraph& c) {
         if (this != &c) {
             if (word != NULL) delete [] word;
             if (c.word != NULL) {
                 word = new Word[c.mwords];
                 cout << endl;
                 for (int i = 0; i < c.nwords; i++)
                     word[i] = c.word[i];  // calls Word assignment operator
             }
             else
                 word = NULL;
             width  = c.width;
             nwords = c.nwords;
             mwords = c.mwords;
         }
         cout << "=P";
         return *this;
     }
     ~Paragraph() {
         if (word != NULL) delete [] word;
         cout << '\n' << nwords << "~P" << endl;
     }
     void make(int m) {
         if (word == NULL) {
             word = new Word[m];
             mwords = m;
             nwords = 0;
             width  = WIDTH;
         }
     }
     void setWidth(int w) { width = w; }
     friend ostream& operator<<(ostream& os, const Paragraph& p) {
         int nextWord = 0;
         for (int i = 0; i < p.nwords; i++) {
             if (nextWord + p.word[i].nLetters() > p.width) {
                 os << '\n';
                 nextWord = 0;
             }
             cout << p.word[i];
             nextWord += p.word[i].nLetters() + 1;
         }
         return os;
     }
     Paragraph& operator+=(const char* w) {
         if (nwords < mwords) {
             cout << "\n+=";
             word[nwords] = w;  // calls Word constructor, then Word = operator
             nwords++;
         }
         return *this;
     }
 };

 int main() {

     Paragraph p;
     cout << "--------\n";
     p.make(5);
     p += "This";
     p += "is";
     p += "hard";
     cout << "\n--------\n";
     cout << p << endl;
     cout << "--------\n";
     Paragraph q = p;
     q.setWidth(6);
     q += "too";
     cout << "\n--------\n";
     cout << q << endl;
     cout << "---------------\n";
     return 0;
 }
  • 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-09T16:30:15+00:00Added an answer on June 9, 2026 at 4:30 pm

    Your professor is right. You don’t need another operator since a const char* is directly (and implicitly) convertible to a Word, through:

    Word(const char* s)
    

    This is called a conversion constructor. If you declare it as explicit, it should no longer compile. See here – http://ideone.com/brjIi

    So on the line

    word[nwords] = w;
    

    a temporary Word is indeed created from w, and then the assignment operator for Word is called to assign the temporary to words[nwords].

    Some other notes on the code:

    • since your professor teaches you C++, you should learn about std::string and std::vector. He doesn’t need C++ nor classes to demonstrate the power of pointers or dynamic memory allocation.
    • statements like if (word != NULL) delete [] word; are redundant. delete or delete[] on a NULL pointer is guaranteed to work (and not do anything).
    • the logic in the constructor is wrong (wouldn’t be if you didn’t manage memory yourself). After you construct a Paragraph, it’s unusable until you call make(), which is bad design.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am programming some 'openvpn-like' thing and thought it would be a good candidate
This StackOverflow question gave me food for thought on what is a good structure
Before I throw something ugly together I thought this would be a good one
I thought I had a good understanding of how quicksort works, until I watched
I'm new to programming and thought Python would be a good language to learn.
I recently started programming JavaScript and thought everything would be good... Well today I
I thought it was a good idea to add some testing to my project,
Working with Unity, I thought it would be a good idea to create some
I'm interested in learning some python, and thought Pylons would be a good starting
Thought my range of search options would easily find this. I wish to combine

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.