6 Replies - 5455 Views - Last Post: 07 December 2010 - 10:56 PM Rate Topic: -----

#1 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Difference between structure and class in c++

Posted 07 December 2010 - 04:38 AM

Hi,

Only difference between structures and classes in c++ is accessing of members and inheritance between the classes rite..??

whether this code is correct..??

struct struct_a
{
private:
    int data;
public:
    struct_a(int data_);
    virtual void foo()=0;
    virtual ~struct_a()
    {}
};

class class_b
{
private:
    int data;
public:
    class_b(int data_);
    virtual void foo()=0;
    virtual ~class_b()
    {}
};



and how to create structure object here..???

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between structure and class in c++

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Difference between structure and class in c++

Posted 07 December 2010 - 04:54 AM

Code looks fine. You can't make an instance of either of those because of your pure virtual methods.

The only difference is the default security on the members. You can extend a struct:
struct struct_c : public struct_a {
    struct_c(int data) : struct_a(data) {}
    void foo() { }
}



Of course, a private member, like your data, still isn't visible to the children...
Was This Post Helpful? 1
  • +
  • -

#3 Guest_shimsha*


Reputation:

Re: Difference between structure and class in c++

Posted 07 December 2010 - 05:14 AM

Thanks baavgai.Its very helpful.

It means structure also provides us inheritance and polymorphism rite..??then why we prefer class in place of structure..?Only because of default security on the members.??

Thanks in advance
Was This Post Helpful? 0

#4 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Re: Difference between structure and class in c++

Posted 07 December 2010 - 05:59 AM

Thanks baavgai.Its very helpful.

It means structure also provides us inheritance and polymorphism rite..??then why we prefer class in place of structure..?Only because of default security on the members.??
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Difference between structure and class in c++

Posted 07 December 2010 - 10:15 AM

Security and tradition.

In C++, using a struct usually means the element is simple. It might have a constructor for some default values and maybe equality operators, but not a huge collection of methods. If you want just a few values wrapped together with public access, a struct is usually the choice. When you start getting into public and private, a class it is.

Again, that's really just the popular convention. In practice, it's just a word and you can code as you like.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Difference between structure and class in c++

Posted 07 December 2010 - 12:13 PM

Technically though there is no difference between a class and a struct in C++.

a struct is just a class with a default public specifier:

class Foo { public:

}


same as:

struct Foo { 

}



Past that they are the same. Structs have all the same abilities as classes. You can declare a Type as a struct and then implement it as a class, or visa versa:
#include <iostream>

using namespace std;

struct Foo;

class Foo {
    int bar;
};


int main() {
    Foo f;

    return 0;
}


You can inherit from a struct or a class to either. The struct keyword is literally just an alias for the class keyword with a default specifier of public.

The struct keyword exists for backwards compatibility with C and generally speaking C++ programmers tend to treat structs as something simpler than a class (mostly by limiting the amount of code it in) but this is by tradition/convention only.
Was This Post Helpful? 1
  • +
  • -

#7 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Re: Difference between structure and class in c++

Posted 07 December 2010 - 10:56 PM

Thanks to both of you for your input :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1