动态绑定 抽象类

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值