Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 3 Q's regarding C++

    Question:
    1.The DOM is an API,and JavaScript is most widely used to access it,but in general it can be used by any language.
    How can I access a browser's (IE) DOM API with C++ ?


    2.In C++ struct`s are generally used as data-only classes (no functions - methods).But what if I want my struct's
    members to have a default value (I'll need a constructor function,but remember,I want my struct to be data-only).
    struct Test
    {
    int myInt=5;
    };


    3.Can anyone explain what binary files differ from normal text files,in most books,you find ways to use them,but
    no one explains the differences...

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    3.Can anyone explain what binary files differ from normal text files,in most books,you find ways to use them,but
    no one explains the differences...

    I don't knwo the first 2,
    When you write code, you write it in a text editor, that's a text file for human eyes to read.

    then you compile the text file into binary file, for computer to read.

    example in java, you write java code and save it as *.java, after you compile the java file, you willl get *.class, if you want to run you program, you have to use the *.class because that what computer understand, if you want to show the code to someone, you have to use the *.java because that's what human understsand

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    2. As far as I know, modern day C++ allows for member functions in structs. I don't know if they have constructors though, since it's been a while since I messed with C++. I'd say just try it.
    Even if they don't, you can always use a class instead that only has public data members, and a constructor. Believe me, the computer won't care.

    shmoove

    Edit:
    "allows for member function in structs", forgot to write "in structs" :P
    Last edited by shmoove; 04-18-2004 at 04:38 PM.

  • #4
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weronpc
    3.Can anyone explain what binary files differ from normal text files,in most books,you find ways to use them,but
    no one explains the differences...

    I don't knwo the first 2,
    When you write code, you write it in a text editor, that's a text file for human eyes to read.

    then you compile the text file into binary file, for computer to read.

    example in java, you write java code and save it as *.java, after you compile the java file, you willl get *.class, if you want to run you program, you have to use the *.class because that what computer understand, if you want to show the code to someone, you have to use the *.java because that's what human understsand
    Yes...I was speaking more of the files you can read/write from, with the special functions made for binary files - read() and write().
    You have to open a file as binary (ios::binary),and also,you can only write a single type of data in each file.
    So,plain text files have ASCII characters,while binary files are a succesion of 1's and 0's.
    Doesn't that mean that they're just simple text files,converted in binary ...?

    *A little confused here...*

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the binary in them is ASCII code (or any other kind of text encoding), then yes.
    But the main difference, is that if you write 123 into a a file in binary mode, then the succession of 0's and 1's that represent the number 123 will be written into the file, and if you do it in text mode, the succession of 0's and 1's that represent the ASCII codes (again, it doesn't have to be ASCII, it could be another form of text encoding) of the digits '1', '2' and '3' will be written into the file. The same goes for reading: in text mode the information will be taken as if it represented some text-encoding, and in binary mode as just plain numbers.

    shmoove

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    C++ classes do have contructors and destructors.
    Omnis mico antequam dominus Spookster!

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    C++ classes do have contructors and destructors.
    And like I was trying to say, structs in modern day C++ are the same as classes, except that their members have public access by default, where in classes members have private access by default.
    (well, I fixed the post now).

    shmoove

  • #8
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually answering 2, the easiest way is using the constructor initialization list (or whatever it's called):
    Code:
    class X { //i prefer using class to struct
      int i;
    public:
      X() : i(5) {};
    }
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #9
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1) The only way to access DOM using C++ code is to write a plugin for the browser you are interested in. This assumes a great deal of knowledge both about how the browser exposes its DOM, and how the plugin interface works.

    2) If you are using a structure only to store data, then the data does not have any inherent characteristics.. otherwise you would have used a class in the first place. So its better to initialize the structure to the default values in the same function you are initializing the structure.

    3) as shmoove was saying, if you write the number(word) 123 to a file,
    binary file look like this
    00000000 01111011 ---> 123
    text file(ASCII) looks like this
    00110001 00110010 00110011 -> 49('1') 50('2') 51('3')

    As you can see, in the first case, the number is represented as a whole, the true representation in binary. note that the order of bytes may vary depending on the architecture of the computer.

    In the second case, the ascii code of each digit of the number is represented in binary.
    Nobody is Perfect. I am Nobody.

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    When you create a struct though, you can do like --

    typedef struct{

    int i,j,m;

    }myStruct, *myStructPtr;

    myStruct ms = {0,0,0};

    That can save some time.
    Omnis mico antequam dominus Spookster!

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why complicate things?

    Programming the browser seems rather redundant. There are many excellent DOM based libraries out there (mostly for XML, but XML and DOM go hand-in-hand).

    You can have constructors and any kind of methods in a C++ struct. As long as you don't use any virtual functions (including a virtual destructor) the C++ standard guarantees bit-level compatibility with C.

    The only difference between binary and text files is that M$ decided to make a difference between them and have I/O routines fiddle with the line breaks on text files. Personally I don't see any reason for doing this. It only makes it harder to write cross-platform programs, but that seems to be the M$ way...

  • #13
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Constructors/Destructors/member functions in a struct? Really?

    I'll have to try this..
    Omnis mico antequam dominus Spookster!

  • #14
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1. Don't know...

    2. As far as I can tell, structs are identical to classes in every way (ie they can have functions, destructors etc) except that in a struct each member is publc by default, whereas in a class you have to define whether it is public or private.

    3. In an ASCII file each character is coded in 8 bits according to the ASCII table (for example, 1 is 0011111, 2 is 01000000, 3 is 01000001). In a binary file you get a bunch of 1's and 0's and you have to know if you read them in as ints, floats, chars or whatever.

    For example, the number 123.456 in a text file will occupy 7 bytes (7 characters) but in a binary file only 4 bytes (1 float).



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •