Home » C++ » C++ Q&A (a) between C++ and C

C++ Q&A (a) between C++ and C

statement: Q&A series of articles I usually encounter or see some of the problems, in a small problem, also need someone to answer the idea, these questions and answers sorted out,.


welcome to discuss the problem with me, and also welcome to reprint Q&A series article.





according to the understanding of the general entry, there is the following example:

bool, print (char, name[20], int, hight, int, age)
{
Printf ("people:n");
Printf ("name:%sn", name);
Printf ("hight:%dcmtage:%dyearn", "hight", "age");
Return true;
}
Int, main ()
{
Char people1[10] = "angle"";
Int hight = 180, age = 300;
Print (people1, hight, age);
System ("PAUSE");
Return 0;
}









another example of a minor change in the book:




#include 
Typedef struct point3d
{
Float x;
Float y;
Float z;
}Point3d;
Void Point3d_print (const, Point3d, *pd)
{
Printf ("(%g,%g,%g), N, pd->x, pd->y, pd->z);
}
Int, main ()
{
Point3d point;
Point.x = point.y = point.z = 1;
Point3d_print (&point);
System ("PAUSE");
Return 0;
}








in C++, data and functions can be encapsulated to form a whole (abstract data type), that is, class.

first example of a minor change:

#include 
Class Point3d
{
Public:
Point3d (float x = 0, float = 0 y, float Z =0.0) / / default constructor, the default parameters
_x (x), _y (y), _z (z) / / initialization list
{
}
/ / often member function for the object is often called
Float, X (), const, {return, _x,}
Float, y (), const, {return, _y,}
Float, Z (), const, {return, _z,}
/ / often overloaded member function above, for general object call
Float, X (), {return, _x;}
Float, y (), {return, _y;}
Float, Z (), {return, _z;}
Void x (float, xval) {_x = xval;}
Private:
Float _x;
Float _y;
Float _z;
};
The overloaded operator / /
Inline std:: "ostream& operator" (std:: ostream, &os, const, Point3d, &pt)
{
Return OS "(") "pt.x ()"","
Pt.y (), ",", "pt.z ()," """;
}
Int, main ()
{
Point3d point (1.1, 2.2, 3.3);
Std:: cout < point; / / call the above version of the operator
Std:: cout, std:: endl;
System ("PAUSE");
Return 0;
}

by class, we can put the "data" and "data processing operation" statement together, and through the public and private access modifier content inside the class package. But from here to spy on the process and object oriented.




reference: < depth exploration C++ object model >



Latest

Hot Channels »