Duoas 1,025 Postaholic Featured Poster

A function is a function, whether it is a method or not. You must call it correctly, and in the right place:

Odometer simulator;
  int number_of_miles_driven;

  cout << "Please enter the amount of miles driven : " << endl;
  cin >> number_of_miles_driven;

  simulator.input_miles(number_of_miles_driven);

Notice how I changed the name of the variable to "number_of_miles_driven"? This is to remind you that the name of the variable you use doesn't have to be the same as the name of the function's parameter (which I didn't change from "miles_driven").

You seem to be a little confused about how your class is going to work...

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Are you talking about this kind of thing?

A number's representation is limited only by the number of digits used by the radix. We use decimal, meaning radix = 10: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Hexidecimal is radix 16: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (without regard to case).

I don't know how you plan to do base 100. The whole point of base-n encoding is to protect text against "special" numbers. Typically this works because the target radix is sufficiently smaller than the source. For email, the source is ASCII: 7-bits or 128 values. The target in base 64 is, amazingly enough, 64 values. These values can then be represented inside the source set using characters that are known not to have special meaning, 0..9, A..Z, a..z, and two others (10+26+26+2=64). (Which two others varies by implementation.)

Converting between bases is easy enough: all you need is a lookup table for each digit. In decimal the LUT would be "0123456789". '0' is element 0. '1' is index 1. etc.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

I know nothing about SunSparc assembly, but I think your problem is in mixing data types.

When the user inputs a number, like 1230, that's four ASCII characters: 49, 50, 51, and 48.

So, you need to decide what kind of numbers you intend to add: binary or ASCII? (I think for your problem you might want to do the latter.)

There must be at some point, no matter which way you do it, conversion between the ASCII representation (received from the user and printed out when done) and something that can be added.

If you do the ASCII one digit at a time, you only need to remember a carry flag each addition:
1224
2199

start with carry of 0

'4'-'0'= 4
'9'-'0'= 9
4+9+carry=13: carry is 1, result is 3
3+'0'= '3'

'2'-'0'= 2
'9'-'0'= 9
2+9+carry=12: carry is 1, result is 2
2+'0'= '2'

'2'-'0'= 2
'1'-'0'= 1
2+1+carry=4: carry is 0, result is 4
4+'0'= '4'

etc.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Heh, nice job. Sorry I goofed in my example and left the ptMaxSize part out: ((LPMINMAXINFO)lParam)->ptMaxSize.x Good job on picking that up! Glad you got it working.

Duoas 1,025 Postaholic Featured Poster

OK, according to your problem description I have a few comments to help...

But FIRST, please take the time to be careful about how you "format" your code. You've made some indentation errors (that is, because of the way you have indented your code you have made some errors!)

text file
OK, if the input file is a text file, why are you opening it in binary mode? Don't. std::ifstream in( "clients.txt" ); That's all you need.

Clearing the array should not be dependent on the file.

input
You cannot force input. According to the problem description, your input is guaranteed only to have the following form:

M|F 1 2 3 4 5 6 7 8 9 10 name ...

That is, the letter 'M' or 'F', followed by ten numbers, followed by one or more names. I would suggest that you don't need to care whether the client has both a first and last name, but you can always separate them later.

This input has a nice characteristic: all the fields at the beginning of the line are definite, while the last field is a free-form string. This means you can mix >> and getline() with impunity.

So input for each line (or client) might look something like this (needs more work):

// get the letter 'M' or 'F'.
std::cin >> sex;
// get the answers
for (i = 0; i < 10; i++) std::cin >> a[ i ];
// …
Killer_Typo commented: great post! +6
Duoas 1,025 Postaholic Featured Poster

I don't know anything about the CppUnit framework, but you should be able to compile just like any other.

A makefile has a pretty simple layout. I'll assume that you are compiling a program named "quux" from the source files "quux.cpp" and "baz.cpp" and linking the library "bar". A makefile for that using GCC might look something like this:

quux: quux.o baz.o
	g++ -o quux quux.o baz.o -lbar

quux.o: quux.cpp
	g++ -c quux.cpp

baz.o: baz.cpp
	g++ -c baz.cpp

clean:
	del quux.o
	del baz.o

The default target is "quux", the name of your exe. It requires "quux.o" and "baz.o", which also have rules. You can clean things up by targeting "clean" (this assumes Windows Cmd.exe --you'll have to adjust to the appropriate commands for your platform).

Presumably libbar.a is in GCC's ~\lib directory.

A make target starts at the beginning of the line, followed by a colon (:), followed by a list of required sources. The following line(s) must begin with a TAB, then the commands necessary to make the target.

So, to make "quux" you can type either make or make quux to compile it.

To just make "baz.o" (instead of the whole project) type make baz.o To clean up any mess from compiling (before archiving or just to make sure for a clean build): make clean This example is about as simple as it gets. If you want to play with a little more sophistication or if you just want to learn more …

Duoas 1,025 Postaholic Featured Poster

Just cast it the usual way:

LRESULT CALLBACK MyWndProc(
  HWND   hWnd,
  UINT   uMsg,
  WPARAM wParam,
  LPARAM lParam
  ) {
  if (uMsg == WM_GETMINMAXINFO) {
    cout << "Max Window Size is ("
         << ((LPMINMAXINFO)lParam)->x << ", "
         << ((LPMINMAXINFO)lParam)->y << ")"
         << endl;
    }
  return DefWindowProc( hWnd, uMsg, wParam, lParam );
  }

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

in order:
1.

if isDigit( my_char ) then
  begin
  curr.next := new_number_node( find_digits( my_char, my_file ) );
  curr := curr.next
  end;

2. Yes, "c" is the same as "my_char". (Sorry)

3. No. Pay attention to how I indented in my pseudo. You actually already wrote yourself a function to skip spaces (in your other thread).

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Hey, no problem. Glad to be of help.

The whole trick to programming is nitpicking to the nth degree. You have to think about things very exactly, and it takes a little while to be able to do that regularly. You'll do fine.

:)

Duoas 1,025 Postaholic Featured Poster

[[I][/I]code=Pascal[I][/I]]

writeln( h:0:1 );

[[I][/I]/code[I][/I]]

This prints a real value (h) with an indefinite number of whole digits (0) and only one fractional digit (1).

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

A 1D array is just a contiguous list of values.
A 2D array is just a contiguous list of 1D arrays.
I.e.

[0][1][2][3][4][5][6][7]  :row 1
[8][9][A][B][C][D][E][F]  :row 2
[0][1][2][3][4][5][6][7]  :row 3
[0][1][2][3][4][5][6][7]  :row 4
[0][1][2][3][4][5][6][7]  :row 5
[0][1][2][3][4][5][6][7]  :row 6
[0][1][2][3][4][5][6][7]  :row 7

That's how we think about it, with those nice line breaks between rows. However, it really looks like this in memory:

[0][1][2][3][4][5][6][7][8][9][A][B][C][D][E][F][0][1]...
 ^row 1                  ^row 2                  ^row 3

So to calculate a position as (row, col): beginning address +(row *num_columns) +col Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You've explained yourself very clearly.

In Windows, an application is only permitted to have one "console" attached at any given time. There are tricky ways around this, but those ways involve far more than you are ready to handle... besides which, it is a weird thing you want to do. (Though, admittedly, cool.)

You could "fake it" by having your application put up a Windows multi-line edit control for the output window. You can change the font and colors to match the console. This is as close to the same thing as you can easily get.

Do you have Win32 documentation? (You'll need it for this!)


Keep in mind that console processes aren't supposed to be aware of "windows" and "console windows" and the like. They are only supposed to understand input and output streams. How the streams are associated with the program is supposed to be entirely up to the person using the program. What I am saying is that you are trying to write a Windows application that just happens to use the Windows Console.

Hope this makes sense, and helps.

Duoas 1,025 Postaholic Featured Poster

What do you mean by "output window"?

If you mean some TTY (like the console) then the answer is: yes, it is possible.
If you mean some other thing then the answer is: maybe.

Either way, you're going to have to be dinking around with things in an OS-specific way.

A better question is why on earth you want to duplicate your output to multiple <something>s?

(BTW. If you are using Unix or Linux, type "man tee".)

Duoas 1,025 Postaholic Featured Poster

int : integer
(int *) : pointer to integer
(int *)& : reference to pointer to integer

So, somewhere you have a pointer to an integer. int *p; Your function wants to play with it. findMax( ..., p ); Now p points to the largest integer in your array of integers. cout << "The largest is " << *p << ".\n"; Hope this helps.

Duoas 1,025 Postaholic Featured Poster

If your assignment was to do it in a high level language then why play with assembly?

The fastest way to convert would be to tell the compiler to produces assembly source instead of a binary image. If you are using the GCC: g++ -S -fverbose-asm foo.cpp Other compilers will vary.

BTW. Good luck. C++ is pretty darn high level.

Duoas 1,025 Postaholic Featured Poster

You are close.

A linked list always has three things:
1. a record with a "next" pointer.
2. a "head" pointer pointing to the first record in the linked list.
3. the last record in the linked list's "next" pointer is nil.

Your main loop should look something like this:

read( my_file, my_char );
while not eof( my_file ) do[/inlinecode]
    while c is a space do skip spaces
        if c is in '0'..'9' then
            read the number and stick it in a new node
            which you append to the end of the linked list.
    else if c is an operator then
        read the operator and stick it in a new node
        which you append to the end of the linked list
    else error (because it's not a number, space, or valid operator)
        loop to 2

I would make a couple of functions that create a new node. They might be named:

function new_number_node( num: integer ): pEquation;

function new_operator_node( oper: char ): pEquation;

Then in your loop all you have to do is call the appropriate function to make life easy.

That should be enough for you to think about for now...

Duoas 1,025 Postaholic Featured Poster

Absolutely. In fact, I'm pleased you remembered it.

You'll still have to know how to tell a number from an operator. How about:

tequation = record
            kind     : (i_am_a_number, i_am_an_operator);
            num      : integer;
            oper     : char;
            next     : pEquation;
            end;

Thereafter, you can know whether or not your tEquation contains a number if node^.kind = i_am_a_number then ... or an operator.

When you are reading your equation into the linked list, you can set a node's type similarly: new_node^.kind := i_am_an_operator; Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Yes, good job. The only question is: how can you tell the difference between a node that contains a number and a node that contains an operator?

(Have you learned about variant records?)

Also, is it possible that the numbers in the equation will ever have fractional parts? (Say, what if it were "56 - 12/5 +1 * 5" ?)

Duoas 1,025 Postaholic Featured Poster

All the information you need is in the second post of this thread. Please read it.

Duoas 1,025 Postaholic Featured Poster

You don't have to be a professional. You only need to pay attention. So far you have not.

When writing a post that contains code, place all your code in code blocks:

[[I][/I]code=Pascal[I][/I]]
program fooey;
begin
writeln( 'fooey' )
end.
[[I][/I]/code[I][/I]]

becomes:

program fooey;
begin
writeln( 'fooey' )
end.

Next, don't start a new thread for every post you make. If you are still working on a problem from a previous thread then reply to that thread.

Now, I've asked you several times to explain yourself better. You've not done that either. However, at this point I think I understand:

You have three image components (band1, band2, band3) which you wish to animate together by making one visible, then another, and then another, in succession, where only one is visible at any given time.

You only need one timer component. (Again, the documentation would have given you a very good example of what follows):

TForm1.Timer1Timer( Sender: TObject );
  begin
  if band1.visible then 
    begin
    band1.visible := false;
    band2.visible := true;
    //wait 4 sec before displaying band3
    timer1.interval := 4000
    end
  else if band2.visible then
    begin
    band2.visible := false;
    band3.visible := true;
    //wait 6 sec before displaying band1
    timer1.interval := 6000
    end
  else // band3 is visible
    begin
    band3.visible := false;
    band1.visible := true;
    //wait 2 sec before displaying band2
    timer1.interval := 2000
    end
  end;

Initially band1, band2, and band3 are all not visible, and Timer1.enabled = false. To start your animation:

Duoas 1,025 Postaholic Featured Poster

That's because you never actually uninstalled it. Press F1 and index "Installing Component Packages" to learn more.

In a nutshell, there are two things you must watch for: the Delphi Library Path, and the available component list. Things on the component palette are chosen from the available component list, but not every available component need be on the Component Palette.

Also, never compile a component into the Delphi Component Library Package, unless, you are absolutely certain that it is bug-free and completely finished. Even so, don't do it.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Ah well, sorry I didn't see this sooner. If it is due today then there's not much more to do here.

If all you wanted to do was store your equation as a string (an array of characters), then we've gone in loops for the wrong assignment.

All the stuff you've done helps, and will help you in your next assignment especially...

Duoas 1,025 Postaholic Featured Poster

You don't need to do anything to the socket. (It should only be open in one thread.)

You need to create a mutex on the buffer, so that it is only accessed by one thread at a time.

Duoas 1,025 Postaholic Featured Poster

That's too vague. Is it an array of integers? Is it an array of strings? Is it an array of reals? Chars? Enumerations? Sets?

Duoas 1,025 Postaholic Featured Poster

Alright. Since your equation is to be stored in an array, and you are not permitted to store it in an array of records, then how are you supposed to store it? What does he want you to use for each element of the array?

Duoas 1,025 Postaholic Featured Poster

Hold on. The entire length of this thread you've maintained that your professor wants you to use an array.

"Every time an intermediate result is calculated the corresponding components of that calculation are removed from the array(s) and replaced with the correct answer".

Did he or did he not? Which is it?

Duoas 1,025 Postaholic Featured Poster

rc.exe is a program that comes with Visual Basic.

If you are using Delphi/CodeGear you should look for brcc32.exe or something similar.
If you are using Free Pascal or GNU Pascal, look for windres.exe.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

How did your professor say you should define your array?

Duoas 1,025 Postaholic Featured Poster

There are two pieces of information you need to keep track of:
- where am I in the string?
- how many '('s have I found?

You start out at the first character in the string, with zero '('s found so far.

I suggest you make your function recurse when it finds a '('.

Hope this helps.

[EDIT] Yes, how do you know you are at the end of the string?
In C, the string ends at the first character with a value of zero (not '0', which is a value of 48).

Duoas 1,025 Postaholic Featured Poster

You've asked one of those questions that starts flame wars. (Personally, I like Pascal.)

The real answer is: it doesn't really matter.

You might choose one language over another based on what you have in mind to do with it. But if you just want to learn programming then pick one you think you'll like and start there. Good choices to begin with include C++, Python, Pascal, and SmallTalk. These are all imperative, object-oriented, mainstream languages with GUI capabilities and RAD environments.

The C and C++ forums on this board are the most active, so C++ might be a good choice if you want help here. So would Python. I and a couple of other people here can help you in Pascal, but there are other places with dedicated Pascal and SmallTalk and etc. forums you'd want to find.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

(Sorry to respond so late. Dinner called.)

But it didn't complain about "reset"? Sounds like you are using BORLAND mode.
I used an Object Pascal thing, sorry:

instead of "assignFile" say "assign"
instead of "closeFile" say "close"

And make sure you give your string a length var s: string[ 255]; If this stuff doesn't work then you are using ISO Pascal mode, and handling files is entirely different. Let me know.

Duoas 1,025 Postaholic Featured Poster

<cstring> aka <string.h> is not more recent. It is the old C string stuff.

The only thing that is recent about it is the ability to refer to it as <cstring> instead of <string.h>.

Duoas 1,025 Postaholic Featured Poster

OK, I've had time to think it over.

Your post #50 is a good plan. You haven't "wasted" any time. You've learned a good deal and what you have learned directly applies to your assignment.

1. read from input file
2. Put the equation from input file into an array

These things can be done together. You have already developed a loop that gets numbers and operators from a file. The only thing you haven't done is considered the possibility that two operators may be next to each other (legally). My post #38 gives you both valid and invalid examples.

As a hint, consider what would happen if you called "find_digits" when c was not a digit. Your routines to read operators and skip spaces and the like should do the same thing.

3. use a procedure to get rid of spaces in the equation (if any)

Don't bother. Spaces can be filtered out when reading from file into an array.

4. another procedure to evaluate brackets

Brilliant! I suggest that the procedure that evaluates brackets be the same procedure that evaluates an equation. procedure evaluate( equ: tEquationArray; len: cardinal; start, stop: cardinal ); This procedure takes an equation array, its length, and what part of the array is to be evaluated. So, given an equation like 0 1 2 3 4 5 6 5 * ( 2 + 3 ) you can first call with: evaluate( ..., 7, 0, 6 ); Since the …

Duoas 1,025 Postaholic Featured Poster

You have still failed to stick your code in [[I][/I]code[I][/I]] blocks, so that it is not a nightmare to read.

May I ask what exactly it is you are doing that requires so many components and timers all on one form? Your program is going to work like an elephant in a chicken coup.

To use a timer, just read your TTimer documentation. It gives very nice examples of how to use it.

Duoas 1,025 Postaholic Featured Poster

It depends. In this case I want an exception to occur, because that would mean he mis-programmed his form, and it will let him know if he does.

That is, it is an inviolate part of his design that the checkbox reference a specific component. This is easily done in the forms designer. Failure to do this causes a runtime exception when testing each button.

If you are aiming to find a component based on some other criteria, then by all means you must validate the result, since it is no longer a design assertion but a very possible runtime error.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Er, you are using FreePascal as the compiler, right? (Not GNU Pascal?)

To open a text file, and print every line in it prefixed by the line number:

{$I-}
var
  t: textFile;
  s: string;
  i: integer;
begin
  assignFile( t, 'fooey.txt' );
  reset( t );
  if IOResult <> 0 then
    begin
    writeln( 'fooey' );
    halt( 1 )
    end;

  i := 1;
  readln( t, s );
  while not eof( t ) do
    begin
    writeln( 'line ', i, ': ', s );
    readln( t, s );
    inc( i )
    end;

  closeFile( t )
end.

If you are using GNU Pascal I think you can still use the above, but GPC tends toward the Extended Pascal syntax, which means that external file variables must be declared as bindable, and opened using different routines than reset and closeFile.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Please:

It would help you an awful lot if you get out a piece of graph paper and draw yourself one of these equations, then figure out how to find and handle each piece yourself.

Do that and you'll find an answer to a lot of your questions. The way you keep data in memory and the way you access that data is all up to you.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You are doing this in assembly?

The answer is "it depends."

It depends on how you have structured your program to work. At the most basic, simply keep a string (a memory buffer) somewhere that stores characters input from the socket. When you are ready to print, just print the buffer and reset it (clear it). If you are handling things in separate threads you'll also have to lock the buffer before printing and clearing.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Doublex, the whole point I and everyone else on this thread has tried to make to you is that strcpy() is not an unsafe function. It may be used unsafely, but if you are to toss it out the window altogether then how exactly do you intend to copy strings?

You can't compare it to gets() because gets() really is unsafe. Why? Because there is absolutely no way to enforce bounds checking on it. No bounds checking == unsafe.

The strcpy() function is bounds checked. It has a well-defined termination condition and the length of the destination is easily verified and bounded before you call the function.

Your argument about the size to error ratio is true, but it does not apply. Why? Because it is true no matter how "safe" you language of choice is. Programmers don't make errors because the library routines are out to get them. They make errors because they don't use the library routines correctly. No matter how huge your project, it is always possible to validate and use a function like strcpy() safely. If you find it difficult to do, then I suggest that the design of your code is in error.

Duoas 1,025 Postaholic Featured Poster

It is hard for a beginner, but not quite "way too hard".

Let me think a bit and post back later. My brain hurts and I'm tired of going around in circles with you.

Some quick things first:
1. Your last thoughts seem like a good plan.
2. Please get out some paper and draw how to accomplish each thing.
3. Please go talk to your prof.
4. See my post #7 for some big help. Just ignore the stuff about using records. Everything else applies.
5. temp_result := num1 causes them to both have the same value, which is not what I said. You don't need two variables with the same value. You need one variable that does what you are currently doing with two.

I'll post again in a day or after I've gotten some sleep.:yawn:

Duoas 1,025 Postaholic Featured Poster

Don't do anything. Just go to the next character. Remember, all you have to do is find matching ().

It would help you an awful lot if you get out a piece of graph paper and draw yourself one of these equations, then figure out how to find and handle each piece yourself.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

That's actually correct. A char * points to chars, not ints or anything else. So saying: int i = *s; causes the computer to go get the (eight-bit) char at *s, then assign the value to the int i.

If you want to get bigger numbers, you'll need to read two characters and stick them together:

i = (s[ 0 ] << 8) +s[ 1 ];

There are other ways to do this but the way I've described is safest. (There are a whole host of technical reasons why, including the width of a char, the endianness of your machine, the size of an int, etc.)

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

A string is way too large to hold in a single register.

Remember, a register holds the address of a string somewhere else in memory. So to concatenate two strings you need two addresses. Find the end of the target string (by modifying the address to point to the end), then copy the characters from the source addresses to the target addresses.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Just a quick look shows:

int main()
{
  ...
  int i;

  getData( userName, &i );  // correct, send the address of 'i'

  ...


void getData( char *userName, int *i )
{
  ...
  scanf( "%d", &i );  // oops. Now you get the address of the address of main's 'i'

I suggest you change your routines to indicate whether you are dealing with an address or not.

void getData( char *userName, int *ptr_to_i )
{
  ...
  scanf( "%d", ptr_to_i );

Notice how I removed that "&" from the front of "ptr_to_i". I already have the address of the int. (With the ampersand you get the address of the pointer to the int, which is wrong.) Make sense?

For just a quick look-over, that's all I see. If that doesn't fix it I or someone else will take a closer look at your code.

BTW. What happened to your nice [[I][/I]code[I][/I]] blocks? (Only use [[I][/I]inlinecode[I][/I]] for a single line of code.)

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Heh, nice catch Narue. :$
I suppose this is what I get for typing in code late at night without reviewing it...

You could fix it with a little note just in case anyone tries to come along and use it...

Doublex, I suggest you stay away from pointers altogether. I can shoot you in the leg with any routine you like...

(I've shot myself enough to know)

Duoas 1,025 Postaholic Featured Poster

No need to be so harsh...

However, mrb260478, you really are asking the impossible. It can't be done in one day. You'll need at least a week, assuming you are a genius. Three or four weeks if not.

I don't personally know of many online tutorials. I learned assembly from books. But I googled "learn assembly language" and got some pretty good hits.

Good luck.

Duoas 1,025 Postaholic Featured Poster

Sure, if you have enough room. For example, you can store two 8-bit values in a single 16-bit register. (By "string elements" do you mean "characters"?)

A better question, though, is why do you want to do this? It doesn't sound safe.

Duoas 1,025 Postaholic Featured Poster

Your teacher is having you implement the Binary GCD Algorithm in assembly? Yoinks.

Well, you can test any positive number for even or odd by looking at the LSB of the number.

# $s0 : number to check for odd/even
# $t0 : result: 1 = odd, 0 = not odd
andi $t0, $s0, 1

Use a branch instruction against $t0 immediately after to choose what to do.

In a high level language, that's the same as dividing by 2 and seeing if there is any remainder.

Hope this helps.

P.S. The Binary GCD Algorithm is a little complex. Make sure you have a good idea of what you want to do either in pseudocode or some high level language, then decompose each high-level statement down into the assembly instructions necessary to do it...

Duoas 1,025 Postaholic Featured Poster

Well, you know that the loop is reading the numbers correctly (because you before tested it by just reading and printing the numbers, and they printed the correct numbers).

But now that you are doing something to the numbers something is wrong.

May I suggest that num1 and temp_result should be the same variable?

Duoas 1,025 Postaholic Featured Poster

dubeyprateek
Don't be confusing, and don't give people gruff for nonsense.

A class is a type of thing. An object is an instance of that thing. A method is a function associated with a class (or type of thing), so objects of that type of thing (or class) can certainly be considered as "owning" a method. Remember, a class doesn't exist --but an object does.

Also, pay attention to the OP's question: she's not interested in calling methods "belonging to" the caller.

need_Direction
You call the method of any class by first specifying the object's name, then the method name. For example: myobject.dosomething(); This assumes that "myobject" has a parameterless method named "dosomething" associated with it. You can put such a statement anywhere that can see "myobject".

Often you can write a little program to test your hypothesis. Our hypothesis will be: object "B" can call a method of object "A".

#include <iostream>
using namespace std;

class myInt {
  public:
    char name;
    int i;
    myInt( char _name, int _i ): name( _name ), i( _i ) { }
    void print();
    void doSomething();
    void doMore( myInt &anInt );
  };

myInt A( 'A', 10 ), B( 'B', 42 );  // (1)

void myInt::print() {
  // Tell the world who I am and my value
  cout << name << ' ' << i << endl;
  }

void myInt::doSomething() {
  // Tell the world who A is (no matter who I am), and value
  A.print();
  }

void …
dubeyprateek commented: You really took time to explain this. +2