第三章作业
2015-04-22 19:05
113 查看
3.22
#include<iostream> #include<math.h> using namespace std; class Circle{ public: void area(int x,double y); void show(); private: int radius; double Area; }; void Circle::area(int x,double y) { radius=x; Area=y; } void Circle::show() { cout<<"半径为R的圆的面积为:"<<Area<<endl; } int main() { Circle circle1; int a; double b; cout<<"请输入圆的半径R:"; cin>>a; b=3.14*a*a; circle1.area(a,b); circle1.show(); return 0; }
3.23
#include<iostream> using namespace std; struct cylinder{ public: void set(double x,double y,double z) { r=x; h=y; v=z; }; void show(); private: double r; double h; double v; }; void cylinder::show() {cout<<"该圆柱体的底面半径为:"<<r<<endl<<"高为:"<<h<<endl<<"体积是:"<<v<<endl;} int main() { cylinder vo1; double a,b,c; a=3.0; b=4.0; c=3.14*a*a*b; vo1.set(a,b,c); vo1.show(); return 0; }
3.24
<p><pre name="code" class="cpp">#include<iostream> using namespace std; class Date{ public: Date(int a,int b,int c); Date(); void printDate(); void GetYear(); void GetMonth(); void GetDay(); void SetDate(int Y,int m,int d) { year=Y; month=m; day=d; } private: int year; int month; int day; }; Date::Date(int a,int b,int c):year(a),month(b),day(c) {} Date::Date() {} void Date::printDate() { cout<<year<<"."<<month<<"."<<day<<endl; } void Date::GetYear() { cout<<"输入一个年份:"; cin>>year; } void Date::GetMonth() { cout<<"输入一个月份:"; cin>>month; } void Date::GetDay() { cout<<"输入一个日子:"; cin>>day; } int main() { Date d1(2010,6,8); d1.printDate(); Date d2; d2.GetYear(); d2.GetMonth(); d2.GetDay(); d2.printDate(); Date d3(d1); d3.printDate(); return 0; }