Class Declarations
The programmers are consented to classify program-unique data types in the C++ programming language through the employ of classes. The classes outline forms of data structures and those data structures which work along with the functions. Cases of these data types are referred as objects and could comprise member variables, member functions, overloaded operators and constants outlined through the programmer. With respect to syntax, the extensions of C struct are classes, that can not comprise of overloaded operators or functions.
Aggregate classes
The aggregate class is the class with no protected or private non-static data members, no virtual functions, no base classes and no user-declared constructors. Such the class could be determined with the brace-enfolded comma-assorted list of initializer clauses.
Plain Old Data Structure (POD-structs)
An aggregate class which has non-POD-reference or union (or array of such types), no non static members of data of type non-POD-struct and has no user defined destructor and no user defined assignment operator is referred as Plain Old Data Structure i.e. POD-struct. A POD-struct is supposed to be the counterpart of C++ of the C struct. In most of the instances, the POD-struct will contain the similar memory layout as the related struct stated in C. Due to this issue, on certain occasions conversationally, POD structs are referred to as C-style structs.
Attributes communicated between POD-structs in C++ and structs in C
ñ Data members are apportioned in the way that afterwards members have more eminent addresses within the object, take exception to where it is distinguished by the access-specifier.
ñ The two types of POD-struct are lay out-congenial whenever they have a similar number of non static data members and conforming to (in order) have layout- congenial types.
ñ A POD-struct might comprise unnamed padding.
ñ A pointer to the POD-struct object, appropriately commuted employing the reinterpret cast, points to its initial member and vice verse, involving that there is no padding at the origion of the POD-struct.
ñ A POD-struct might be employed with the offset of macro.
Declaring the Class
C++ classes have their own members. These members comprise variables (comprising other classes and structures), functions (overloaded operators ) known as constructors, destructors or methods. Members are announced to be either privately or publicly accessible employing the public: and private: gain specifiers in the order given. Any member encountered after the specifier will have the consociated access before the time another specifier came across. There is also inheritance among classes which could make employ of the protected: specifier
Member functions
The important characteristic of the C++ class and structure are member functions. Each data type could have its own built-in functions (denoted to as methods) that have approach to all ( private and public ) members of the data type. In the body of these non-static member functions, the keyword this could be employed to refer to the object for which the function is known as. This is by and large carried out by passing the address of the object as the implicit first argument to the function
The Instantiation of Class
A class is the mechanism for creating user-defined data types. It is alike to the C language structure data type. In C, the structure is composed of the set of data members. In C++, the class type is like the C structure, take exception to that the class is composed of the set of data members and the set of operations that could be executed on the class.
In C++, the class type could be declared with the keywords union, struct, or class. A union object could accommodate any one of the set of named members. Structure and class objects accommodate the complete set of members. Each class type represents the unique set of class members comprising data members, member functions, and other type names. The default access for the members is based on the class key:
ñ The members of the class declared with the keyword class are private by default. A class can be inherited privately by default action.
ñ The members of the class declared with the keyword struct are public by default. A structure can be inherited publicly by default.
ñ The members of the union (declared with the keyword union) are public by default. A union cannot be employed as the base class in derivation.
ñ A class is the expanded conception of the data structure: instead of accommodating only data, it could accommodate both data and functions.
ñ the object is the instantiation of the class. In terms of variables, the class would be the type, and the object would be the variable.
Classes are by and large declared employing the keyword class, with the accompanying format:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Where class_name is the valid identifier for the class, object_names is the opt for objects of this class. The body of the declaration could comprise members, that could be either data or function declarations, and optionally access specifiers.
All is very similar to the declaration on data structures, take exception to that computer programmer could now comprise also functions and members, but also this new thing known as access specifier. the access specifier is one of the accompanying three keywords: public, protected or private. These specifiers alter the access rights that members accompanying them assume:
ñ Private members of the class are accessible only from within other members of the similar class or from their friends.
ñ Protected members are accessible from members of their similar class and from their friends, but also from the members of their derived classes.
ñ Finally, public members are approachable from to any place where the object is visible.
By default, all members of the class declared with the class keyword have private access for all its members. Thus, any member that is declared before an another class specifier automatically has private access.
The scope operator (::) specifies the class to which the member being declared belongs, allowing exactly the similar scope attributes as if this function definition was directly comprised within the class definition. For illustration, in the function set_values() of the previous code, computer programmer have been able to employ the variables x and y, which are private members of class CRectangle, which refers as they are only accessible from other members of their class.
The only difference between determining the class member function completely within its class or to comprise only the prototype and later its definition, is that in the first case the function will automatically be regarded the inline member function by the compiler, while in the second it will be the normal (not-inline) class member function, which in reality supposes no difference in behavior.
Members x and y have private access (remember that if nothing else is said, all members of the class defined with keyword class have private access). By declaring them private computer programmer deny access to them from anywhere outside the class. This makes sense, as computer programmer have already defined the member function to set values for those members within the object: the member function set_values(). Therefore, the rest of the program does not need to have direct access.