class A {
public:
A(int a = 0)
:ma(a)
{}
private:
int ma;
static int mb;
};
int A::mb = 20;
int main() {
A a;
cout << sizeof(a) << endl;
return 0;
}
class Base {
public:
Base(int data)
:ma(data)
{
cout << "Base(int data)" << endl;
}
~Base() { cout << "~Base()" << endl; }
protected:
int ma;//想要派生类访问 不想要外部访问
};
class Derive :public Base {
public:
Derive(int data)
:mb(data)
,Base(data)//ma(data) 不允许这样直接初始化
{
cout << "Derive(int data)" << endl;
}
~Derive() { cout << "~Derive()" << endl; }
private:
int mb;
};
int main() {
Derive d(20);
return 0;
}
class Base {
public:
Base(int data = 10)
:ma(data)
{}
void show() { cout << "void Base::show() " << endl; }
void show(int) { cout << "void Base::show(int)" << endl; }
protected:
int ma;
};
class Derive :public Base {
public:
Derive(int data=20)
:Base(data)//调用基类的构造函数初始化基类继承过来的成员变量
,mb(data)
{}
void show() { cout << "void Derive::show()" << endl; }
//show将基类的show()和show(int)隐藏了
private:
int mb;
};
int main() {
Derive d;
d.show();
//d.show(10);//“Derive::show”: 函数不接受 1 个参数
//基类的show(int)被隐藏了
d.Base::show(10);
Base b;
b = d;//从下(派生类)到上(基类)OK
//d = b;//从上(基类)到下(派生类)NO
Base* pb = &d;
pb->show();
pb->show(10);
((Derive*)pb)->show();
//Derive* pd = &b;
Derive* pd = (Derive*)&b;//不安全存在内存非法访问
//pd->show();//不安全
return 0;
}
class Base {
public:
Base(int data = 10)
:ma(data){}
void show() { cout << "void Base::show()" << endl; }
void show(int) { cout << "void Base::show(int)" << endl; }
protected:
int ma;
};
class Derive :public Base {
public:
Derive(int data = 20)
:Base(data)
,mb(data)
{}
void show() { cout << "void Derive::show()" << endl; }
private:
int mb;
};
int main() {
Derive d(20);
Base* pb = &d;
pb->show();//静态绑定 编译时期call Base::show()
pb->show(10);//静态绑定 编译时期 call Base::show(10)
cout << sizeof(Base) << endl;
cout << sizeof(Derive) << endl;
cout << typeid(pb).name() << endl;
cout << typeid(*pb).name() << endl;
return 0;
}
#include <typeinfo>
class Base {
public:
Base(int data = 10)
:ma(data)
{}
virtual void show() { cout << "void Base::show()" << endl; }
virtual void show(int) { cout << "void show(int )" << endl; }
protected:
int ma;
};
class Derive :public Base {
public:
Derive(int data = 20)
:Base(data)//ma(data)不允许
,mb(data)
{}
//virtual void show() 重写 覆盖
void show() { cout << "void Derive::show()" << endl; }
private:
int mb;
};
int main() {
Derive d(20);
Base* pb = &d;
/*
pb->Base Base::show如果发现show()是普通函数 就进行静态绑定
如果发现show()是虚函数 就进行动态绑定
mov eax ,dword ptr[pb]
mov ecx, dword ptr[eax]
call ecx(虚函数地址)动态(运行时期)绑定(函数调用)
*/
pb->show();
/*
pb->Base Base::show如果发现show(int)是普通函数 进行静态绑定
如果发现show()是虚函数 就进行动态绑定
mov eax,dword ptr[pb]
mov ecx,dword ptr[eax]
call ecx(虚函数地址)动态(运行时期)绑定(函数调用)
*/
pb->show(30);
cout << sizeof(Base) << endl;
cout << sizeof(Derive) << endl;
cout << typeid(pb).name() << endl;
/*
*p类型的判定:Base有没有虚函数
Base没有选虚函数 *pb的类型就是编译时期的类型
*p<<==>>Base类型
如果Base有虚函数 *pb的类型就是运行时期的RTTI类型
pb->d(vfptr)->Derive vftable class Derive
*/
cout << typeid(*pb).name() << endl;
return 0;
}
class Base {
public:
Base(int data=10)
:ma(data)
{
cout << "Base(int data=10)" << endl;
}
virtual void show() { cout << "void Base::show()" << endl; }
virtual ~Base() { cout << "~Base()" << endl; }
protected:
int ma;
};
class Derive :public Base {
public:
Derive(int data=10)
:Base(data)
,mb(data)
,ptr(new int[data])
{
cout << "Derive(int data=10)" << endl;
}
void show() { cout << "void Derive::show()" << endl; }
~Derive() { delete[] ptr; cout << "~Derive()" << endl; }
private:
int mb;
int* ptr;
};
int main() {
/*Derive d(20);
Base* p = &d;
p->show();*/
Base* pb = new Derive(20);
pb->show();
delete pb;
return 0;
}