classes question - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: classes question

  1. #16
    Join Date
    Jan 2009
    Posts
    1,689

    Re: classes question

    Classes can be used for polymorphism and abstraction. Structs can not, that's the biggest difference.

    Anything you can do with OOP you can do with procedural programming. There is nothing that you can do with classes that you can't do with C and structs. The difference is how easy it is to code.
    Last edited by ninja9578; March 23rd, 2009 at 02:27 PM.

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: classes question

    It's a legacy thing mainly. In C, structs were just collections of data. C++ introduced classes, which represent objects which have data and a defined way of interacting with it.

    As it happened C++ also upgraded structs to have all the same capabilities as classes with just a few minor differences which have been covered.

    So essentially they're the same thing. However, in many cases structs will be used like the old C structs----just to hold data, nothing more. Classes are more often used if a fully-encapsulated object is truly desired. It's tradition more than anything else.

    Quote Originally Posted by ninja9578 View Post
    Classes can be used with polymorphism. Structs can not, that's the biggest difference.
    Structs work just fine with polymorphism in C++.

    One advantage of structs is that "struct" is a keyword in C as well as C++. So if you want to write a module in C++ which is callable from C, you can forward declare the struct type in the header, and then the C program can carry around pointers to your object even though it can't correctly interpret them; essentially they'd just be opaque handles as far as C is concerned. But it can do it, and in a type-safe manner (rather than relying on void*). That's not true for classes, since the "class" keyword doesn't exist in C.
    Last edited by Lindley; March 23rd, 2009 at 02:30 PM.

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: classes question

    Quote Originally Posted by StGuru
    I tried, and also I can use the public and private in the struct. If struct = class what are both for?
    Conventionally, the struct keyword would be used to define a class that is nothing more than an aggregate of some variables, without any behaviour specified by member functions. Sometimes, I use it to define function objects that only have one member, namely the public overloaded operator().

    Quote Originally Posted by ninja9578
    Classes can be used with polymorphism. Structs can not, that's the biggest difference.
    Here is a counterexample:
    Code:
    #include <iostream>
    #include <memory>
    
    struct Abc
    {
        virtual ~Abc() {}
        virtual void print() const = 0;
    };
    
    struct X : Abc
    {
        virtual void print() const
        {
            std::cout << "I am an X." << std::endl;
        }
    };
    
    struct Y : Abc
    {
        virtual void print() const
        {
            std::cout << "I am a Y." << std::endl;
        }
    };
    
    int main()
    {
        std::auto_ptr<Abc> p(new X);
        p->print();
        p.reset(new Y);
        p->print();
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center