Todo programador de C++ sabe que la librería standard tiene una clase string. Pero, mientras se está aprendiendo, es una buena idea saber cómo poder desarrollar tu propia clase string.
Esa es la razón por la que hice la clase String. No está pensada para proyectos profesionales (para ellos, deberías utilizar la string de la librería standard), sino como una ayuda para aprender C++.
class String
{
char *m_pszString; //Allocated buffer
int m_nAllocated; //Allocated length
public:
//Construction and destruction
String() { /* ... */ }
~String() { /* ... */ }
//Copy constructors
String(const char *pszString) { /* ... */ }
String(const String& rsString) { /* ... */ }
//Operators (assignment)
String& operator=(const char *pszString);
String& operator=(const String& rsString) { /* ... */ }
//Operators (concatenation)
String& operator+=(const char *pszString);
String& operator+=(String& rsString) { /* ... */ }
String operator+(String rsString);
//Operators (comparison)
bool operator<(String sString) { /* ... */ }
bool operator<=(String sString) { /* ... */ }
bool operator>(String sString) { /* ... */ }
bool operator>=(String sString) { /* ... */ }
bool operator==(String sString) { /* ... */ }
bool operator!=(String sString) { /* ... */ }
//Operations
void Clear();
String Lower() { /* ... */ }
String Upper() { /* ... */ }
//Information
int Length() { /* ... */ }
//Cast operators
operator const char*() { /* ... */ }
protected: //Helper functions
/* ... */
};
//Output e input
std::ostream& operator<<(std::ostream& oStream, String& rsString);
std::istream& operator>>(std::istream& iStream, String& rsString);
Este es un proyecto de ejemplo hecho para explicar la utilización de la clase String.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "String.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "String sample project" << endl;
cout << "---------------------" << endl << endl;
String a("This"), b("is"), c("a"), d("test");
String e = a + " " + b + " " + c + " " + d;
cout << "String parts:" << endl;
cout << '\t' << a << endl;
cout << '\t' << b << endl;
cout << '\t' << c << endl;
cout << '\t' << d << endl << endl;
cout << "Concatenated with spaces:" << endl;
cout << '\t' << e << endl << endl;
String f, g;
cout << "Please input a string (empty to break): ";
cin >> f;
cout << "Please input other string (empty to break): ";
cin >> g;
cout << endl;
cout << "1st string (" << f << ") is " << f.Length() << " chars long." << endl;
cout << "2nd string (" << g << ") is " << g.Length() << " chars long." << endl;
cout << endl;
cout << "1st string in lowercase is " << f.Lower() << endl;
cout << "1st string in uppercase is " << f.Upper() << endl << endl;
cout << "2nd string in lowercase is " << g.Lower() << endl;
cout << "2nd string in uppercase is " << g.Upper() << endl << endl;
cout << "1 < 2\t=> " << (f<g?"true":"false") << endl;
cout << "1 <= 2\t=> " << (f<=g?"true":"false") << endl;
cout << "1 > 2\t=> " << (f>g?"true":"false") << endl;
cout << "1 >= 2\t=> " << (f>=g?"true":"false") << endl;
cout << "1 == 2\t=> " << (f==g?"true":"false") << endl;
cout << "1 != 2\t=> " << (f!=g?"true":"false") << endl << endl;
if (f == g)
cout << "Strings are equal." << endl;
else
cout << "Strings are not equal." << endl;
cout << endl;
cout << "1+2\t=> " << f+g << endl;
cout << "2+1\t=> " << g+f << endl;
cout << endl;
return 0;
}
Y esta su salida:
String sample project
---------------------
String parts:
This
is
a
test
Concatenated with spaces:
This is a test
Please input a string (empty to break): Hello, string 1
Please input other string (empty to break): Phrase 2
1st string (Hello, string 1) is 15 chars long.
2nd string (Phrase 2) is 8 chars long.
1st string in lowercase is hello, string 1
1st string in uppercase is HELLO, STRING 1
2nd string in lowercase is phrase 2
2nd string in uppercase is PHRASE 2
1 < 2 => false
1 <= 2 => false
1 > 2 => true
1 >= 2 => true
1 == 2 => false
1 != 2 => true
Strings are not equal.
1+2 => Hello, string 1Phrase 2
2+1 => Phrase 2Hello, string 1
El código es completamente portable.
Ha sido desarrollado, compilado y testeado utilizando wxDev-C++ para Windows con el compilador MinGW (incluído en el paquete). En Linux, fue compilado utilizando el compilador GNU GCC.
String está liberado bajo la licencia GNU GPL v3 (attached)…
Ahora los links de descarga:
Tu ayuda es valorada!
Todo el contenido ofrecido en este sitio, a menos que se indique lo contrario, es de naturaleza libre. Esto significa que podés compartirlo siempre y cuando lo hagas en forma gratuita y aclarando la fuente.
Si te resultó útil y querés contribuir, podés hacer una donación o, al menos, visitar alguno de nuestros patrocinadores de tu elección; están por todo el sitio.
Incoming search terms for the article:
- open source c string class
- clase string c
- simple string class
- string c
- simple string class c
- C String Class Source Code
- la clase string c
- simple c string class
- clase cadena c
- clase string en c
Related posts:
0 Response to “Clase String simple para C++”