Wednesday, November 21, 2012

Something about Class And Struct in cpp

#include <iostream>
using namespace std;

class A{
    public:
        int a;
};

struct B:A{};

struct C{
    int c;
};

class D:C{};

int main()
{
    B b;
    D d;
    b.a = 1;
    d.c = 2;  //give error at this line
}

OUTPUT:

Something to Remember:
  • In c# struct goes on stack means they are value type. and class is reference type. So there is huge difference between struct and class in c# but in C++ there is almost no difference between a struct and  a class (beside that public and private thing).

No comments:

Post a Comment