Home > Mobile >  How can we use '=' operator to assign value to our custom made datatype in c ?
How can we use '=' operator to assign value to our custom made datatype in c ?

Time:12-28

I have created a basic string class and i want to assign it some value but the problem is that I have to do something like this... string str("Hello"); Isn't there a way so i can do it something like.... string str = "Hello"; Like how c std:: defined types (vector,string etc) do?

And also I want that instead of typing std::cout << str.val() I can do std::cout << str; to access its value and same for modifying (or updating) it.

#include<iostream>
class string
{
  private:
  char* str; // An uninitialized string
  public:
  string(char* text) // Constructor which takes a string
  {
    str = text;
  }
  char* val() // used to access the value stored in String
  {
   return str;
  }
  void update(char* string2) // used to update the value of string
  {
    str = string2;
  }
};

int main()
{
  string myStr("Hello World\n"); //initializes a string object
  std::clog << myStr.val();

  myStr.update("Bye World\n"); //updates the value of myStr
  std::clog << myStr.val();
}

Thanks to whoever answers....

CodePudding user response:

You need to implement these operators ('=' and '<<') manually. See https://en.cppreference.com/w/cpp/language/operators

CodePudding user response:

you can do this using overloaded operators

string& operator= ( const string & );

string& operator= ( const char *);

CodePudding user response:

Here are operator= and operator<< for the string class.

Also changed the member function signatures from char* to char const (&text)[N] to be suitable for passing in C string literals and avoid pointer decay.

#include <cstddef>
#include <iostream>

using std::cout;
using std::ostream;
using std::size_t;

// /!\ non-owning, non-managing.
// Borrows the parameter for the string object's lifetime.
// Intended to wrap C string literals.
// (More of a "view" object for the parameter, rather than a "smart" object.)
class string {
    char const* str;
public:
    template <size_t N>
    string(char const (&text)[N]) : str{text} { }

    auto val() const -> char const* { return str; }

    template <size_t N>
    auto operator=(char const (&text)[N]) -> string& {
        str = text;
        return *this;
    }

    friend auto operator<<(ostream& out, string const& s) -> ostream& {
        return out << s.str;
    }
};

int main() {
    string myStr = "Hello World\n";
    cout << myStr;

    myStr = "Bye World\n";
    cout << myStr;
}

CodePudding user response:

You can Overload the operators = and << like Below

  1. Overloading the = Operator
    string& operator = (char * text)
    {
       this->str=text;
       return *this;
    }
    
  2. Overloading the << Operator
    friend std::ostream& operator &lt;&lt; (std::ostream& outputstream,string& thestr)
    {
        outputstream&lt;&lt;thestr.val();
        return outputstream;
    }
    

So the Overall will be like below

#include<iostream>
class string
{
  private:
  char* str; // An uninitialized string
  public:
  string(char* text) // Constructor which takes a string
  {
    str = text;
  }
  char* val() // used to access the value stored in String
  {
   return str;
  }
  void update(char* string2) // used to update the value of string
  {
    str = string2;
  }

  string& operator = (char * text)
  {
    this->str=text;
    return *this;
  }

  friend std::ostream& operator << (std::ostream& outputstream,string& thestr)
  {
    outputstream<<thestr.val();
    return outputstream;
  }

};

int main()
{
  string myStr="Hello World\n"; //initializes a string object
  std::clog<<myStr;

  myStr="Bye World\n"; //updates the value of myStr
  std::clog << myStr;
}
  • Related