class Base {
public:
Base(int data=10):ma(data){ }
virtual void show() { cout << "Base::show()" << endl; }
protected:
int ma;
};
class Derive :public Base {
public:
Derive(int data=20)
:Base(data)
,mb(data)
{}
void show() { cout << "Derive::show()" << endl; }//void virtual show()
protected:
int mb;
};
int main() {
Base b(10);
Derive d(20);
b.show();//静态绑定 //call Base::show()
d.show();//静态绑定//call Derive::show()
//动态绑定必须有指针或者引用
Base* pb = &b;
Derive* pd = &d;
pb->show();//动态绑定 pb ->b(vfptr) ->vftable ->call ecx(Base::show())
pd->show();//动态绑定pb->d(vfptr) ->vftable ->call ecx(Derive::show())
//动态绑定 pb1/b1 ->d(vfptr) ->vftable
Base* pb1 = &d;
pb1->show();
Base& b1 = d;
d.show();
//动态绑定
Derive& d1 = d;//d(vfptr) ->vftable
Derive* pd1 = (Derive*)&b;//pd1 ->b(vfptr)->vftable
d1.show();
pd1->show();
return 0;
}
class Animal {
public:
Animal(string name):_name(name){}
virtual void bark() {};
protected:
string _name;
};
class Cat :public Animal {
public:
Cat(string name):Animal(name){}
void bark() { cout <<_name<<" " << " Cat:miao miao" << endl; }
};
class Dog :public Animal {
public:
Dog(string name):Animal(name){}
void bark() { cout <<_name<<" "<<"Dog:wang wang" << endl; }
};
class Pig :public Animal {
public:
Pig(string name):Animal(name){}
void bark() { cout <<_name<<" " << "Pig:heng heng" << endl; }
};
//void bark(Animal& animal) {
// animal .bark();
//}
void bark(Animal* p) {
p->bark();
}
int main() {
string str = "喵咪";
Cat cat(str);
Dog dog("二哈");
Pig pig("佩奇");
/*bark(cat);
bark(dog);
bark(pig);*/
bark(&cat);
bark(&dog);
bark(&pig);
return 0;
}
class Car {
public:
Car(string name,double oil)
:_name(name)
,_oil(oil)
{}
double getLeftMiles() {//动态绑定
return _oil * getMilesPreGallon();
}
string getName() {
return _name;
}
protected:
string _name;
double _oil;//虚析构函数
virtual double getMilesPreGallon() = 0;
};
class Benz :public Car {
public:
Benz(string name,double oil)
:Car(name,oil)
{}
double getMilesPreGallon() {
return 22.0;
}
};
class Audi :public Car {
public:
Audi(string name ,double oil)
:Car(name,oil)
{}
double getMilesPreGallon() {
return 20.0;
}
};
class BWM :public Car {
public:
BWM(string name,double oil)
:Car(name,oil)
{}
double getMilesPreGallon() {
return 18.0;
}
};
void showLeftMiles(Car& car) {
cout <<"name:" << car.getName() << "LeftMiles:" << car.getLeftMiles() << endl;//静态绑定
//call getLeftMiles()
}
int main() {
Benz b1("奔驰", 20);
Audi a("奥迪", 20);
BWM b2("宝马", 20);
showLeftMiles(b1);
showLeftMiles(a);
showLeftMiles(b2);
return 0;
}
class Base {
public:
Base(){ }
virtual void show(int i=10) { cout << "Base::show()i:"<<i<< endl; }
};
class Derive:public Base{
public:
Derive(){}
void show(int i = 20) { cout << "Derive::show()i:" << i << endl; }
};
int main() {
Base* pd = new Derive();
pd->show();//10
delete pd;
return 0;
}
class Base {
public:
Base() {//ptr->Base::vftable
clear();
cout << "Base()" << endl;
}
void clear() {
memset(this, 0, sizeof(*this));
}
virtual void show() { cout << "Base::show()" << endl; }
};
class Derive :public Base {
public:
Derive() { //ptr ->Derive ::vftable
cout << "Derive()" << endl; }
void show() { cout << "Derive::show()" << endl; }
};
int main() {
/*Base* pb = new Base();
pb->show();
delete pb;*/
Derive* pd = new Derive();//Base()->Derive()
pd->show();
delete pd;
return 0;
}