C ++ implementation class String -- including constructor and overload & gt;, & lt;, & gt;, & lt;, =, string -- Constructor

Source: Internet
Author: User

C ++ implementation class String -- including constructor and overload >>,<<>,<,==,=, string -- Constructor

Write String-like constructor, destructor, and value assignment function.

The input (>), output (<), greater than (>), less than (<), value assignment (=), and equal to (=) operators are overloaded.

For space reasons, I did not write about the implementation of strlen () and strcpy () functions.

// String class // strlen and strcpy functions # include <iostream> # include <string> using namespace std; class String {public: string (const char * str = NULL); // construct a common function String (const String & other); // copy the constructor ~ String (void); String & operator = (const String & other); // overload = friend bool operator = (const String & s1, const String & s2 ); // reload = friend bool operator> (const String & s1, const String & s2); // reload> friend bool operator <(const String & s1, const String & s2); // reload <friend ostream & operator <(ostream &, const String & other ); // reload <friend istream & operator> (istream &, String & other); // reload> private: char * m_data;}; String: Str Ing (const char * str) {if (str = NULL) {m_data = new char [1]; m_data = '\ 0 ';} else {int length = strlen (str); m_data = new char [length + 1]; strcpy (m_data, str) ;}} String ::~ String (void) {delete [] m_data;} String: String (const String & other) {int length = strlen (other. m_data); m_data = new char [length + 1]; strcpy (m_data, other. m_data);} String & String: operator = (const String & other) {if (& other = this) return * this; delete [] m_data; int length = strlen (other. m_data); m_data = new char [length + 1]; strcpy (m_data, other. m_data); return * this;} ostream & operator <(ostream & output, const String & other) {Output <other. m_data; return output;} istream & operator> (istream & input, String & other) {char p [256]; input> p; int length = strlen (p ); other. m_data = new char [length + 1]; strcpy (other. m_data, p); return input;} bool operator = (const String & s1, const String & s2) {int len_s1 = strlen (s1.m _ data ); int len_s2 = strlen (s2.m _ data); if (len_s1! = Len_s2) return false; int I = 0; while (s1.m _ data [I] = s2.m _ data [I]) {I ++; if (I = len_s1) return true;} return false;} bool operator> (const String & s1, const String & s2) {int len = strlen (s1.m _ data)> strlen (s2.m _ data )? Strlen (s2.m _ data): strlen (s1.m _ data); for (int I = 0; I <len; I ++) {if (s1.m _ data [I]> s2.m _ data [I]) {return true;} else if (s1.m _ data [I] = s2.m _ data [I]) {continue;} else {return false;} if (strlen (s1.m _ data)> strlen (s2.m _ data) return true; elsereturn false ;} bool operator <(const String & s1, const String & s2) {int len = strlen (s1.m _ data)> strlen (s2.m _ data )? Strlen (s2.m _ data): strlen (s1.m _ data); for (int I = 0; I <len; I ++) {if (s1.m _ data [I]> s2.m _ data [I]) {return false;} else if (s1.m _ data [I] = s2.m _ data [I]) {continue;} else {return true;} if (strlen (s1.m _ data) <strlen (s2.m _ data) return true; elsereturn false;} int main () {// test String Input and Output String s; cin >>> s; cout <"s:" <s; cout <endl; // test the constructor and '= 'string a = "1234"; String B (a); // copy the constructor to initialize String c; c =; // value assignment statement "=" cout <"a:" <a <endl; Cout <"B:" <B <endl; cout <"c:" <c <endl; // test '=' if (s = B) cout <"s = B" <endl; elsecout <"s! = B "<endl; // test '>'' <' if (s> B) cout <"s> B" <endl; else if (s <B) cout <"s <B" <endl; elsecout <"s = B" <endl; system ("pause "); return 0 ;}


 


In java, there are multiple constructor in a class, or the overload of constructor in a class. What is the purpose?

One of the advantages of Java is polymorphism (reload, rewrite). You can easily cope with various requirements in the program by doing so. I will not give an example. I am also a newbie, so wait for the answer. Try again

4. What is the role of the constructor? What is the overload of the constructor?

A constructor is used to instantiate an object. When you create an object, it calls its constructor.
You can write multiple constructors with different parameter types and numbers. It is the same as the normal function overload.
Class Person {
Private String id;
Private String name;
Public Person (){
// Default
}
Public Person (String id, String name ){
This. id = id;
This. name = name;

// Default
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.