I just started learning about operator overloading and was just playing around with code to learn how it works. So, I wrote a code that adds two characters. Ex: '#' + '%' = 'H' because of ASCII value addition.
Here is my code:
#include <iostream>
using namespace std;
class Strings {
//everything is public
public:
char str;
//empty constructor
Strings();
Strings(char);
//operator takes in an object of Strings
Strings operator+(Strings);
};
Strings::Strings()
{}
//constructor
Strings::Strings(char a) {
str = a;
}
//aso is "another string object"
//makes new empty object "brandNew"
//brandNew is the two characters added together
//returns object brandNew
Strings Strings::operator+(Strings aso) {
Strings brandNew;
brandNew.str = str + aso.str;
return brandNew;
}
int main() {
Strings a('#');
Strings b('%');
Strings c;
//now, we can use + operator to add characters
c = a + b;
cout << c.str << endl;
return 0;
}
What if I wanted to add two strings? If I make the input
Strings a("###");
Strings b("%%%");
And I want the output to be
HHH
How would I alter my code to add the two strings? I started with changing all of the char type declarations into string. I'm thinking I would have to make a for loop inside the operator function to iterate through each character of both inputs while adding them. But, I'm confused on the syntax for it and also confused on how to implement that. Any help and explanation would be appreciated!
I will give you some help declaring the class.
class Strings {
private:
char* str;
unsigned int length;
unsigned int size;
public:
//constructor
Strings();
~Strings();
Strings(const char*);
Strings(const Strings&);
//operator
Strings operator+(const Strings&);
Strings operator+(const char*);
Strings operator=(const Strings&);
Strings operator=(const char*);
Strings operator+=(const Strings&);
Strings operator+=(const char*);
///Accessors
const char* GetStr()const;
unsigned int GetLength()const;
unsigned int GetSize()const;
};
Related
I have a derived class called Mystring that is derived from std::string, and I would like to set the value of the string that I am working with.
From my understanding to access the string object from std::string I would use *this to get the string that I am currently working with.
I would like to set *this to a string of my choosing, I did this my setting *this = n; but it crashes my code and returns a "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)" my code is below:
So my question is, how can I set the value of std::string to something through my derived class. Much thanks!
class Mystring : public std::string
{
public:
Mystring(std::string n);
std::string removePunctuation();
std::string toLower();
};
Mystring::Mystring(std::string n)
{
*this = n;
}
std::string Mystring::removePunctuation()
{
long int L = length();
char *cstr = new char[L + 1];
strcpy(cstr, c_str());
//cout << cstr[L-1] << endl; // last character of c string
if(!isalpha(cstr[L-1]))
{
pop_back() ;
}
return *this;
}
std::string Mystring::toLower()
{
long int L = length();
char *cstr = new char[L + 1];
strcpy(cstr, c_str());
for(int i = 0; i < L;i++)
{
int buffer = cstr[i];
cstr[i] = tolower(buffer);
std::cout << cstr[i];
}
std::string returnstring(cstr);
delete [] cstr;
return returnstring;
}
int main() {
Mystring temp("dog");
std::cout << "Hello World";
return 0;
}
Style aside, the fundamental idea of using an assignment operator to "reset" an inherited subobject is not necessarily incorrect.
However, a conversion is required to get from std::string (the type of the RHS) to Mystring (the type of the LHS, i.e. *this). The only way to perform that conversion is to use the constructor Mystring(std::string). Except… you're already in it. Hence that function is effectively recursive and will repeat forever until you exhaust your stack.
You need to upcast *this to a std::string in order to make this work:
static_cast<std::string&>(*this) = n;
I do agree with the other people here that you shouldn't be deriving from std::string, and certainly not just to add a couple of utility functions that ought to be free functions taking std::string (perhaps in a nice namespace, though?).
Don't do it. Derivation provides no benefit in this situation.
Create your added functions as free functions that operate on a string. For example:
void remove_punctuation(std::string &s) {
if (!std::isalpha(s.back()))
s.pop_back();
}
void tolower(std::string &s) {
for (auto &c : s)
c = std::tolower(c);
}
Making either/both of these a member function serves no purpose and provides no benefit.
References
GOTW #84: Monoliths Unstrung
How Non-Member Functions Improve Encapsulation
I am confused about this library code..
what is the purpose of writing pointer in attributes name?
Like string *name;
this is the code:
#include <iostream>
using namespace std;
class stringlist
{
private:
int numstrings;
string *strings;
public:
stringlist() : numstrings(0), strings(NULL)
{}
// copy constructor
stringlist(const stringlist &other) :
numstrings(other.numstrings),
strings(new string[other.numstrings]) {
for (int i=0; i<numstrings; i++) {
strings[i] = other.strings[i];
}
}
// destructor
~stringlist() {
if (numstrings > 0) delete [] strings;
}
void printStrings();
const string& getString(int num);
void setString(int num,
const char *astring);
void setString(int num,
const string &astring);
int getNumstrings() {
return numstrings;
}
void setNumstrings(int num);
void swapStrings(stringlist &other) ;
// overloaded operators
stringlist& operator =
(const stringlist &other) {
stringlist temp(other);
swapStrings(temp);
return *this;
}
};
Can anyone please explain what is the purpose of
using string *strings
instead of string strings?
Thanks all!
string *strings;
declares a pointer to the first element of an array of strings. Because of C/C++ array/pointer semantics, you can treat this as an array of strings, e.g. index it as strings[n] to get the nth element.
string strings;
would just be one string. Since the class is for holding a list of strings, declaring just one string would not be sufficient.
In the code you gave, *strings is used for a dynamic array.
You can see that it's allocated with strings(new string[other.numstrings]) and the destructor deletes it if it's pointing to something.
More on dynamic arrays:
- http://www.cplusplus.com/doc/tutorial/dynamic/
Static array vs. dynamic array in C++
readFruit.name is initialized to NULL before I try to initialize it as a char array. I included size to see if that was the culprit but it is exactly what it should be depending on my input. No matter what length tempString is, readFruit.name is allocated around 25 "characters" in memory and they're all garbage. Why isn't it being allocated a space the size of tempString.length() and how can I fix it?
relevant CPP
std::istream & operator>>(std::istream &is, Fruit &readFruit)
{
string tempString;
is >> tempString;
int size = tempString.length();
readFruit.name = new char[tempString.length()];
for(int i = 0; i < (int)tempString.length(); i++)
{
readFruit.name[i] = tempString[i];
}
for(int i =0; i < CODE_LEN; i++)
{
is >> readFruit.code[i];
}
return is;
}
Relevant H file (constructor)
#ifndef _FRUIT_H
#define _FRUIT_H
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
enum { CODE_LEN = 4 };
enum { MAX_NAME_LEN = 30 };
class Fruit
{
private:
char *name;
char code[CODE_LEN];
public:
Fruit(const Fruit &temp);
Fruit(){name = NULL;};
bool operator<(const Fruit& tempFruit);
friend std::ostream & operator<<(std::ostream &os, const Fruit& printFruit);
bool operator==(const Fruit& other){return *name == *other.name;};
bool operator!=(const Fruit& other){return *name != *other.name;};
friend std::istream & operator>>(std::istream& is, Fruit& readFruit);
};
#endif
If you're trying to print readFruit.name, it will display garbage value until it finds a null termination, that's what I assume you saying 25 characters "all garbage"
Allocated memory like this :
readFruit.name = new char[tempString.length()+1];
And after for loop do:
readFruit.name[i] ='\0'; // C strings are null terminated
To solve your problem, you need to null-terminate your name character array. Right now you copied all characters, but you need to have a binary zero character at the end for all string functions to work. So you need to allocate one char more and write a '\0' to it.
That said: use std::string for your fruit's name. There is no reason to help yourself to a ton of self-made bugs by staying with a character array.
Out of curiosity, I was wondering how different files are able to assign certain symbols to change they regular c-string literals into other literals.
For Example...
In Objective-C, the NSString literal can be written by #"..."
In C++ (I think), the C++ String literal is written S"..."
In the wchar library, the wchar_t literal is writtel L"..."
Can anyone tell me how to do a string like, MyClass literal as #"..."
Thanks
You can use only something like this.
#include <iostream>
#include <string>
struct MyClass
{
public:
MyClass(const char* a, size_t sz):
s(a, sz)
{
}
std::string get() const { return s; }
private:
std::string s;
};
MyClass operator "" _M(const char* arr, size_t size) { return MyClass(arr, size); }
int main()
{
MyClass c = "hello"_M;
std::cout << c.get() << std::endl;
}
C++11 allows user-defined literals. http://liveworkspace.org/code/cfff55e34d3b707e1bf0cb714e8e8f29
But there are no abilities to define prefix literals.
I am practicing overloading operators in C++ right now and I have a problem.
I created String class, it has just to fields one is char array other is length.
I have a String "Alice has a cat" and when I call
cout<<moj[2];
I would like to get 'i', but now I am getting moj + 16u adress of moj + 2 sizeof(String)
When I call
cout<<(*moj)[2];
it works as it shoud but I would like to dereference it in overloaded operator definition. I tried many things but I can't find solution. Please correct me.
char & operator[](int el) {return napis[el];}
const char & operator[](int el) const {return napis[el];}
AND the whole code, the important things are down the page. It's compiling and working.
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstring>
using namespace std;
class String{
public:
//THIS IS UNIMPORTANT------------------------------------------------------------------------------
char* napis;
int dlugosc;
String(char* napis){
this->napis = new char[20];
//this->napis = napis;
memcpy(this->napis,napis,12);
this->dlugosc = this->length();
}
String(const String& obiekt){
int wrt = obiekt.dlugosc*sizeof(char);
//cout<<"before memcpy"<<endl;
this->napis = new char[wrt];
memcpy(this->napis,obiekt.napis,wrt);
//cout<<"after memcpy"<<endl;
this->dlugosc = wrt/sizeof(char);
}
~String(){
delete[] this->napis;
}
int length(){
int i = 0;
while(napis[i] != '\0'){
i++;
}
return i;
}
void show(){
cout<<napis<<" dlugosc = "<<dlugosc<<endl;
}
//THIS IS IMPORTANT
char & operator[](int el) {return napis[el];}
const char & operator[](int el) const {return napis[el];}
};
int main()
{
String* moj = new String("Alice has a cat");
cout<<(*moj)[2]; // IT WORKS BUI
// cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE
return 0;
}
String* moj = new String("Alice has a cat");
cout<<(*moj)[2]; // IT WORKS BUI
// cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE
That can't be done, the subscript operator in the later case is applied to a pointer. It is only possible to overload operators when at least one of the arguments is of user defined type (or a reference to it, but not a pointer); in this particular case the arguments are String* and 2, both fundamental types.
What you may do is drop the pointer altogether, I don't see why you need it:
String moj("Alice has a cat");
// cout<<(*moj)[2]; <-- now this doesn't work
cout<<moj[2]; // <-- but this does
String * means a pointer to a String, if you want to do anything with the String itself you have to dereference it with *moj. What you can do instead is this:
String moj = String("Alice has a cat"); // note lack of * and new
cout << moj[2];
Also note that anything you allocate with new needs to be deleted after:
String *x = new String("foo");
// code
delete x;