20_6_7 3
point.h
#pragma once
#ifndef POINT_H
#define POINT_H
#include<iostream>
#include<stdlib.h>
using namespace std;
const double PI = 3.1415926;
//定义基类
class Point {
private:
int x, y;
public:
Point(int xx, int yy) {
x = xx;
y = yy;
}
int getX() const {
return x;
}
int getY()const {
return y;
}
friend ostream& operator<<(ostream&, const Point&);
};
ostream& operator<<(ostream& show, const Point& p) {
show << "(" << p.x << "," << p.y << ")" << endl;
return show;
}
#endif
Circle.h
#pragma once
#ifndef CIRCLE_H
#define CIRCLE_H
#include<iostream>
#include<stdlib.h>
#include"point.h"
using namespace std;
//定义子类圆,public的继承
class Cirlce :public Point {
protected:
int r;
public:
Cirlce(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 Cirlce&);
};
ostream& operator<<(ostream& show, const Cirlce& c) {
show << "(" << c.getX() << "," << c.getY() << "),半径" << c.r << ",周长:" << c.circumference() << endl;
return show;
}
#endif
Cylinder.h
#pragma once
#ifndef CYLINDER_H
#define CYLINDER_H
#include"Circle.h"
class Cylinder :public Cirlce {
private:
int h;
public:
Cylinder(int xx, int yy, int rr, int hh = 0) :Cirlce(xx, yy, rr) {
h = hh;
}
int getH() {
return h;
}
double Cy_area() const {
return 2 * area() + 2 * PI * r * h;
}
double volume() const {
return Cirlce::area() * h;
}
friend ostream& operator<<(ostream& show, const Cylinder& cy) {
show << "(" << cy.getX() << "," << cy.getY() << "),半径" << cy.r << ",表面积:" << cy.Cy_area() << "体积:" << cy.volume() << endl;
return show;
}
};
#endif
main.cpp
#include<iostream>
#include<stdlib.h>
#include"Cylinder.h"
using namespace std;
int main(void) {
Point objP(3, 5);
cout << "横坐标的值:" << objP.getX() << " ";
cout << "纵坐标的值:" << objP.getY() << endl;
Cirlce objC(3, 5, 9);
cout << "圆的半径:" << objC.getR() << endl;
cout << "圆的周长:" << objC.circumference() << endl;
cout << "圆的面积:" << objC.area() << endl;
Cylinder objCy(3, 5, 9, 12);
cout << "圆柱的表面积:" << objCy.Cy_area() << endl;
cout << "圆柱的体积:" << objCy.volume() << endl;
system("pause");
return 0;
}

浙公网安备 33010602011771号