structures fooloing to look like classes

This is a discussion on structures fooloing to look like classes within the C++ Programming forums, part of the General Programming Boards category; Hi c++ ppl; I do program in C and couple of years ago finished reading the book "C++ how to ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    20

    Thumbs up structures fooloing to look like classes

    Hi c++ ppl;

    I do program in C and couple of years ago finished reading

    the book "C++ how to programm" by deitel&deitel which is a great book and give me scoop on the language(i had read their
    C how to programm as well), so i have an idea about C++ but can any one show me in a code how close we can get to classes

    by using structure that in some way use a function interface.?

    I know u cant use functions as members in struct but can u show me the unorthodox way of getting around it.

    because in WIN API most of the structures be it be in GDI or

    other parts use structures and interfaces to them which i understand are separate entities but can we have then integrated?


    thats all thank you,

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    The only difference between structures and classes in C++ is that structures have public access by default and classes have private access by default. Otherwise they're exactly the same. In C the best you can get is pointers to functions.
    Code:
    #include <stdio.h>
    
    typedef struct A {
      int x;
      void (*set)(struct A *, int);
      void (*print)(struct A);
    } A;
    
    void set(A *self, int x)
    {
      self->x = x;
    }
    
    void print(A self)
    {
      printf("%d\n", self.x);
    }
    
    int main(void)
    {
      A obj = {10, set, print};
    
      obj.print(obj);
      obj.set(&obj, 20);
      obj.print(obj);
    }
    *Cela*

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    That is the beauty of C++ when considering Windows programming, too. We can easily extend otherwise unwieldy structures through inheritence. Here's a example of taking the WNDCLASSEX structure, and making it "smarter" by exploiting the C++ constructor, etc.






    You can't do that with C !
    Attached Files Attached Files
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    20
    thx but can we set up a lookalike constructors and destructors as well or virtual functions that we can reference later?


    oh that one answered my question

    very good example but is default the constructor?
    Last edited by samsam1; 01-18-2003 at 10:38 AM.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    thx but can we set up a lookalike constructors and destructors as well or virtual functions that we can reference later?

    I'm not sure I follow you. constructors/destructors _must_ have the name of the class. They can always be referenced later.

    If you mean "can we define contructors for pre-existing structures", of course not. But inheritance makes it possible to have the same functionality. Note that, when you DO derive from existing structures/classes such as WNDCLASSEX, never add members - only functions. Because when you add member variables, and then do something like:

    RegisterClassEx(this);

    ...the function could fail.


    [edit]

    Yes, contructors/destructors are always called in C++.

    [/edit]
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-17-2005, 07:31 AM
  2. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 04:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 01:57 PM
  5. classes and structures
    By mcorn in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21