程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 點類,圓類,圓柱類

點類,圓類,圓柱類

編輯:C++入門知識

print?/* Copyright (c) 2012, 煙台大學計算機學院       
* All rights reserved.       
* 文件名稱:text.cpp       
* 作    者:  胡穎      
*完成日期:2013年5月20日       
* 版 本 號:v1.0       
*       
* 輸入描述:無   
* 問題描述:(1)先建立一個Point(點)類,包含數據成員x,y(坐標點);(2)以Point為基類,派生出一個Circle(圓)類,增加數據成員 (半徑);(3)再以Circle類為直接基類,派生出一個Cylinder(圓柱體)類,再增加數據成員h(高)。要求編寫程序,設計出各類中基本的成員函數(包括構造函數、析構函數、修改數據成員和獲取數據成員的公共接口、用於輸出的重載運算符“<<”函數等),使之能用於處理以上類對象,最後求出圓格柱體的表面積、體積並輸出。      
* 程序輸出:輸出點,圓的信息和圓柱的信息 
* 問題分析:無   
* 算法設計:略       
*/              
#include <iostream>  
using namespace std; 
class Point 

public: 
      Point(double x=0,double y=0);//初始化點坐標為(0,0)  
      double getX()const 
      { 
          return x; 
      } 
      double getY()const 
      { 
          return y; 
      } 
      void setPoint(double,double); 
      friend ostream & operator<<(ostream &,const Point &); 
protected: 
      double x,y; 
}; 
Point::Point(double a,double b) 

   x=a; 
   y=b; 

void Point::setPoint(double a,double b) 

    x=a; 
    y=b; 

ostream & operator<<(ostream &output,const Point &p) 

    output<<"point:["<<p.x<<","<<p.y<<"]"<<endl; 
    return output; 

class Circle:public Point 

public: 
    Circle(double x=0,double y=0,double r=0); 
    void setRadius(double); 
    double getRadius()const ; 
    double area()const; 
    friend ostream & operator<<(ostream &,const Circle &); 
protected: 
    double radius; 
}; 
Circle::Circle(double a,double b,double r):Point(a,b),radius(r){} 
void Circle::setRadius(double r) 

    radius=r; 

double Circle::getRadius()const 

    return radius; 

double Circle::area()const 

    return 3.14159*radius*radius; 

ostream & operator<<(ostream &output,const Circle &c) 

   output<<"center=["<<c.x<<","<<c.y<<"],r="<<c.getRadius()<<",area="<<c.area()<<endl; 
   return output; 

class Cylinder:public Circle 

public: 
    Cylinder(double x=0,double y=0,double r=0,double h=0); 
    void setHeight(double); 
    double getHeight()const; 
    double area()const; 
    double volume()const; 
    friend ostream & operator<<(ostream &,const Cylinder &); 
protected: 
    double height; 
}; 
Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h){} 
void Cylinder::setHeight(double h) 

    height=h; 

double Cylinder::getHeight()const 

    return height; 

double Cylinder::area()const 

    return 2*Circle::area()+2*3.14159*radius*height;//求面積借用Circle的成員函數area的方式為Circle::area()  

double Cylinder::volume()const 

   return Circle::area()*height; 

ostream & operator<<(ostream &output,const Cylinder &cy) 

    output<<"center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<",area="<<cy.area()<<",volume="<<cy.volume()<<endl; 
    return output; 

int main() 

  Point p(3.5,6.4); 
  cout<<"x="<<p.getX( )<<",y="<<p.getY( )<<endl; 
  p.setPoint(8.5,6.8); 
  cout<<"p:"<<p<<endl; 
  Circle c(1.2,2.2,3.2); 
  cout<<"original circle:x="<<c.getX()<<",y="<<c.getY()<<",r="<<c.getRadius()<<",area="<<c.area()<<endl; 
  c.setPoint(5,5); 
  cout<<"new circle:"<<c; 
  Cylinder cy1(1.2,2.2,3.2,4.2); 
  cout<<"original Cylinder:x="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<",area="<<cy1.area()<<",volume="<<cy1.volume()<<endl; 
  cy1.setPoint(5,5); 
  cy1.setRadius(2.1); 
  cy1.setHeight(3); 
  cout<<"new Cylinder:"<<cy1; 
  return 0; 

/* Copyright (c) 2012, 煙台大學計算機學院      
* All rights reserved.      
* 文件名稱:text.cpp      
* 作    者:  胡穎     
*完成日期:2013年5月20日      
* 版 本 號:v1.0      
*      
* 輸入描述:無  
* 問題描述:(1)先建立一個Point(點)類,包含數據成員x,y(坐標點);(2)以Point為基類,派生出一個Circle(圓)類,增加數據成員 (半徑);(3)再以Circle類為直接基類,派生出一個Cylinder(圓柱體)類,再增加數據成員h(高)。要求編寫程序,設計出各類中基本的成員函數(包括構造函數、析構函數、修改數據成員和獲取數據成員的公共接口、用於輸出的重載運算符“<<”函數等),使之能用於處理以上類對象,最後求出圓格柱體的表面積、體積並輸出。     
* 程序輸出:輸出點,圓的信息和圓柱的信息
* 問題分析:無  
* 算法設計:略      
*/            
#include <iostream>
using namespace std;
class Point
{
public:
      Point(double x=0,double y=0);//初始化點坐標為(0,0)
      double getX()const
      {
          return x;
      }
      double getY()const
      {
          return y;
      }
      void setPoint(double,double);
      friend ostream & operator<<(ostream &,const Point &);
protected:
      double x,y;
};
Point::Point(double a,double b)
{
   x=a;
   y=b;
}
void Point::setPoint(double a,double b)
{
    x=a;
    y=b;
}
ostream & operator<<(ostream &output,const Point &p)
{
    output<<"point:["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}
class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    void setRadius(double);
    double getRadius()const ;
    double area()const;
    friend ostream & operator<<(ostream &,const Circle &);
protected:
    double radius;
};
Circle::Circle(double a,double b,double r):Point(a,b),radius(r){}
void Circle::setRadius(double r)
{
    radius=r;
}
double Circle::getRadius()const
{
    return radius;
}
double Circle::area()const
{
    return 3.14159*radius*radius;
}
ostream & operator<<(ostream &output,const Circle &c)
{
   output<<"center=["<<c.x<<","<<c.y<<"],r="<<c.getRadius()<<",area="<<c.area()<<endl;
   return output;
}
class Cylinder:public Circle
{
public:
    Cylinder(double x=0,double y=0,double r=0,double h=0);
    void setHeight(double);
    double getHeight()const;
    double area()const;
    double volume()const;
    friend ostream & operator<<(ostream &,const Cylinder &);
protected:
    double height;
};
Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h){}
void Cylinder::setHeight(double h)
{
    height=h;
}
double Cylinder::getHeight()const
{
    return height;
}
double Cylinder::area()const
{
    return 2*Circle::area()+2*3.14159*radius*height;//求面積借用Circle的成員函數area的方式為Circle::area()
}
double Cylinder::volume()const
{
   return Circle::area()*height;
}
ostream & operator<<(ostream &output,const Cylinder &cy)
{
    output<<"center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<",area="<<cy.area()<<",volume="<<cy.volume()<<endl;
    return output;
}
int main()
{
  Point p(3.5,6.4);
  cout<<"x="<<p.getX( )<<",y="<<p.getY( )<<endl;
  p.setPoint(8.5,6.8);
  cout<<"p:"<<p<<endl;
  Circle c(1.2,2.2,3.2);
  cout<<"original circle:x="<<c.getX()<<",y="<<c.getY()<<",r="<<c.getRadius()<<",area="<<c.area()<<endl;
  c.setPoint(5,5);
  cout<<"new circle:"<<c;
  Cylinder cy1(1.2,2.2,3.2,4.2);
  cout<<"original Cylinder:x="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<",area="<<cy1.area()<<",volume="<<cy1.volume()<<endl;
  cy1.setPoint(5,5);
  cy1.setRadius(2.1);
  cy1.setHeight(3);
  cout<<"new Cylinder:"<<cy1;
  return 0;
}

運行結果:\

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved