C第4章面向对象的思想.ppt
文本预览下载声明
*;*;*;*;*;*;*;*;*;*;*;*;*;*;*;*;*;void Clock::setTime(int newH, int newM,
int newS) {
hour = newH;
minute = newM;
second = newS;
}
void Clock::showTime() {
cout hour : minute : second;
};*;*;*;*;*;inline void Point:: init(int initX,int initY) {
x = initX;
y = initY;
}
inline int Point::getX() {
return x;
}
inline int Point::GetY() {
return y;
};*;*;*;*;*;构造函数的实现:
Clock::Clock(int newH, int newM, int newS) {
hour = newH;
minute = newM;
second = newS;
}
建立对象时构造函数的作用:
int main() {
Clock c(0,0,0); //隐含调用构造函数,将初始值作为实参。
c.showTime();
return 0;
};*;*;Point::Point (Point p) {
x = p.x;
y = p.y;
cout Calling the copy constructor endl;
};*;*;*;*;*;*;Point::Point(int xx,int yy) {
x = xx;
y = yy;
}
Point::~Point() {
}
//...其他函数的实??略;*;#include iostream
using namespace std;
const float PI = 3.141593; //给出p的值
const float FENCE_PRICE = 35; //栅栏的单价
const float CONCRETE_PRICE = 20;//过道水泥单价
class Circle { //声明定义类Circle 及其数据和方法
public: //外部接口
Circle(float r); //构造函数
float circumference(); //计算圆的周长
float area(); //计算圆的面积
private: //私有数据成员
float radius; //圆半径
};;//类的实现
//构造函数初始化数据成员radius
Circle::Circle(float r) {
radius = r;
}
//计算圆的周长
float Circle::circumference() {
return 2 * PI * radius;
}
//计算圆的面积
float Circle::area() {
return PI * radius * radius;
};int main () {
float radius;
cout Enter the radius of the pool: ; // 提示用户输入半径
cin radius;
Circle pool(radius); //游泳池边界
Circle poolRim(radius + 3); //栅栏
//计算栅栏造价并输出
float fenceCost = poolRim.circumference() * FENCE_PRICE;
cout Fencing Cost is $ fenceCost endl;; //计算过道造价并输出
float concreteCost = (poolRim.area() - pool.area()) * CONCRETE_PRICE;
cout Concrete Cost is $ concreteCost endl;
return 0;
}
运行结果
Enter the radius of the pool: 10
Fencing Cost is $2858.85
Concrete Cost is $4335.4;*;*;class Line {
private:
Point p1, p2; //线段的两个端点
public:
Line(Point a, Point b); //构造函数
void draw(void); //画出线段
};
//...函数的实现略;*;*;
显示全部