C++继承

博客围绕C++继承展开,虽暂无具体内容,但可知聚焦于C++这一编程语言的继承特性,继承是C++面向对象编程的重要概念,能实现代码复用等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Polygon.h:
#pragma once
#include <iostream> 
#include <iomanip>
using namespace std;

class Polygon//多边形
{
public:
	 void setbian(double t);
protected:
	double a;
};
class Square : public Polygon//正方形
{
public:
	double area();
	double perimeter();

};
class Triangle :public Polygon//三角形
{

public:
	double area();
	double perimeter();
};



Polygon.cpp:

#include"Polygon.h"
using namespace std;
void Polygon::setbian(double t) { a = t; };
double Square::perimeter() { cout << "正方形周长:" << " "; return 4 * a; };
double Square::area() { cout << "正方形面积:" << " "; return a*a; };
double Triangle::perimeter() { cout << "三角形周长" << ends; return 3 * a; };
double Triangle::area() { cout << "三角形面积" << ends; return (a*0.433)*(a*0.433); };

PolygonArea.cpp :

#include"Polygon.h";
void PolygonArea()
{
	Polygon *po;
	Square square;
	po=&square;
	po->setbian(5);
	cout << square.area() << endl;
	cout << square.perimeter() << endl;
	Triangle triangle;
	po = &triangle;
	po->setbian(5);
	cout << triangle.area() << endl;
	cout << triangle.perimeter() << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值