Создал два класса: базовый Base и производный от него Derived.
#include<iostream>
using namespace std;
class Base
{
double d;
protected:
long l;
public:
int i;
Base(int I=1, long L=132132, double D=3,14)
{
i=I; l=L; d=D;
}
};
class Derived: public Base
{
float f;
public:
Derived()
{}
Derived(int I, long L, double D, float F)
{
i=I; l=L; d=D; f=F;
}
};
int main()
{
Derived Der;
Derived Der(7,1321,3.14,2.16);
cout<<"Size of Base = "<<sizeof(Base)<<endl;
cout<<"Size of Derived = "<<sizeof(Derived)<<endl;
}
Derived(int I, long L, double D, float F): Base(I, L, D)
{
f=F;
}
#pragma pack(1)
class Derived: public Base
...
#pragma pack(1)
class Derived: public Base
...
Ну, скажем так, Стандарт не прописывает жестко размеры типов, это все компиляторо- и машинно-зависимо. У меня на машине sizeof(float) = sizeof(int) = sizeof(long) = 4, а sizeof(double) = 8... Вот тебе и 20 байт...
ostream& operator << (ostream& os, const Derived& obj) {
os <<
"i = " << obj.i << " &i = " << &(obj.i) << endl <<
"L = " << obj.l << " &L = " << &(obj.l) << endl <<
// "d = " << obj.d << " &d = " << &(obj.d) << endl <<
"f = " << obj.f << " &f = " << &(obj.f) << endl;
return os;
}
ostream& operator << (ostream& os, const Derived& obj) {
os <<
"i = " << obj.i << " &i = " << &(obj.i) << endl <<
"L = " << obj.l << " &L = " << &(obj.l) << endl <<
// "d = " << obj.d << " &d = " << &(obj.d) << endl <<
"f = " << obj.f << " &f = " << &(obj.f) << endl;
return os;
}
#include<iostream>
using namespace std;
class Base
{
double d;
protected:
long l;
public:
int i;
Base(int I=1, long L=132132, double D=3.14)
{
i=I; l=L; d=D;
}
};
#pragma pack(1)
class Derived: public Base
{
friend ostream& operator << (ostream&, const Derived&);
float f;
public:
Derived() {}
Derived(int I, long L, double D, float F): Base(I, L, D) {
f=F;
}
};
ostream& operator << (ostream& os, const Derived& obj) {
os <<
"i = " << obj.i << " &i = " << &(obj.i) << endl <<
"L = " << obj.l << " &L = " << &(obj.l) << endl <<
// "d = " << obj.d << " &d = " << &(obj.d) << endl <<
"f = " << obj.f << " &f = " << &(obj.f) << endl;
return os;
}
int main()
{
Derived Der;
Derived Der2(7,1321,3.14,2.16);
cout<<"Size of Base = "<<sizeof(Base)<<endl;
cout<<"Size of Derived = "<<sizeof(Derived)<<endl;
cout << Der2 << endl;
}
#include<iostream>
using namespace std;
class Base
{
double d;
protected:
long l;
public:
int i;
Base(int I=1, long L=132132, double D=3.14)
{
i=I; l=L; d=D;
}
};
#pragma pack(1)
class Derived: public Base
{
friend ostream& operator << (ostream&, const Derived&);
float f;
public:
Derived() {}
Derived(int I, long L, double D, float F): Base(I, L, D) {
f=F;
}
};
ostream& operator << (ostream& os, const Derived& obj) {
os <<
"i = " << obj.i << " &i = " << &(obj.i) << endl <<
"L = " << obj.l << " &L = " << &(obj.l) << endl <<
// "d = " << obj.d << " &d = " << &(obj.d) << endl <<
"f = " << obj.f << " &f = " << &(obj.f) << endl;
return os;
}
int main()
{
Derived Der;
Derived Der2(7,1321,3.14,2.16);
cout<<"Size of Base = "<<sizeof(Base)<<endl;
cout<<"Size of Derived = "<<sizeof(Derived)<<endl;
cout << Der2 << endl;
}