|
-
Dec 5th, 2000, 06:24 AM
#1
Thread Starter
transcendental analytic
I'm a newbie here, when it comes to C++, but i know how to make functions that overloads each other. I've heard you can overload operators too for isntance
Code:
Cls1 Obj1;
Cls2 Obj2;
Obj1 = Obj2;
Obj1 = Obj1 + Obj2 / Obj1 * Obj1;
Obj1 += Obj2;
how do i achieve this?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 5th, 2000, 06:45 AM
#2
Frenzied Member
Maybe you can use this:
Code:
Listing 10.8. Overloading operator++.
1: // Listing 10.8
2: // The Counter class
3:
4: typedef unsigned short USHORT;
5: #include <iostream.h>
6:
7: class Counter
8: {
9: public:
10: Counter();
11: ~Counter(){}
12: USHORT GetItsVal()const { return itsVal; }
13: void SetItsVal(USHORT x) {itsVal = x; }
14: void Increment() { ++itsVal; }
15: void operator++ () { ++itsVal; }
16:
17: private:
18: USHORT itsVal;
19:
20: };
21:
22: Counter::Counter():
23: itsVal(0)
24: {};
25:
26: int main()
27: {
28: Counter i;
29: cout << "The value of i is " << i.GetItsVal() << endl;
30: i.Increment();
31: cout << "The value of i is " << i.GetItsVal() << endl;
32: ++i;
33: cout << "The value of i is " << i.GetItsVal() << endl;
34: return 0;
35: }
Output: The value of i is 0
The value of i is 1
The value of i is 2
-
Dec 5th, 2000, 01:09 PM
#3
Thread Starter
transcendental analytic
Thanks, i appreciate that much Can you explain how i would go doing these operations i mentioned?
Code:
15: void operator++ () { ++itsVal; }
so if this works for Obj1++ then how do i get Obj1 = Obj1 + Obj2; to work?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 5th, 2000, 01:45 PM
#4
Frenzied Member
An Example, What my book says....
#include <iostream.h>
class number
{
public:
int n;
number() {n = 1;}
number operator+ (number);
};
number number: perator+ (number N)
{
cout << "\noperator+() function is working ";
number T;
T.n = N + N.n;
return T;
}
void main()
{
number x,y,z;
Z = X.operator+(Y) //Explicit
cout << Z.n; //2
Z = X + Y; //Implicit
cout << Z.n; //2
Z = Z + X + Y; //Chain of calls
cout << Z.n; //4
}
-
Dec 5th, 2000, 01:53 PM
#5
Sorry for the post, but...
Originally posted by kedaman
I'm a newbie here, when it comes to C++
Join the party kedaman.
For C++, we have to look to parksie, vlatko, and a few others .
I wish C++ coding was as easy as VB.
I'm still learning..still reading the tutorials at http:///www.cprogramming.com .
-
Dec 5th, 2000, 01:55 PM
#6
Thread Starter
transcendental analytic
Thanks, Psy, very nice of you helping me thanks for the tutorial Matt i'll have a look at it. Maybe i should add parksie to my ICQ list?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 5th, 2000, 05:22 PM
#7
Frenzied Member
Here is a sample of how to overload the + operator.
Code:
1: // Listing 10.14
2: //Overload operator plus (+)
3:
4: typedef unsigned short USHORT;
5: #include <iostream.h>
6:
7: class Counter
8: {
9: public:
10: Counter();
11: Counter(USHORT initialValue);
12: ~Counter(){}
13: USHORT GetItsVal()const { return itsVal; }
14: void SetItsVal(USHORT x) {itsVal = x; }
15: Counter operator+ (const Counter &);
16: private:
17: USHORT itsVal;
18: };
19:
20: Counter::Counter(USHORT initialValue):
21: itsVal(initialValue)
22: {}
23:
24: Counter::Counter():
25: itsVal(0)
26: {}
27:
28: Counter Counter::operator+ (const Counter & rhs)
29: {
30: return Counter(itsVal + rhs.GetItsVal());
31: }
32:
33: int main()
34: {
35: Counter varOne(2), varTwo(4), varThree;
36: varThree = varOne + varTwo;
37: cout << "varOne: " << varOne.GetItsVal()<< endl;
38: cout << "varTwo: " << varTwo.GetItsVal() << endl;
39: cout << "varThree: " << varThree.GetItsVal() << endl;
40:
41: return 0;
42: }
Output: varOne: 2
varTwo: 4
varThree: 6
-
Dec 5th, 2000, 05:56 PM
#8
Monday Morning Lunatic
Maybe i should add parksie to my ICQ list?
If you want. I'm 91747330
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 5th, 2000, 08:34 PM
#9
Thread Starter
transcendental analytic
Well good, now i have someone who i can ask anything about
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 6th, 2000, 06:55 PM
#10
Monday Morning Lunatic
Well...C++ related anyway
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 8th, 2000, 09:59 AM
#11
Thread Starter
transcendental analytic
Well, here's something related, about inheritance 
Is it possible for a class to inherit two other classes?
I was thinking about letting a windowcomponent inhering both visualcomponent and a scriptcomponent.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 8th, 2000, 03:01 PM
#12
Monday Morning Lunatic
Most things are possible. That of course depends on whether your brain explodes while debugging it .
Code:
class MyClass : public WindowClass, public ScriptClass {
MyClass() { }
virtual ~MyClass() { }
int WindowClassFunction() { } // Overload from WindowClass
int ScriptClassFunction() { } // Overload from ScriptClass
};
You also inherit any data members. Although I think you get an error (maybe a warning) if two classes you inherit from have two identical members.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 8th, 2000, 07:28 PM
#13
Thread Starter
transcendental analytic
Thanks parksie, that was i was hoping for
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|