#include
using namespace std;
class base
{
public:
virtual void disp();
};
class triangle:public base
{
private:
double bottom;
double height;
public:
triangle(double b,double h)
{
bottom = b;
height = h;
}
void disp()
{
cout<<"三角形面积:"<
};
class square:public base
{
private :
double a;
public:
square(double a1)
{
a = a1;
}
void disp()
{
cout<<"正方形面积:"<卜前}
};
#define pi 3.1415
class circle:public base
{
private:
double r;
public:
circle(double r1)
{
r = r1;
}
void disp()
{
cout<<"圆形面积:"<
};
int main() {
base *p;
triangle t(20, 20); //第一个参数为三角形底边,第二个参数为底边上的高
square s(20);
circle c(20);
p = &t;
p->disp();
p = &s;
p->disp();
p = &c;
p->disp();
return 0;
}
扩展资料:
编译方式下,首先通过一个对应于所用程序设计语言的编译程序对源程序进行处理,经过对源程序的词法分析、语法分析、语意分析、代码生成和代码优化等阶段将所处理的源程序转换为用二进制代码表示的目标程型拿序。
然后通过连接程序处理将程序中所用的函数调用、系统功能调用等嵌入到目标程序中,构成一个可以连续执行的二进制执行文件。调用这个执行文件就可以实现程序员在对应源程序文件中所指定的相应功能。
参考资料来源:卜弊搭百度百科-编程
#include
#include
#include
using namespace std;
class base
{
public:
virtual void disp() = 0;
};
class triangle : public base
{
public:
triangle(int base, int height) : m_base(base), m_height(height) {}
void disp()
{
cout << "三角形面积: "渗型改 << m_base* m_height / 2 << endl;
}
private:
int m_base;
int m_height;
};
class square : public base
{
public:
square(int side) : m_side(side) {}
void disp()
{
cout << "正方形面丛判积: " << m_side* m_side << endl;
}
private:
int m_side;
};
class circle : public base
{
public:
circle(int radius) : m_radius(radius) {}
void disp()
{
cout << "圆租陆形面积: "
<< setprecision(5) // nasty!!
<< m_radius * m_radius * M_PI << endl;
}
private:
int m_radius;
};
1.首先实现基类:
class abstract base
{
virtual void disp() = 0;
};
2.依次实现三角形,正方形,圆形面积的计算:
#include
using namespace std;
class triangle:public base
{
private:
double bottom;
double height;
triangle(double b,double h)
{
bottom = b;
height = h;
}
void disp()
{
cout<<"三角形面积:"<
};
class square:public base
{
private :
double a;
square(double a1)
{
a = a1;
}
void disp()
{
cout<<"正方形面积:"枝悔<}
}
#define pi 3.141
class circle:public base
{
private:
double r;
circle(double r1)
{
r = r1;
}
void disp()
{
cout<<念猛"圆形面积:"<
}