Posted on 24th September 2009No Responses
What is a Class in C++

A class is an expanded concept of a data structure. In C++ class is an encapsulation of data members and functions that manipulate the data. The class can also have some other important members which are architecturally important. The C++ programming language allows programmers to define program-specific datatypes through the use of classes instead of holding only data, it can hold both data and functions.
Classes are generally declared using the keyword class, with the following format:

class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;

} object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.

Aggregate classes
An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. Such a class can be initialized with a brace-enclosed comma-separated list of initializer-clauses. The following code has the same semantics in both C and C++:
struct C
{
int a;
double b;
};
struct D
{
int a;
double b;
C c;
};
// initialize an object of type C with an initializer-list
C c = { 1, 2 };
// D has a sub-aggregate of type C. In such cases initializer-clauses can be nested
D d = { 10, 20, { 1, 2 } };

Access Types in a Class
All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:
• Private members of a class are accessible only from within other members of the same class or from their friends.
• Protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
• Finally, public members are accessible from anywhere where the object is visible.

Differences between struct in C and classes in C++
In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default.

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
Leave a Response