写这种.h和.cpp文件分开的大程序,虽然对很多人来说很简单,对自己来说算是第一次吧,好好学C++,加油~
题目:定义Point类,由Point派生出Circle类,再由Circle派生出Cylinder类。将类的定义部分分别作为3个头文件,对他们的成员函数的定义分别作为3个源文件
1、Point.h文件
1 #ifndef POINT_H 2 #define POINT_H 3 #include<iostream> //头文件也需要包含这个 4 using namespace std; 5 6 class Point 7 { 8 protected: 9 float x, y; 10 public: 11 Point(float a = 0, float b = 0); //带默认参数的构造函数的声明 12 float getX(); 13 float getY(); 14 void setPoint(float, float); 15 friend ostream& operator<<(ostream &output, const Point &p); //运算符重载 16 17 }; 18 19 #endif
2、Circle.h文件
1 #ifndef CIRCLE_H 2 #define CIRCLE_H 3 4 #include "Point.h" 5 6 class Circle:public Point 7 { 8 public: 9 Circle(float a = 0, float b = 0, float r = 0); 10 float getR(); 11 void setR(float r); 12 float area(); 13 friend ostream& operator<<(ostream &output, Circle &c); //运算符重载 14 protected: 15 float radius; 16 }; 17 18 #endif
3、Cylinder.h文件
1 #ifndef CYLINDER_H 2 #define CYLINDER_H 3 4 #include "Circle.h" 5 6 class Cylinder :public Circle 7 { 8 public: 9 Cylinder(float a = 0, float b = 0, float r = 0, float h = 0); 10 void setH(float h); 11 float getH(); 12 float area(); 13 float volume(); 14 friend ostream& operator<<(ostream &, Cylinder &); 15 private: 16 float height; 17 }; 18 19 #endif
4、Point.cpp文件
1 #include<iostream> 2 #include"Point.h" 3 using namespace std; 4 5 Point::Point(float a, float b) 6 { 7 x = a; 8 y = b; 9 } 10 11 float Point::getX() 12 { 13 return x; 14 } 15 float Point::getY() 16 { 17 return y; 18 } 19 void Point::setPoint(float a, float b) 20 { 21 x = a; 22 y = b; 23 } 24 25 ostream& operator<<(ostream &output, const Point &p) //重载运算符“<<” 26 { 27 output << "(" << p.x << "," << p.y << ")" << endl; 28 return output; 29 }
5、Circle.cpp文件
1 #include<iostream> 2 #include"Circle.h" 3 using namespace std; 4 5 Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) { } 6 7 float Circle::getR() 8 { 9 return radius; 10 } 11 12 void Circle::setR(float r) 13 { 14 radius = r; 15 } 16 17 float Circle::area() 18 { 19 return 3.14*radius*radius; 20 } 21 22 ostream& operator<<(ostream &output, Circle &c) //运算符重载 23 { 24 output << "圆心:(" << c.x << "," << c.y << ") 半径:" << c.radius << " 面积:" << c.area() << endl; 25 return output; 26 }
6、Cylinder.cpp文件
1 #include<iostream> 2 #include"Cylinder.h" 3 using namespace std; 4 5 Cylinder::Cylinder(float a, float b, float r, float h) :Circle(a, b, r), height(h) { } 6 7 void Cylinder::setH(float h) 8 { 9 height = h; 10 } 11 12 float Cylinder::getH() 13 { 14 return height; 15 } 16 17 float Cylinder::area() 18 { 19 return 2 * Circle::area() + 2 * 3.14*radius*height; 20 } 21 22 float Cylinder::volume() 23 { 24 return Circle::area()*height; 25 } 26 27 ostream& operator<<(ostream &output, Cylinder &cy) //cy.area() 属于同名覆盖 28 { 29 output << "圆心:(" << cy.x << "," << cy.y << ") 半径:" << cy.radius << " 高:" << cy.height << " 面积: " << cy.area() << " 体积:" << cy.volume() << endl; 30 return output; 31 }
7、main函数
1 //习题6.1 2 //#include<iostream> 3 //#include"Point.h" 4 //#include"Circle.h" 5 #include"Cylinder.h" 6 //using namespace std; 7 8 int main() 9 { 10 Cylinder cy1(3, 3, 5, 6); 11 cout << "original cylinder:" << cy1 << endl; 12 cy1.setPoint(-2, -2); 13 cy1.setR(8); 14 cy1.setH(10); 15 cout << "new cylinder: " << cy1 << endl; 16 Point &p1 = cy1; 17 cout << "as a point: " << p1 << endl; 18 Circle &c1 = cy1; 19 cout << "as a circle: " << c1 << endl; 20 return 0; 21 }
运行结果:
总结:
1、在写头文件是要注意写
#ifndef POINT_H
#define POINT_H
……
#endif
可以避免多次包含同一头文件,防止重定义
2、基本的头文件要写在.h文件中