C++——虚函数和抽象类的使用方法 – 作者:Johnson666

编程题

1.虚函数的使用
代码:

#include<iostream>
using namespace std;
class X
{
private:
int Xa,Xb;
public:
X(){Xa=0;Xb=0;}
virtual void display();
virtual ~ X(){cout<<"析构X"<<endl;}
};
void X::display(){cout<<"X的值为:("<<Xa<<","<<Xb<<")"<<endl;}

class Y:public X
{
private:
	int Ya,Yb;
public:
	 Y(int a,int b):Ya(a),Yb(b){}
	virtual void display();
	virtual ~ Y(){cout<<"析构Y"<<endl;}
};
void Y::display(){cout<<"Y的值为:("<<Ya<<","<<Yb<<")"<<endl;}


class Z:public X
{
private:
int Za,Zb;
public:
Z(int a,int b):Za(a),Zb(b){}
virtual void display();
virtual ~Z(){cout<<"析构Z"<<endl;}

};
void Z::display(){cout<<"Z的值为:("<<Za<<","<<Zb<<")"<<endl;}
int main()
{
X x;
X *x1=new Y(1,2);
X *x2=new Z(3,4);
x1->display();
x2->display();
x.display();
delete x1;
delete x2;
return 0;
}

结果:image.png2.掌握虚函数的使用方法

#include<iostream>
using namespace std;
class Point 
{
private:
int x,y;
public:
Point(int i1,int i2)
{
x=i1;
y=i2;
}
virtual int area() const
{
return 0;
}
};
class Rect:public Point 
{
private:
int l,w;
public:
	Rect(int i1,int i2,int l1,int w1):Point(i1,i2)
	{
	l=l1;
	w=w1;
	}
	virtual int area() const
	{
	return l*w;
	}
};
void fun(Point &p)
{
cout<<p.area()<<endl;
}
int main()
{
Rect rec(2,3,4,5);
fun(rec);
return 0;
}

image.png3.掌握抽象类的使用方法。

#include<iostream>
using namespace std;
const double PI=3.14159;
class Shapes
{
protected:
	int x,y;
public:
	void setvalue(int d,int w=0){x=d;y=w;}
	virtual void disp()=0;
};
class Rect:public Shapes
{
public:
	void disp(){cout<<"矩形面积:"<<x*y<<endl;}	
};
class Circle:public Shapes
{
public:
	void disp(){cout<<"圆面积;"<<PI*x*x<<endl;}
};
int main()
{
Shapes *ptr[2];
Rect r1;
Circle c1;
ptr[0]=&r1;
ptr[0]->setvalue(10,5);
ptr[0]->disp();
ptr[1]=&c1;
ptr[1]->setvalue(10);
ptr[1]->disp();
return 0;
}

image.png4.掌握虚析构函数的使用方法

#include<iostream>
using namespace std;
class A
{
public:
	virtual void fun()
	{
	cout<<"class A"<<endl;
	}
	virtual ~A()
	{
	cout<<"A的对象析构完成"<<endl;
	}
};
class B:public A
{
public:
virtual void fun()
	{
	cout<<"class B"<<endl;
	}
	virtual ~B()
	{
	cout<<"B的对象析构完成"<<endl;
	}
};
int main()
{
	A *t=new A;
	B *x=new B;
	A * array[2];
	array[0]=t;
	array[1]=x;
	for(int i=0;i<2;i++)
		array[i]->fun();
	delete array[0];
	delete array[1];
return 0;
}

image.png5.某学校教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师为抽象类,派生不同职称的教师子类,编写程序求若干教师的月工资。

#include<iostream>
using namespace std;
#define count 100
class teachers
{
public:
virtual void display()=0;
};

class professor:public teachers
{
public:
	professor(){a=5000;b=50;}
	virtual void display()
	{
	cout<<"教授工资为:"<<a+b*count<<endl;
	}

private:
	int a;
	int b;
};
class adjunctprofessor:public teachers
{
public:
	adjunctprofessor(){a=3000;b=30;}
	virtual void display()
	{
	cout<<"副教授工资为:"<<a+b*count<<endl;
	}

private:
	int a;
	int b;
};

class docent:public teachers
{
public:
	docent(){a=2000;b=20;}
	virtual void display()
	{
	cout<<"讲师工资为:"<<a+b*count<<endl;
	}

private:
	int a;
	int b;
};
int main()
{
	teachers *t[3];
	professor p;
	adjunctprofessor a;
	docent d;
	t[0]=&p;
	t[1]=&a;
	t[2]=&d;
	t[0]->display();
	t[1]->display();
	t[2]->display();
	return 0;
}

结果:image.png

来源:freebuf.com 2021-06-10 14:47:16 by: Johnson666

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论