HOME

TheInfoList



OR:

A class in C++ is a user-defined type or
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
declared with
keyword Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syste ...
class that has data and functions (also called
member variable In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (''member functions''). In class-based programming languages, thes ...
s and
member function A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
s) as its members whose access is governed by the three access specifiers ''private'', ''protected'' or ''public''. By default access to members of a C++ class is ''private''. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
to the class and are accessible outside the class. Instances of a class data type are known as
objects Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ai ...
and can contain member variables,
constant Constant or The Constant may refer to: Mathematics * Constant (mathematics), a non-varying value * Mathematical constant, a special number that arises naturally in mathematics, such as or Other concepts * Control variable or scientific const ...
s, member functions, and overloaded operators defined by the programmer.


Differences between a structure and a class in C++

In C++, a class defined with the class keyword has
private Private or privates may refer to: Music * "In Private", by Dusty Springfield from the 1990 album ''Reputation'' * Private (band), a Denmark-based band * "Private" (Ryōko Hirosue song), from the 1999 album ''Private'', written and also recorded ...
members and base classes by default. A structure is a class defined with the struct keyword.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §9 Classes
lass Lass may refer to: *A girl/young woman in Scottish/Northern English People Surname *August Lass (1903–1962), Estonian footballer * Barbara Kwiatkowska-Lass (1940–1995), Polish actress *Donna Lass (1944–' 1970), possible victim of the Zodiac ...
' para. 4
Its members and base classes are
public In public relations and communication science, publics are groups of individual people, and the public (a.k.a. the general public) is the totality of such groupings. This is a different concept to the sociological concept of the ''Öffentlichk ...
by default. In practice, structs are typically reserved for data without functions. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.


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.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §8.5.1 Aggregates cl.init.aggr' para. 1
Such a class can be initialized with a brace-enclosed comma-separated list of initializer-clauses.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §8.5.1 Aggregates cl.init.aggr' para. 2
The following code has the same semantics in both C and C++. struct C ; struct D ; // initialize an object of type C with an initializer-list C c = ; // D has a sub-aggregate of type C. In such cases initializer-clauses can be nested D d = ;


POD-structs

A POD-struct (Plain Old Data Structure) is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined
assignment operator Assignment, assign or The Assignment may refer to: * Homework * Sex assignment * The process of sending National Basketball Association players to its development league; see Computing * Assignment (computer science), a type of modification ...
and no user-defined destructor. A POD-struct could be said to be the C++ equivalent of a C struct. In most cases, a POD-struct will have the same memory layout as a corresponding struct declared in C. For this reason, POD-structs are sometimes colloquially referred to as "C-style structs".


Properties shared between structs in C and POD-structs in C++

*Data members are allocated so that later members have higher addresses within an object, except where separated by an access-specifier.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §9.2 Class members lass.mem' para. 12
*Two POD-struct types are layout-compatible if they have the same number of nonstatic data members, and corresponding nonstatic data members (in order) have layout-compatible types.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §9.2 Class members lass.mem' para. 14
*A POD-struct may contain unnamed
padding Padding is thin cushioned material sometimes added to clothes. Padding may also be referred to as batting when used as a layer in lining quilts or as a packaging or stuffing material. When padding is used in clothes, it is often done in an attempt ...
.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §9.2 Class members lass.mem' para. 17
*A pointer to a POD-struct object, suitably converted using a
reinterpret cast In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal ...
, points to its initial member and vice versa, implying that there is no padding at the beginning of a POD-struct. *A POD-struct may be used with the
offsetof C's macro is an ANSI C library feature found in . It evaluates to the offset (in bytes) of a given member within a struct or union type, an expression of type . The offsetof() macro takes two parameters, the first being a structure name, and the ...
macro.
ISO ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/
IEC The International Electrotechnical Commission (IEC; in French: ''Commission électrotechnique internationale'') is an international standards organization that prepares and publishes international standards for all electrical, electronic and ...
(2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §18.1 Types ib.support.types' para. 5


Declaration and usage

C++ classes have their own members. These members include variables (including other structures and classes), functions (specific identifiers or overloaded operators) known as methods, constructors and destructors. Members are declared to be either publicly or privately accessible using the public: and private: access specifiers respectively. Any member encountered after a specifier will have the associated access until another specifier is encountered. There is also inheritance between classes which can make use of the protected: specifier.


Global and local class

A class defined outside all methods is a global class because its objects can be created from anywhere in the program. If it is defined within a function body then it's a local class because objects of such a class are local to the function scope.


Basic declaration and member variables

Classes are declared with the class or struct
keyword Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syste ...
. Declaration of members are placed within this declaration. The above definitions are functi