Operator

T

titi

Hi! How to right the C++ program that initialize the name with null
value and with some specific string?
and how to overload the + operator to concatenate two object and == to
check 2 objects are equal.

Thank.
 
R

Rolf Magnus

titi said:
Hi! How to right the C++ program that initialize the name with null
value and with some specific string?

I have no idea what you're talking about. What name is supposed to be
initialized, and where?
and how to overload the + operator to concatenate two object and == to
check 2 objects are equal.

MyClass operator+(const MyClass& lhs, const MyClass& rhs)
{
//do the addition
}

bool operator==(const MyClass& lhs, const MyClass& rhs)
{
//do the comparison
}

In many cases (but not all), it's best to write an operator+= for your
class and implement operator+ using +=. Then operator+ becomes:

MyClass operator+(const MyClass& lhs, const MyClass& rhs)
{
return MyClass(lhs) += rhs;
}
 
T

Thomas Matthews

titi said:
Hi! How to right the C++ program that initialize the name with null
value and with some specific string?

How can one initialize a string with two values, NULL and a
a specific string?

#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;

string name_not_const("name not const");
string null_name;
const string const_name("constant name");

int main(void)
{
cout << '"' << name_not_const << '"' << '\n';
cout << '"' << null_name << '"' << '\n';
cout << '"' << const_name << '"' << endl;
return 0;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top