Background: I'm trying to learn C++ and I'm doing that by writing some programs by myself. I'm decent with C, but I'm having trouble translating to OOP paradigm. I'm reading from learncpp.com and a book called Thinking in C++.
I'm noticing that reading doesn't really help much as much as writing programs and running into issues and solving them.
Here is a program I've written to reverse a string in C++ using std::string
(SO tells me this should be used as opposed to C-style strings/char arrays):
example_one.h
#ifndef example_one_h
#define example_one_h
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
class stringOps{
private:
string arr;
int length;
public:
// Constructor - Destructor
stringOps(string arr);
~stringOps();
// Member functions
//1. get length of string
int getLength();
//2. print string
const string& displayString(const stringOps &a);
//3. Reverse the string and return obj of type stringOps.
stringOps reverseStr(stringOps &a);
};
#endif
example_one.cc
#include "example_one.h"
/* Constructor and Destructor */
stringOps::stringOps(string a):arr(a), length(a.size()) {
}
stringOps::~stringOps() {
}
/* Member functions */
// 1. get Length
int stringOps::getLength()
{
return arr.size();
}
//2. display string
const string& stringOps::displayString(const stringOps &a)
{
//cout << a.arr << endl;
return a.arr;
}
//3. reverse array of the object.
stringOps stringOps::reverseStr(stringOps &a)
{
reverse(a.arr.begin(), a.arr.end());
return a;
}
/* -------------------------------- Main --------------------- */
int main()
{
stringOps strA("Hello");
string x = strA.displayString(strA);
printf("Original string: %s \n", x.c_str());
int len = strA.getLength();
printf("String length:%d \n",(len));
strA.reverseStr(strA);
string y = strA.displayString(strA);
printf("Reverse string: %s \n", y.c_str());
return 0;
}
Questions:
What is good about this code (so that I can continue to do this for other programs)?
What is bad about this code? (Be brutal in your assessment.) Specifically, I have doubts about the best approach to take while writing functions 2 and 3.
Function 2:
const string& stringOps::displayString(const stringOps &a) { //cout << a.arr << endl; return a.arr; }
I tried doing this with return type as
void
andcout
within the function, but apparently there's going to be an error for this withiostream
. So is returningconst string&
a good way to do this? I passed byconst
reference because I do not change the object within the function.Function 3:
stringOps stringOps::reverseStr(stringOps &a) { reverse(a.arr.begin(), a.arr.end()); return a; }
This again takes the argument of type
stringOps
object by reference and directly manipulates the string. However, if I were to pass this argument as aconst
, how do I go about this?Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly.
The header would look something like this:
string stringOps::reverseStr(const stringOps &a)
Do I declare static string inside the function and return it? I tried using a local non-static string (which would be destroyed outside the function, but still for experiment purposes) and I got an error that said:
read-only variable is not assignable
I'm assuming this is because it's a
const
reference.
return;
the same object. And a member function could display its instance's data without passing it its own instance, by reference and then return it, just by calling it and adding thecout
inside of it. \$\endgroup\$