0

I have the following code:

#include <iostream>
#include <cstring>

using namespace std;

// Class declaration
class NaiveString {
    private:
        char *str;

    public:
        NaiveString(const char*);
        NaiveString(const NaiveString&);
        void update(char, char);
        void print();
};

// Constructors
NaiveString::NaiveString(const char* t) {
    str = new char[strlen(t) + 1];
    strcpy(str, t);
    cout << "First constructor is being invoked." << endl;
}

NaiveString::NaiveString(const NaiveString& src) {
    str = new char[strlen(src.str) + 1];
    strcpy(str, src.str);
    cout << "Second copy constructor is being invoked." << endl;
}

// Methods
/* The following method replaces occurrences of oldchar by newchar */
void NaiveString::update(char oldchar, char newchar) {
    unsigned int i;
    for (i = 0; i < strlen(str); i++) {
        if (str[i] == oldchar) {
            str[i] = newchar;
        }
    }
}

/* The following method prints the updated string in the screen */
void NaiveString::print() {
    cout << str << endl;
}

// Function
void funcByVal(NaiveString s) {
    cout << "funcbyval() being called" << endl;
    s.update('B', 'C');
    // Printing the results
    s.print();
}

int main() {

    NaiveString a("aBcBdB");
    a.print();

    NaiveString b("test, second object");
    b.print();

    cout << "About to call funcbyval(): " << endl;
    // Calling funcByVal
    funcByVal(a);

    // Printing the results
    a.print();


    return 0;
}

and this problem for the above code:

Based on the above source code, implement the method funcByref(). Then in your main function create at least two objects using the different constructors, call funcByVal(), funcByRef(), and print the results on the screen. Then make sure that the memory occupied by the objects will be released by the end of the program.

I have created the 2 objects but they are currently using the same constructor. How can I do it so they can use different constructors? Also, how can I create that funcByRef() and call it? Last, how to release the memory occupied by the objects in the end of the program?

Looking forward to your answers.

Thank you

zeeks
  • 775
  • 2
  • 12
  • 30
  • 1
    Read about copy constructors (your second constructor) Also, don't forget to add a destructor to delete str. – doug Nov 29 '15 at 04:40
  • 1
    Guess you need to learn about constructors and destructors in detail. However, just add destructor to solve the problem. There will be only one destructor unlike constructors. – sarat Nov 29 '15 at 04:57

3 Answers3

1

How can I do it so they can use different constructors?

That is not necessary. Why do you think you need that?

Also, how can I create that funcByRef() and call it?

There are lot of resources on the web that describe how to pass by reference in C++. See functions in c++ call by value and call by reference as an example.

Last, how to release the memory occupied by the objects in the end of the program?

There is nothing to do. The destructor will be called for each object, which should release any memory. That hasn't been done in your class. See What is The Rule of Three? for more info.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You want to call the second constructor but you are passing a char* to it, you need to pass a NaiveString& like this

NaiveString a("aBcBdB");
a.print();

NaiveString b(a);   // a is a NaiveString reference here
b.print();

This is how the funcByRef() would be, just pass it by reference

void funcByRef(NaiveString& s){
    s.update('B', 'C');
    s.print();
}

Lastly, to free your string you need to create a destructor like this one

NaiveString::~NaiveString()
{
    delete[] str;
}
1

How can I do it so they can use different constructors?

In order to call different constructors, you must overload them, which can be done by having different argument lists.

For example, in your class, you have two different constructors, one of them receives a string and the other receives a reference to an object.

So you can call either one of these constructors by passing different types of arguments to it.

Also, how can I create that funcByRef() and call it?

I'm imagining that the funcByRef should receive a reference to the object such as

void funcByRef(NaiveString& s)
{
    s.update(A,B);
    s.print();
}

Last, how to release the memory occupied by the objects in the end of the program?

At first you wouldn't need to release the memory, because your program finished and the whole memory stack will be freed anyway. However, I'm assuming this is some sort of assignment, so, in order to release the memory you should call delete. As in:

delete a;
delete b;

However, notice that the delete calls the destructor functions of your class, which, is not defined. The default destructor will probably miss the char array. So in your destructor function you must free the memory from that array. I'd say something like:

~NaiveString()
{
    delete str;
}
wmaciel
  • 53
  • 8