类的嵌套定义、使用以及用头文件实现多个文件编程(匠心之作C++ p105 22 )
我的方法1:
1 #include<iostream> 2 3 class Point{ 4 int cX,cY; 5 public: 6 void setPoint(int x,int y){ 7 cX=x;cY=y; 8 } 9 int getPointX(){ 10 return cX; 11 } 12 int getPointY(){ 13 return cY; 14 } 15 }; 16 class Circle{ 17 int cRadius;Point cCenter; 18 public: 19 void setCircle(int radius,Point center){ 20 cRadius=radius;cCenter=center; 21 } 22 int getRadius(){ 23 return cRadius; 24 } 25 int getCenterX(){ 26 return cCenter.getPointX(); 27 } 28 int getCenterY(){ 29 return cCenter.getPointY(); 30 } 31 }; 32 int main(){ 33 Point p1,cp;Circle c1; 34 p1.setPoint(7,0);cp.setPoint(0,0); 35 c1.setCircle(8,cp); 36 if((c1.getCenterX()-p1.getPointX())*(c1.getCenterX()-p1.getPointX())+ 37 (c1.getCenterY()-p1.getPointY())*(c1.getCenterY()-p1.getPointY())> 38 c1.getRadius()*c1.getRadius()) 39 std::cout<<"点在圆外\n"<<std::endl; 40 else if(((c1.getCenterX()-p1.getPointX())*(c1.getCenterX()-p1.getPointX())+ 41 (c1.getCenterY()-p1.getPointY())*(c1.getCenterY()-p1.getPointY())== 42 c1.getRadius()*c1.getRadius())) 43 std::cout<<"点在圆上\n"<<std::endl; 44 else 45 std::cout<<"点在圆内\n"<<std::endl; 46 system("pause"); 47 return 0; 48 }
老师的做法:
1 #include<iostream> 2 class Point{ 3 int cX,cY; 4 public: 5 void setPoint(int x,int y){ 6 cX=x;cY=y; 7 } 8 int getPointX(){ 9 return cX; 10 } 11 int getPointY(){ 12 return cY; 13 } 14 }; 15 class Circle{ 16 int cRadius;Point cCenter; 17 public: 18 void setCircle(int radius,Point center){ 19 cRadius=radius;cCenter=center; 20 } 21 int getRadius(){ 22 return cRadius; 23 } 24 Point getCenter(){ 25 return cCenter; 26 } 27 }; 28 int main(){ 29 Point p1,cp;//声明2个点,p1是任意点,cp是圆心 30 Circle c1; //声明圆 31 p1.setPoint(0,5); //赋值点 32 cp.setPoint(0,0); //赋值圆心 33 c1.setCircle(6,cp); //设置圆的半径及圆心 34 35 if((c1.getCenter().getPointX()-p1.getPointX())* 36 (c1.getCenter().getPointX()-p1.getPointX())+ 37 (c1.getCenter().getPointY()-p1.getPointY())* 38 (c1.getCenter().getPointY()-p1.getPointY())> 39 c1.getRadius()*c1.getRadius()) 40 std::cout<<"点在圆外\n\n"; 41 else if((c1.getCenter().getPointX()-p1.getPointX())* 42 (c1.getCenter().getPointX()-p1.getPointX())+ 43 (c1.getCenter().getPointY()-p1.getPointY())* 44 (c1.getCenter().getPointY()-p1.getPointY())== 45 c1.getRadius()*c1.getRadius()) 46 std::cout<<"点在圆上\n\n"; 47 else 48 std::cout<<"点在圆内\n\n"; 49 system("pause"); 50 return 0; 51 }
多文件实现方法,point.h:
1 #pragma once 2 #include<iostream> 3 class Point{ 4 public: 5 int cX,cY; 6 public: 7 void setPoint(int x,int y); 8 int getPointX(); 9 int getPointY(); 10 };
circle.h:
1 #pragma once 2 #include<iostream> 3 #include"point.h" 4 class Circle{ 5 int cRadius;Point cCenter; 6 public: 7 void setCircle(int radius,Point center); 8 int getRadius(); 9 Point getCenter(); 10 };
point.cpp
1 #include "point.h" 2 void Point::setPoint(int x,int y){ 3 cX=x;cY=y; 4 } 5 int Point::getPointX(){ 6 return cX; 7 } 8 int Point::getPointY(){ 9 return cY; 10 }
circle.cpp
1 #include"circle.h" 2 3 void Circle::setCircle(int radius,Point center){ 4 cRadius=radius;cCenter=center; 5 } 6 int Circle::getRadius(){ 7 return cRadius; 8 } 9 Point Circle::getCenter(){ 10 return cCenter; 11 }
主函数circle3.cpp:
1 #include<iostream> 2 #include"circle.h" 3 #include"point.h" 4 int main(){ 5 Point p1,cp;//声明2个点,p1是任意点,cp是圆心 6 Circle c1; //声明圆 7 p1.setPoint(0,9); //赋值点 8 cp.setPoint(0,0); //赋值圆心 9 c1.setCircle(6,cp); //设置圆的半径及圆心 10 int distance=(c1.getCenter().getPointX()-p1.getPointX())* 11 (c1.getCenter().getPointX()-p1.getPointX())+ 12 (c1.getCenter().getPointY()-p1.getPointY())* 13 (c1.getCenter().getPointY()-p1.getPointY()); 14 15 if(distance>c1.getRadius()*c1.getRadius()) 16 std::cout<<"点在圆外\n\n"; 17 else if(distance==c1.getRadius()*c1.getRadius()) 18 std::cout<<"点在圆上\n\n"; 19 else 20 std::cout<<"点在圆内\n\n"; 21 system("pause"); 22 return 0; 23 }