谭浩强第四版C++12.5

class shape {
public:
    virtual float Printarea()const { return 0; };

};
class circle :public shape {
public:
    circle(float r) :radius(r) {}
    virtual float Printarea()const;//覆写面积函数
private:
    float radius;
};
float circle::Printarea()const {
    return (radius * radius * 3.14);
}


class rectangle :public shape {
public:
    rectangle(float w,float h) :weight(w),height(h) {}
    virtual float Printarea()const;
private:
    float weight,height;
};
float rectangle::Printarea()const {
    return weight * height;
}


class triangle :public shape {
public:
    triangle(float z, float x, float n) :a(z), b(x), c(n) {}
    virtual float Printarea()const;
private:
    float a, b, c;
    float p = (a + b + c) / 2;
};
float triangle::Printarea() const {
    return sqrt(p * (p - a) * (p - b) * (p - c));
}

class trapezoid :public shape {
public:
    trapezoid(float a,float b, float h):s(a),x(b),height(h){}
    virtual float Printarea()const;
private:
    float s, x, height;
};
float trapezoid::Printarea()const {
    return ((s + x) * height) / 2;

}

class square :public shape {
public:
square(float r):a(r){}
virtual float Printarea()const;
private:
    float a;
};
float square::Printarea()const {
    return a * a;
}

int main() {
    
    circle c(3);
    rectangle r(3, 4);
    triangle t(3, 4, 5);
    square s(2);
    trapezoid tr(4, 5, 8);
    shape *p[5] = {&c,&t,&s,&tr,&r};
    float totalarea = 0;
    for (int i = 0; i < 5; i++) {
        totalarea += p[i]->Printarea();
    }
    cout << totalarea;
}

C++程序设计,谭浩强编著,清华大学出版社。 课件制作:南京理工大学 陈清华 朱红 C语言的主要特点是: 1、C语言是一种结构化的程序设计语言,语言本身简洁、使用灵活方便。既适用于设计和编写大的系统程序,又适用于编写小的控制程序,也适用科学计算。 2、它既有高级语言的特点,又具有汇编语言的特点。运算符丰富,除了提供对数据的算术逻辑运算外,还提供了二进制的位运算。并且也提供了灵活的数据结构。用C语言编写的程序表述灵活方便,功能强大。用C语言开发的程序,其结构性好,目标程序质量高,程序执行效率高。 3、程序的可移植性好。用C语言在某一种型号的计算机上开发的程序,基本上可以不作修改,而直接移植到其它型号和不同档次的计算机上运行。 4、程序的语法结构不够严密,程序设计的自由度大。这对于比较精通C语言的程序设计者来说,可以设计出高质量的非常通用的程序。但对于初学者来说,要能比较熟练运用C语言来编写程序,并不是一件容易的事情。与其它高级语言相比而言,调试程序比较困难。往往是编好程序输入计算机后,编译时容易通过,而在执行时还会出错。但只要对C语言的语法规则真正领会,编写程序及调试程序还是比较容易掌握的。 为了克服C语言本身存在的缺点,并保持C语言简洁、高效,与汇编语言接近的特点,1980年,贝尔实验室的Bjarne Stroustrup博士及其同事对C语言进行了改进和扩充,并把Simula 67中类的概念引入到C中。并在1983年由Rick Maseitti提议正式命名为C++(C Plus Plus)。后来,又把运算符的重载、引用、虚函数等功能加入到C++中,使C++的功能日趋完善。 当前用得较为广泛的C++有:VC++ (Visual C Plus Plus)、 BC++(Borland C Plus Plus)、AT&T C++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值