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