C++实训(1.3)

头函数1:point.h

#pragma once
#if!defined(point_H)
#define point_h
#define PI 3.1415926
#include <iostream>
using namespace std;

//定义基类
class Point
{
private:
int x, y;
public:
Point(int xx, int yy)
{
x = xx;
y = yy;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
friend ostream& operator<<(ostream&, const Point&);
};

ostream& operator<<(ostream& show, const Point& p)
{
show << "(" << p.x << "," << p.y << ")" << endl;
return show;
}

#endif

头函数2:circle.h

#include "point.h"

//定义子类"圆",public的继承
class Circle :public Point
{
protected:
int r;
public:
Circle(int xx, int yy, int rr) :Point(xx, yy)
{
r = rr;
}
int Getr()
{
return r;
}
double circumference() const
{
return 2 * PI * r;
}
double area() const
{
return PI * r * r;
}
friend ostream& operator<<(ostream&, const Circle&);
};

ostream& operator<<(ostream& show, const Circle& c)
{
show << "半径" << c.r << ",周长:" << c.circumference() << "面积:" << c.area() << endl;
return show;
}

#endif

头函数3:cylinder.h

#include "circle.h"

//创建圆柱子类
class Cylinder :public Circle
{
private:
int h;
public:
Cylinder(int xx = 0, int yy = 0, int rr = 0, int hh = 0) :Circle(xx, yy, rr)
{
h = hh;
}

int Geth()
{
return h;
}

double Cy_area() const
{
return 2 * Circle::area() + 2 * PI * r * h;
}
double volume() const
{
return Circle::area() * h;
}

friend ostream& operator<<(ostream& show, const Cylinder& cy)
{
show << cy.r << ",表面积:" << cy.Cy_area() << ",体积:" << cy.volume() << endl;
return show;
}
};

 主函数:20200607_3.cpp

#include <iostream>
#include <stdlib.h>
#include "cylinder.h"
using namespace std;

int main()
{
Point objP(3,5);
cout << "横坐标的值:" << objP.Getx() << " ";
cout << "纵坐标的值:" << objP.Gety() << endl;

Circle objC(3,5,9);
cout << "圆的半径:" << objC.Getr() << endl;
cout << "圆的周长:" << objC.circumference() << endl;
cout << "圆的半径:" << objC.area() << endl;

Cylinder objCy(3,5,9,12);

cout << "圆柱的高:" << objCy.Geth() << endl;
cout << "圆柱的表面积:" << objCy.Cy_area() << endl;
cout << "圆柱的体积:" << objCy.volume() << endl;

return 1;
}

原文地址:https://www.cnblogs.com/duanqibo/p/13065156.html