operators

hi, I understand basic programming (understand classes, functions, etc.)
something i don't get however is operators
i know you can't create new ones but can't you use ones already in place in a different way
for example:
1
2
3
//i'll skip the beggining stuff
         ztring mystring; //creates a variable of type 'ztring' (class)
         cin>>mystring; //how do i tell the program what to do, can I? 


any help will be appreciated
You use operator overloading.

For Example::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Employee
{
     public:
 

    private:
    std::string firstName;
    std::string lastName;
    int  eID;

    friend  std::istream&  operator >> (std::istream& in,  Employee & e);
    friend  std::ostream&  operator << (std::ostream& os,  Employee & e);
};

 std::istream& operator >> (std::istream& in, Employee& e)
{
    std::cout << "Enter Employee Details - firstname, lastname and ID number seperated by spaces" << endl;
    in >> e.firstName >> e.lastName >> e.eID;
    return in;
}


 std::ostream& operator << (std::ostream& os, Employee& e)
{
      os << e.firstName << "    " << e.lastName << "   " << e.eID << endl;   
       return os;
}


int main()
{
	Employee e;
	cin >> e;	 
	cout << e;
}


You can have all sorts of operator overloads for your class;

So read up further on Operator Overloading
Last edited on
thanks a ton! that really helped me!
Now, can you tell me what's wrong with this:
the '.h':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
using namespace std;
class ztring
{
public:
    char buffer[1000];
    char useless;
    int count;
    ztring();
    ~ztring();
    void getline ();
    friend std::istream& operator >>(std::istream& in, ztring & z);
    void putline ();
    friend std::ostream& operator <<(std::ostream& on, ztring & z);
    friend ztring& operator += (ztring& z,string& s);
    friend ztring& operator + (ztring& z,string& s);
    friend ztring& operator = (ztring& z, ztring& x);//it says this needs to be a non-static 
    char at(int);                                                       //member
    int length();
    void replace(int, char);
    void replace(int, string);
    void add(char);
    void add(string);
};

this is the cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "ztring.h"
#include <iostream>
#include <string>
ztring::ztring()
{
     count=0;
}
void ztring::getline ()
{
     count=0;
     int counted(0);
     while (counted<1000)
     {
     buffer[count]=cin.get();
     if (!(counted==0&&buffer[count]=='\n'))
     {
     if (buffer[count]=='\n')
     {
          count--;
          break;
     }
     }
     else{
     count--;}
     count++;
     counted++;
     }
}
void ztring::putline ()
{
     int counter;
     counter=0;
     while (counter<=count)
     {
           cout<<buffer[counter];
           counter++;
     }
}
char ztring::at(int num)
{
     if (buffer[num-1])
     {
           return buffer[num-1];
     }
     else
     throw "invalid";
}
int ztring::length()
{
    return count+1;
}
void ztring::replace(int num, char c)
{
     if (buffer[num-1])
     {
          buffer[num-1]=c;
     }
     else
     throw "invalid";
}
void ztring::replace (int num, string s)
{
     num--;
     int counter(0);
     while (s[counter])
     {
           buffer[num]=s[counter];
           num++;
           counter++;
     }
     if (num>count)
     {
                   count=num;
     }
}
void ztring::add(char c)
{
     buffer[count+1]=c;
     count++;
}
void ztring::add (string s)
{
     int counter(0);
     while (s[counter])
     {
     buffer[count+1+counter]=s[counter];
     counter++;
     }
     count+=counter;
}
ztring::~ztring ()
{
                count=0;
}
std::istream& operator >> (std::istream& in, ztring& z)
{
     z.getline ();
}
std::ostream& operator << (std::ostream& on, ztring& z)
{
     z.putline ();
}
ztring& operator += (ztring& z,string& s)
{
    z.add(s);
}
ztring& operator + (ztring& z,string& s)
{
    z.add(s);
}
ztring& operator = (ztring& z, ztring& x)//same problem down here
{
    int *count=new int;
    *count=0;
    while (count<x.count)
    {
        z.buffer[count]=x.buffer[count];
        count++;
    }
    delete count;
}
operator= has to be a member of the class; it cannot be a friend.

You have some const correctness issues as well. Namely, a friend operator+ should
take both arguments by const reference; operator<< should take the string by
const reference; operator= should take the rhs as a const reference.

operator+= should be a member.

And you don't need a destructor (the default one is sufficient).
dude, thanks! i realized what i did wrong, thanks anyway!
Topic archived. No new replies allowed.