Home arrow C programming arrow Data Types, Variables, and Constants

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
Data Types, Variables, and Constants PDF Print E-mail
Written by Hemanshu Patel   
Friday, 19 October 2007
Article Index
Data Types, Variables, and Constants
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8

Structures

A structure is created using the keyword struct. In C++, a structure also defines a class. The only difference between class and struct is that, by default, all members of a structure are public. To make a member private, you must use the private keyword. The general form of a structure declaration is like this:

struct struct-name : inheritance-list {
// public members by default
protected:
// private members that can be inherited
private:
// private members
} object-list;

In C, several restrictions apply to structures. First, they may contain only data members; member functions are not allowed. C structures do not support inheritance. Also, all members are public and the keywords public, protected, and private are not allowed.


Unions

A union is a class type in which all data members share the same memory location. In C++, a union may include both member functions and data. In a union, all of its members are public by default. To create private elements, you must use the private keyword. The general form for declaration of a union is

union class-name {
// public members by default
private:
// private members
} object-list;>

In C, unions may contain only data members and the private keyword is not supported.

The elements of a union overlay each other. For example,

union tom {   char ch;   int x; } t;

declares union tom, which looks like this in memory (assuming 2-byte integers):
Like a class, the individual variables that comprise the union are referenced using the dot operator. The arrow operator is used with a pointer to a union.

There are several restrictions that apply to unions. First, a union cannot inherit any other class of any type. A union cannot be a base class. A union cannot have virtual member functions. No members may be declared as static. A reference member cannot be used. A union cannot have as a member any object that overloads the = operator. Finally, no object can be a member of a union if the object’s class explicitly defines a constructor or destructor function. (Objects that have only the default constructors and destructors are acceptable.)



 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits