抽象类的基本用法

纯虚函数和抽象类

    纯虚函数是一个在基类中说明的虚函数,在积累中没有定义,要求任何派生类都定义自己的版本

    纯虚函数为各派生类提供一个公共界面(接口的封装和设计、软件的模块功能划分)

    纯虚函数说明形式:

    virtual 类型 函数名(参数列表)=0

    一个纯虚函数的基类成为抽象类;

 

例如:Shape 为一个抽象类

    Shape x;//error          抽象类不能建立对象

    Shape *p;//ok            可以声明抽象类的指针

    Shape f();//error        抽象类不能作为返回类型

    void g(Shape)//error 抽象类不能作为参数类型

    Shape &h(Shape &)//ok可以声明抽象类的引用

#include<iostream>
using namespace std;

class A//一个具有纯虚函数的基类叫做抽象类,抽象类不能实例化
{
public:
	virtual void getArea()=0;
};
class A1 :public A
{
public:
	A1(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	virtual void getArea()
	{
		cout << 3 * a*a << endl;
	}
private:
	int a;
	int b;
};
class A2 :public A
{
public:
	A2(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	virtual void getArea()
	{
		cout<< a*b<<endl;
	}
private:
	int a;
	int b;
};
void howprintf(A *base)
{
	base->getArea();
}
void main()
{
	//A *p;
	A1 a1(10, 10);
	A2 a2(20, 30);
    howprintf(&a1);
	howprintf(&a2);
	system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文是一个word讲义 介绍了 Figure circle Rectangle等类在接口中计抽象类中的用法 抽象类 接口多态 public class Polymorphism { public static void main(String args[]) { Triangle t=new Triangle(5.0,2.0); t.show_area(); Rectangle r=new Rectangle(3.0,4.0); r.show_area(); Circle c=new Circle(10.0); c.show_area(); } } abstract class Figure { protected double x=10.0,y=5.0; abstract void show_area(); } class Triangle extends Figure { Triangle(double a,double b) { x=a; y=b; } void show_area() { System.out.println("triangle:"+(0.5*x*y)); } } class Rectangle extends Figure { Rectangle(double a,double b) { x=a; y=b; } void show_area() { System.out.println("rectangle:"+(x*y)); } } class Circle extends Figure { Circle(double a){x=a;} final double pi=3.1416; void show_area() { System.out.println("circle:"+(pi*x*x)); } } package inf; interface Figure { abstract double area(); } //Rectangle.java package inf; /** * * @author tai */ public class Rectangle implements Figure{ double width,height; public Rectangle(double w,double h) //构造方法 { width=w; height=h; } public double area(){ return (width * height); } } //Triangle.Java package inf; /** * * @author tai */ class Triangle implements Figure { double a; double b; double c; Triangle(double a,double b,double c) { this.a=a; this.b=b; this.c=c; } public double area() { double p=(a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } } //Circle.java package inf; /** * * @author tai */ class Circle implements Figure { double radius; Circle(double radius) { this.radius=radius; } public double area() { return Math.PI*radius*radius; } } 测试一: public class Test4 { public static void main(String args[]) { Triangle t=new Triangle(5.0,6.0,7.0); System.out.println("三角形面积="+t.area()); Rectangle r=new Rectangle(3.0,4.0); System.out.println("矩形面积="+r.area()); Circle c=new Circle(10.0); System.out.println("圆的面积="+c.area()); } } 测试二: package inf; import javax.swing.JOptionPane; public class Test5 { public static double totalArea1(Figure f[]) { double totalArea=0; for(Figure f1:f){ totalArea+=f1.area(); } return totalArea; } public static void main(String args[]) { Figure f[] =new Figure[3];//为什么 f[0]=new Triangle(5.0,6.0,7.0); f[1]=new Rectangle(6.0,87.0); String input=JOptionPane.showInputDialog("输入圆的半径"); Double r=Double.parseDouble(input); f[2]=new Circle(r); double totalArea=0; for(Figure f1:f){ totalArea+=f1.area(); } JOptionPane.showMessageDialog(null,"totalarea="+totalArea); JOptionPane.showMessageDialog(null,"totalarea1="+totalArea1(f)); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值