將類的定義放在頭文件中,把成員函數的實現代碼放在一個cpp文件中


寫這種.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 << "\n                  面積: " << 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文件中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM