Classes Vs. structures (C++ only)

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Classes Vs. structures (C++ only)

Post by Saman » Sun Jan 22, 2012 7:41 am

The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.

For example, in the following code fragment, the class X is equivalent to the structure Y:

Code: Select all

class X {

  // private by default
  int a;

public:

  // public member function
  int f() { return a = 5; };
};

struct Y {

  // public by default
  int f() { return a = 5; };

private:

  // private data member
  int a;
};
If you define a structure and then declare an object of that structure using the keyword class, the members of the object are still public by default. In the following example, main() has access to the members of obj_X even though obj_X has been declared using an elaborated type specifier that uses the class key class:

Code: Select all

#include <iostream>
using namespace std;

struct X {
 int a;
 int b;
};

class X obj_X;

int main() {
  obj_X.a = 0;
  obj_X.b = 1;
  cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;
}
The following is the output of the above example:

Here are a and b: 0 1
evangilbort
Sergeant
Sergeant
Posts: 12
Joined: Fri Jun 21, 2013 12:19 pm

Re: Classes Vs. structures (C++ only)

Post by evangilbort » Mon Jun 24, 2013 5:01 pm

A classes is define types of data structure and the function that operate on data. A classes is default to private mode access while structure are public. A structure has public as default access specifier default inheritance type is public.
Post Reply

Return to “C/C++ Programming”