A Few Questions

Started by
5 comments, last by delbogun 22 years, 6 months ago
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
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
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
hi!

1. you can write hex like this .. DWORD x = 0x2145;

2. this declaration is for static arrays.
if you want dynamic array du this
int a = 10;
int *b;
b = new intLink

Dave007
dave007@volny.cz
--------Dave[ Math Studio ] A Computer Algebra System
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 
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 ?
No, to write in octal prefix with a zero:

Decimal 17
Hexadecimal 0x11
Octal 021
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan

This topic is closed to new replies.

Advertisement