In C, there is no language-supported relationship between data and functions. We speak of this method of programming as procedural.
Two flavors of class member data: static, nonstatic.Three flavors of class member function: static, nonstatic,
an object is a sequence of slots, where each slot points to a member
this simple concept of an index or slot number is the one that has been developed into the C++ pointer-to-member concept
The class object contains the pointers to the two member tables, the one is member data table and the other is member function table.
Although this model is not used in practice within C++, the concept of a member function table has been the traditional implementation supporting efficient runtime resolution of virtual functions
Nonstatic data members are allocated directly within each class object. Virtual functions are supported in two steps:
One reasonable use of the C struct in C++, then, is when you want to pass all or part of a complex class object to a C function. This struct declaration serves to encapsulate that data and guarantees a compatible C storage layout.
struct : default public, can’t use in template as class.
class : defautl private, template key word.
struct C_point {
... };
class Point {
public:
operator C_point() {
return _c_point; }
// ...
private:
C_point _c_point;
// ...
};
The C++ programming model directly supports three programming paradigms.
char boy[] = "Danny";
char *p_son;
...
p_son = new char[ strlen( boy ) + 1 ];
strcpy( p_son, boy );
...
if ( !strcmp( p_son, boy ))
take_to_disneyland( boy );
String girl = "Anna";
String daughter;
...
// String::operator=();
daughter = girl;
...
// String::operator==();
if ( girl == daughter )
take_to_disneyland( girl );
void
check_in( Library_materials *pmat )
{
if ( pmat->late() )
pmat->fine();
pmat->check_in();
if ( Lender *plend = pmat->reserved() )
pmat->notify( plend );
}
The C++ language supports polymorphism in the following ways:
shape *ps = new circle();
ps->rotate();
if ( circle *pc =
dynamic_cast< circle* >( ps )) ...
The memory requirements to represent a class object in general are the following:
The accumulated size of its nonstatic data members
Plus any padding (between members or on the aggregate boundary itself) due to alignment constraints (or simple efficiency)
Plus any internally generated overhead to support the virtuals
class Bear : public ZooAnimal {
public:
Bear();
~Bear();
// ...
void rotate();
virtual void dance();
// ...
protected:
enum Dances {
... };
Dances dances_known;
int cell_block;
};
Bear b( "Yogi" );
Bear *pb = &b;
Bear &rb = *pb;
{
ZooAnimal za;
ZooAnimal *pza;
Bear b;
Panda *pp = new Panda;
pza = &b;
}
To summarize, polymorphism is a powerful design mechanism that allows for the encapsulation of related types behind an abstract public interface, such as our Library_materials hierarchy. The cost is an additional level of indirection, both in terms of memory acquisition and type resolution. C++ supports polymorphism through class pointers and references. This style of programming is called object-oriented.