纯虚函数(附源码)

strncmp:

​ 是字符串比较函数 int strncmp ( const char * str1, const char * str2, size_t n );功能是把 str1 和 str2 进行比较,最多比较前 n 个字节,若str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 小于s2,则返回小于0的值。

atoi函数:

​ 函数原型int atoi(const char *nptr) ,扫描字符串,自动跳过空格,,如果不能转化为整数则会返回0.如果可以转换默认为十进制。相应的atoi则为将字符串转化为浮点数{double atof(const char *nptr)}。默认返回double类型。

​ 纯虚函数的意义:

​ 是一类特殊的虚函数,没有函数体,只有函数定义virtual <类型><函数名>(<参数表>)=0;其作用类似与Java中的抽象方法,包含纯虚函数的类也叫做抽象类,抽象类不能声明本类对象,但是可以有本类的指针,也可以派生出类。

\#*include*"iostream"

\#*include* <cstring>

using namespace std;

class Auto {

protected:

  string stypename;//*类型名*

  int npassengers;//*乘客数量*

  string smanufacturer;//*厂商名称*

public:

  *Auto*() {//*构造函数*

​    stypename *=* "Auto";

​    npassengers = 0;

​    smanufacturer *=* "no manufacturer";

  }

  *virtual* *~Auto*() {};//*虚析构函数*

  *static* void *TrimLine*(char*** sbuf) {//*静态函数,整理字符串,去掉串尾换行字符*

​    *while* (*sbuf != '\0') {

​      *if* (*sbuf == '\r' || *sbuf == '\n') {

​        *sbuf = '\0'; *break*;

​      }

​      sbuf++;

​    }

  }

  *virtual* bool *Input*(FILE*** fp) = 0;

  *virtual* void *Show*() = 0;

};

class Car :public Auto {

public:

  *Car*() { stypename *=* "Car"; }//*构造函数*

  bool *Input*(FILE*** fp) {

​    char sbuf[100];

​    *fgets*(sbuf, 100, fp);

​    *TrimLine*(sbuf);

​    smanufacturer *=* sbuf;

​    *fgets*(sbuf, 100, fp);

​    npassengers = *atoi*(sbuf);//*字符串->整数*

​    *return* true;

  }

  void *Show*() {

​    cout *<<* "Style: " *<<* stypename *<<* *endl*;

​    cout *<<* "Manud=facturer " *<<* smanufacturer *<<* *endl*;

​    cout *<<* "Passengers: " *<<* npassengers *<<* *endl*;

  }

};

class Truck :public Car {

protected:

  float fload;

public:

  *Truck*() {

​    stypename *=* "Truck";

​    fload = 0;

  }

  bool *Input*(FILE*** fp) {

​    char sbuf[100];

​    Car::*Input*(fp);

​    *fgets*(sbuf, 100, fp);

​    fload = *atof*(sbuf);//*字符串->浮点数*

​    *return* true;

  }

  void *Show*() {

​    Car::*Show*();

​    cout *<<* "Load: " *<<* fload *<<* *endl*;

  }

};

class Crane :public Truck {

protected:

  float fheight;

public:

  *Crane*() { stypename *=* "Crane"; fheight = 0; }

  bool *Input*(FILE*** fp) {

​    char sbuf[100];

​    Truck::*Input*(fp);

​    *fgets*(sbuf, 100, fp);

​    fheight = *atof*(sbuf);

​    *return* true;

  }

  void *Show*() {

​    Truck::*Show*();

​    cout *<<* "Height:" *<<* fheight *<<* *endl*;

  }

};

int *main*() {

  FILE* stream;

  stream = *fopen*("autos.txt", "r");

  *if* (stream == *NULL*) {

​    cout *<<* "Can't open the file." *<<* *endl*;

​    *return* 0;

  }

  Auto* autos[3];

  char sbuf[100];

  int index = 0;

  *while* (*fgets*(sbuf, 100, stream) != *NULL* && index < 3) {

​    *if* (*strncmp*(sbuf, "Car", 3) == 0) {

​      autos[index] = new *Car*();

​    }

​    *else* *if* (*strncmp*(sbuf, "Truck", 5) == 0)

​      autos[index] = new *Truck*();

​    *else* *if* (*strncmp*(sbuf, "Crane", 5) == 0)

​      autos[index] = new *Crane*();

​    *else* *break*;

​    autos[index]->*Input*(stream);

​    index++;

  }

  *fclose*(stream);

  *for* (int i = 0; i < index; i++) {

​    autos[i]->*Show*();

​    cout *<<* *endl*;

​    delete autos[i];

  }

  *return* 0;

}
运行结果:
Style: Car
Manud=facturer Volkswagen
Passengers: 5
Style: Truck
Manud=facturer GM
Passengers: 2
Load: 5

Style: Crane
Manud=facturer Ford
Passengers: 1
Load: 4
Height:20

此程序中应用了继承和派生以及虚函数(动态多态)的一些内容。


#include<iostream>
#define PI 3.14
using namespace std;
class shape {
public:
	virtual double area() = 0;
};
class circle :public shape{
public:
	int r;
	circle(int r)
	{
		this->r = r;
	}
	double area()
	{
		return PI * r * r;
	}
};
class square :public shape {
public:
	int h;
	square(int h)
	{
		this->h = h;
	}
	double area()
	{
		return 1.0*h * h;
	}
};
class rectangle :public shape {
public:
	int h, w;
	rectangle(int h, int w)
	{
		this->h = h;
		this->w = w;
	}
	double area()
	{
		return 1.0*h * w;
	}
};
class trapezoid :public shape {
public:
	int upper;
	int lower;
	int high;
	trapezoid(int a, int b, int c)
	{
		upper = a;
		lower = b;
		high = c;
	}
	double area()
	{
		return (1.0*upper + 1.0*lower) * high / 2.0;
	}
};
class triangle :public shape{
public:
	int lower;
	int high;
	triangle(int lower, int high)
	{
		this->high = high;
		this->lower = lower;
	}
	double area()
	{
		return 1.0*lower * high / 2.0;
	}
};
int main()
{
	circle c(2);
	square s(2);
	rectangle r(2, 4);
	trapezoid t(2, 4, 6);
	triangle t1(2, 4);
	shape* sq[5] = {&c,&s,&r,&t,&t1};//需要自己给每个元素赋值,想知道可以不可以直接
    //shape *sq[5] = {&circle(2), &square(2), &rectangle(2, 4), &trapezoid(2, 4, 6), &triangle(2, 4)};
    //应该改成这个:因为在创建堆对象时,new本身就是一个地址,没有必要去重复取地址
    //shape *sq[5] = {new circle(2), new square(2), new rectangle(2, 4), new trapezoid(2, 4, 6), new triangle(2, 4)};
	double sum = 0;
	double u[5] = { 0 };
	for (int i = 0; i < 5; i++) {
		u[i] = sq[i]->area();
		sum = sum + sq[i]->area();
	}
	cout <<"面积之和:" <<sum << endl;
	sum = 0;
	double temp = 0;
    shape *temp1;
    for (int i = 0; i < 5; i++) {
		for (int j = i + 1; j < 5; j++) {
			if (u[i] > u[j]) {
				temp = u[i];
                temp1 = sq[i];
                u[i] = u[j];
                sq[i] = sq[j];
                u[j] = temp;
                sq[j] = temp1;
            }
		}
	}
    //按面积数组排序
    cout << "面积排序" << endl;
    for (int i = 0; i < 5; i++) {
		cout << u[i] << endl;
	}
    //指针排序
    for (int i = 0; i < 5;i++){
        cout << sq[i]->area() << endl;
    }
        return 0;
}

利用虚函数可以实现在主动结束基类指针指向的派生类对象时可以实现连锁析构,实现派生类和基类均析构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值