• Advertisement

Archived

This topic is now archived and is closed to further replies.

A Few Questions

This topic is 5993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have three questions I hope to get answers on. 1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex. 2. Why doesn't it work to write this in c++: int a = 10; int b[ a ]; it complains about that 'a' isn't a constant. 3. In java you have a variable called Objects that works on all variables, does that excist in c++ too? Edited by - delbogun on September 24, 2001 3:20:24 PM

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by delbogun
1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex.


In C/C++ you write like you did: 0x1d03 for example. This way of writing hexadecimal numbers is understood by some assemblers too. Another common way to write hex-numbers for assemblers is: 034ab0h etc...
(btw, the "ten form" as you called it is called "decimal")
quote:

2. Why doesn't it work to write this in c++:

int a = 10;
int b[ a ];

it complains about that 'a' isn't a constant.


C++ doesn't support statically allocated arrays whose size isn't known at compile time. If you know that 'a' will never change, then write:
    
// The 'const' keyword tells the compiler that the value

// of the variable 'a' must never change.

const int a = 10;
int b[ a ];


Or if the value of a is not known at compile-time (eg. if it depends on user input or something), then write it like:
   
int a = 10;
int * b = new int[a];
// In this case you must remember to delete[] 'b' once it is no longer needed.

delete[] b;

quote:

quote:
3. In java you have a variable called Objects that works on all variables, does that excist in c++ too?


No, not like in Java. In C++ you have templates that can work on a specific type of objects/types.

eg. If you want a vector containing integers you declare
   
#include <vector> // the std::vector template is defined here


std::vector<int> an_int_vector;
// ^^ this symbol means that 'vector' is an entity declared

// in the 'std' namespace.

// Read about STL (Standard Template Library) in your docs.


Here an_int_vector can only contain int's, not any kind of objects/types, but you can declare another vector that contains another type and so on (you can accomplish a vector similar to Java's with (virtual) classes you write yourself, if they all inherit from the same superclass).

Edited by - Dactylos on September 24, 2001 3:37:14 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i dunno about number 1 or 3, but for number 2, a isnt a constant!
to make a a constant u have3 to declare it as one.


i.e. const int a = 10;
int b[ a ];


later

Share this post


Link to post
Share on other sites
quote:
Original post by delbogun
1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex.


If you''re talking about instantiating a variable with a hex value, simply prefix the hex value with ''0x''. e.g.

long a = 0xffff;

You hardly ever instantiate binary variables in C/C++. The language performs automatic tranlsation from hex to binary to decimal as the context is appropriate.

Now, if you''re talking about displaying hex or binary values, you need to use manipulators . Manipulators change the numeric display mode. Search for iomanip.h and read up on the functions in it. (Any other equivalents, anyone?)

quote:
2. Why doesn''t it work to write this in c++:

int a = 10;
int b[ a ];

it complains about that ''a'' isn''t a constant.


In C/C++ you are not permitted to "statically" declare an array with a variable length. To do this you need to manipulate pointers:

int a = 10;
int *b;
b = new intLink

Share this post


Link to post
Share on other sites
ok, thanks a lot guys..
the reason i wanted to write in hex is because it is easier when you work with single bits, example the fourth bit should be 1. But how do you write in octal, isn''t it 8x0000 ?

Share this post


Link to post
Share on other sites

  • Advertisement