C / C + + programming style
1.C Style (structured programming): Data and functions (operations) without any correlation typedef struct point3d{ float x; float y; float Z;} Point3d_t;voidpoint3d_print (const point3d_t *PD) { printf ("%g,%g,%g\n", Pd->x, Pd->y, pd->z);}
2. Object-based (Object-base): Provides an abstract data type (ADT) to support encapsulating class point3d{ friend Ostream &operator<< (Ostream &os, Const Point3D &pt);p ublic: Point3D (float x = 0.0, float y = 0.0, float z = 0.0): _x (x), _y (y), _z (z) {} FL Oat x () { return _x; } Float y () { return _y; } Float Z () { return _z; } void X (float xval) { _x = xval; } etc... Private: float _x; float _y; float _z;}; Inline Ostream &operator<< (ostream &os, const Point3D &pt) { return OS << ("<< pt._x << "," << pt._y << "," << pt._z << ")";}
3. Object-oriented (Object-oriented-model): Provides inheritance and polymorphism class Point{public:point (float x = 0.0): _x (x) {} float X () { return _x; } void X (float xValue) {_x = XValue; }protected:float _x;}; Class Point2d:public point{public:point2d (float x = 0.0, float y = 0.0): Point (X), _y (y) {} float Y () { return _y; } void Y (float yvalue) {_y = Yvalue; }protected:float _y;}; Class point3d:public point2d{friend inline Ostream & operator<< (ostream &os, const Point3D &PT) ;p Ublic:point3d (Float x = 0.0, float y = 0.0, float z = 0.0): point2d (x, y), _z (z) {} float Z () { return _z; } void Z (float zvalue) {_z = Zvalue; }private:float _z;}; Inline Ostream &operator<< (ostream &os, const Point3D &pt) {return OS << ("<< pt._x &L t;< "," << pt._y << "," << pt._z << ")";}
4. Generic: Provides a template that provides type-agnostic programming styles//Example (1): Provides only type parameterized template <typename type>class point3d{public: Point3D (Type x = 0.0, type y = 0.0, type z = 0.0): _x (x), _y (y), _z (z) {} Type x () { return _x; } Type y () { return _y; } Type Z () { return _z; } void x (const Type &xval) { _x = xval; } etc... Private: Type _x; Type _y; Type _z;};
Example (2): Provide both the type and number (int dim) Parameterized template <typename type, int dim>class point{public: Point () {} point (type Coordes[dim]) {for (int index = 0; index < Dim; ++index) { _coordes[index] = Coordes[index]; } } Const TYPE &operator[] (int index) const { Assert (Index < Dim && Index >= 0); return _coordes[index]; } Private: Type _coordes[dim];}; Template <typename type, int dim>inline ostream &operator<< (ostream &os, const point< Type, Dim ; &PT) { os << "("; for (int i = 0; i < Dim; ++i) os << pt[i] << ","; OS << ")"; return OS;} Test int array[] = {11,12,333}; Point<int, 3> Point (array); cout << Point << endl;cout << point[2] << Endl;
Summary: C + + Plus packaged layout costs
(1) Three data-member[data members] are directly contained in each class-object[object], as in c-struct;
(2) Although the member-function exists in the class declaration, but does not appear in the object, each non-inline Member function[non-inline member function] will only birth a function entity;
(3) and the inline member function produces a functional entity on each of its users.
C + + object model
1. All data members (except static data members) are stored in each object;
2.[1]static data members, [2] member functions (either static/non-static) are stored outside of all objects;
3. For virtual-functions, each object has a vptr (point to a VTBL (containing Type_info object, virtual functions).
Therefore: the size of objects in memory
(1) The sum size of its non-static data member
(2) The space filled with any alignment (memory to its) needs [may exist in the middle of the members, may also exist in the aggregate boundary];
(3) Add any additional burden arising from the interior, with the support of Virtual[vptr].
Test class Point1{};class Point2{private: int number;}; Class Point3{public: Point3 (int _number = 0): Number (_number) {} ~point3 () {} static void Showcount () { std::cout << "point3_count =" << point3_count << Std::endl; } Private: static int point3_count;private: int number;}; Class Point4{public: virtual ~point4 ();p rivate: int number;}; int main () { cout << sizeof (Point1) << Endl; cout << sizeof (POINT2) << Endl; cout << sizeof (POINT3) << Endl; cout << sizeof (POINT4) << Endl;}
The layout of objects in memory
Derived Class's object and pointer layouts (e.g.)
(In fact, there are some errors in the layout of Zooanimal subobject, the string type only accounts for 4 bytes in modern C + +, so zooanimal subobject occupies only 12 bytes of memory, not 16 bytes in the graph)
Description
(1) Pointer & reference All need a space of only one word length (4 bytes in x86_32);
(2) in a derived class object: The base class sub-object is adjacent to the location of the derived class sub-object in memory.
Test class Zooanimal{public: Zooanimal () {} Virtual ~zooanimal () {} virtual void rotate () {}protected: int loc; string name;}; Class Bear:public Zooanimal{public: Bear () {} ~bear () {} void rotate () { }protected: enum Dances { //... }; Dances Dances_known; int cell_block;}; int main () {bear B; Zooanimal *pz = &b; Bear *PB = &b; cout << "Pointer ..." << Endl; cout << sizeof (PZ) << Endl; cout << sizeof (PB) << Endl; cout << "\nobject ..." << Endl; cout << sizeof (*PZ) << Endl; cout << sizeof (*PB) << Endl; cout << "\nclass ..." << Endl; cout << sizeof (zooanimal) << Endl; cout << sizeof (bear) << Endl;}
Test Description:
PB covers an address that contains the entire bear object, while the PZ contains only the Zooanimal subobject!
Relive "Inside the C + + object Model" (1)--about objects