类数据成员圆柱类
在本篇文章中,我们主要介绍类数据成员的内容,自我感觉有个不错的建议和大家分享下
/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:邱学伟
* 完成日期:2013 年 5 月 19 日
* 版本号:v1.0
* 输入描述:无
* 问题描述:
* 程序输出:
* 问题分析:
* 算法计划:略
*/
/*(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,计划出各类中基本的成员函数(包含构造函数、析构函数、修改数据成员和获得数据成员的大众接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0);
void SetPoint(double,double);
double getX() const {return x;}
double getY() const {return y;}
void ShowPoint();
friend ostream & operator<<(ostream &output,const Point &c);
protected:
double x,y;
};
Point::Point(double a,double b)
{
x=a;
y=b;
}
void Point::SetPoint(double x1,double y1)
{
x=x1;
y=y1;
}
void Point::ShowPoint()
{
cout<<"Point: ("<<x<<","<<y<<")"<<endl;
}
ostream & operator<<(ostream &output,const Point &c)
{
output<<"point: ("<<c.x<<","<<c.y<<")"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(double a,double b,double c):Point(a,b),r(c){}
void SetCircle(double r2);
double Getr(){return r;}
double area();
void ShowCircle();
friend ostream & operator<<(ostream &,const Circle &C);
protected:
double r;
};
void Circle::SetCircle(double r2)
{
r=r2;
}
double Circle::area()
{
return 3.1415*r*r;
}
void Circle::ShowCircle()
{
cout<<"Circle: ("<<x<<","<<y<<") r="<<r<<endl;
}
ostream & operator<<(ostream &output,const Circle &C)
{
output<<"Circle: ("<<C.x<<","<<C.y<<") r="<<C.r<<endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(double x3,double y3,double r3,double h3):Circle(x3,y3,r3),h(h3){}
void setheight(double);
double GetCylinder();
double area();
double volume();
friend ostream &operator<<(ostream &, Cylinder &);
protected:
double h;
};
void Cylinder::setheight(double h1)
{
h=h1;
}
double Cylinder::GetCylinder()
{
return h;
}
double Cylinder::area()
{
return 2*3.1415*r*r+3.1415*2*r*h;
}
double Cylinder::volume()
{
return 3.1415*r*r*h;
}
ostream &operator<<(ostream &output, Cylinder &cy)
{
output<<"cylinder: ("<<cy.x<<","<<cy.y<<") r="<<cy.r<<" height="<<cy.h<<" area="<<cy.area()<<" volume="<<cy.volume()<<endl;
return output;
}
int main()
{
Cylinder cy(3.5,6.4,5.2,10);
cout<<"cylinder: POINT:("<<cy.getX()<<","<<cy.getY()<<") r="<<cy.Getr()<<" height="<<cy.GetCylinder()<<" are="<<cy.area()<<" volume="<<cy.volume()<<endl;
cy.setheight(15);
cy.SetCircle(7.5);
cy.SetPoint(5,5);
cout<<"cylinder: "<<cy<<endl;
return 0;
}
文章结束给大家分享下程序员的一些笑话语录: 火车
一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现 列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面是一个老奶 奶和一个年轻漂亮的姑娘。两个上前坐了下来。程序员和那个姑娘他们比较 暧昧地相互看对方。这时,火车进入山洞,车厢里一片漆黑。此时,只听见 一个亲嘴的声音,随后就听到一个响亮的巴掌声。很快火车出了山洞,他们 四个人都不说话。
那个老奶奶在喃喃道, “这个年轻小伙怎么这么无礼, 不过我很高兴我的孙女 扇了一个巴掌”。
项目经理在想,“没想到这个程序员居然这么大胆,敢去亲那姑娘,只可惜那 姑娘打错了人,居然给打了我。”
漂亮的姑娘想,“他亲了我真好,希望我的祖母没有打疼他”。
程序员坐在那里露出了笑容, “生活真好啊。 这一辈子能有几次机会可以在亲 一个美女的同时打项目经理一巴掌啊”

浙公网安备 33010602011771号