这样:
#include<iostream>
using namespace std;
class container //定义抽象类conyainer
{public:
container(........)
virtual xx(){}; //虚函数
virtual xx(){}; //虚函数
virtual void xxx() const =0; //纯虚函数
private:
.......
};
class xxxx:public container //公用派生类xxxx
{public:
vitual xxxxx{....} //对虚函数再定义
扩展资料:
注意事项
多态是在不同继承关系的类对象,去调用同一函数,产生了不同的行为。比如Student继承了Person.Person买票就是全价,而Student买票就是半价。
那么在继承中要构成多态还需要两个条件:
1、调用函数的对象必须是指针或者引用。
2、被调用的函数必须是虚函数,且完成了虚函数的重写。
虚函数:在类的成员函数前加virtual关键字。
class Person{public:
virtual void BuyTicket()
{
cout << "买票-全价" << endl;
}};
虚函数的重写:派生类中有一个跟基类的完全相同的虚函数,就称子类的虚函数重写了基类的虚函数。“完全相同”是指:函数名、参数、返回值都相同。另外,虚函数的重写也叫做虚函数的覆盖。
C++程序:
#include <iostream>
using namespace std;
const double PI = 3.14159;
//抽象类:容器类
class Container
{
public:
//纯虚函数:计算容器的表面积
virtual double area() = 0;
//纯虚函数:计算容器的体积
virtual double volume() = 0;
};
//长方体类:继承容器类
class Cuboid : public Container
{
protected:
double length; //长
double width; //宽
double height; //高
public:
Cuboid() : length(0), width(0), height(0)
{
}
Cuboid(double length, double width, double height) : length(length), width(width), height(height)
{
}
double area()
{
return 2 * (length * width + length * height + width * height);
}
double volume()
{
return length * width * height;
}
};
//球类:继承容器类
class Spherome : public Container
{
protected:
double radius; //半径
public:
Spherome() : radius(0)
{
}
Spherome(double radius) : radius(radius)
{
}
double area()
{
return 4 * PI * radius * radius;
}
double volume()
{
return 4 * PI * radius * radius * radius / 3;
}
};
void main()
{
Container *c;
c = new Cuboid(2, 3, 4);
cout<<"长方体的表面积:"<<c->area()<<endl;
cout<<"长方体的体积:"<<c->volume()<<endl;
c = new Spherome(2);
cout<<"球体的表面积:"<<c->area()<<endl;
cout<<"球体的体积:"<<c->volume()<<endl;
}
运行测试:
长方体的表面积:52
长方体的体积:24
球体的表面积:50.2654
球体的体积:33.5103
给你一个相关的案例参考着写,顺便学习一下相关知识,这个虚基类是一个图形类,子类继承 并实现它的计算面积方法,
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
using namespace std;
class Graph
{
public:
Graph();
virtual ~Graph();
virtual double calcuArea();
};
#endif
------------------------------
#include "Graph.h"
Graph::Graph()
{
cout<<"Graph()"<<endl;
}
Graph::~Graph()
{
cout<<"~Graph()"<<endl;
}
double Graph::calcuArea()
{
cout<<"Graph---->calcuArea()"<<endl;
return 0.0;
}
-------------------
子类Rectangle 计算长方形面积
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Graph.h"
class Rectangle:virtual public Graph
{
public:
Rectangle(double _h,double _w);
~Rectangle();
virtual double calcuArea();
protected:
double m_dLength;
double m_dWidth;
};
#endif
--------------------------
#include "Rectangle.h"
Rectangle::Rectangle(double _h,double _w)
{
cout<<"Rectangle(double _h,double _w)"<<endl;
this->m_dLength =_h;
this->m_dWidth =_w;
}
----------------------------------
Rectangle::~Rectangle()
{
cout<<"~Rectangle()"<<endl;
}
double Rectangle::calcuArea()
{
cout<<"Rectangle----->calcuArea()"<<endl;
return m_dWidth*m_dLength;
}
#include<iostream>
class container
{
public:
virtual ~container(void){}
virtual double area(void) = 0; //表面积
virtual double volume(void) = 0; //体积
void print(void){std::cout<<"表面积:"<<area()<<"\t体积:"<<volume()<<std::endl;}
};
class cube : public container //立方体
{
public:
explicit cube(double side):container(void)
{
this->side = side;
}
~cube(void){}
double area(void)
{
return 6*side*side;
}
double volume(void)
{
return side*side*side;
}
private:
double side;
};
class cylinder : public container //圆柱体
{
public:
cylinder(double radius,double height):container(void)
{
this->radius = radius;
this->height = height;
}
~cylindervoid){}
double area(void)
{
double base = 3.14*radius*radius;
double sideface = 2*3.14*radius*height;
return 2*base+sideface;
}
double volume(void)
{
return 3.14*radius*radius*height;
}
private:
double radius;
double height;
};
int main(void)
{
container a = new cube(5.0);
container b = new cylinder(1.5,3.0);
a->print();
b->print();
delete a;
delete b;
return 0;
}
using namespace std;
class container //定义抽象类conyainer
{public:
container(........)
virtual xx(){}; //虚函数
virtual xx(){}; //虚函数
virtual void xxx() const =0; //纯虚函数
private:
.......
};
class xxxx:public container //公用派生类xxxx
{public:
vitual xxxxx{....} //对虚函数再定义
..................
这个是抽象类大概内容,还有你没说清楚,还有你定义抽象类应该是建立派生类,你也没说明白。
这个抽象类派生出哪些类,以及具体的作用和结构,只能写个大概了。
说得不好之处请见谅哈~