类的成员
按照PPT中的示例,作业代码编写及运行。
作业范围:课件PPT第二章,P28、P39、P50、P55、P59。
注意:列出的起始页码,如果完成程序跨多个PPT,需要书写完整代码并运行。
要求:
1.必须要有详细注释
2.格式要调整对齐(注意缩进)
3.代码和运行结果截图贴在答题框
4.要补充自己对核心代码及运行结果的理解说明文字
P28:
#include <iostream>
using namespace std;
const float PI = 3.14159; // 设置常量PI
const float FencePrice = 35; // 设置栅栏价格常量
const float ConcretePrice = 20; //设置过道价格常量
//声明类Circle 及其数据和方法
class Circle
{
private:
float radius; //圆半径
public:
Circle(float r); //构造函数
float Circumference( ) const; //圆周长
float Area( ) const; //圆面积
};
// 类的实现
// 构造函数初始化数据成员radius
Circle::Circle(float r)
{ radius=r; }
// 计算圆的周长
float Circle::Circumference( ) const
{
return 2 * PI * radius;
}
// 计算圆的面积
float Circle::Area( ) const
{
return PI * radius * radius;
}
int main () {
float radius;
float FenceCost, ConcreteCost;
// 提示用户输入半径
cout<<"请输入游泳池的半径: ";
cin>>radius;
Circle Pool(radius); // 声明 Circle 对象
Circle PoolRim(radius + 3);
//计算栅栏造价并输出
FenceCost=PoolRim.Circumference()*FencePrice;
cout<<"栅栏价格为 ¥"<<FenceCost<<endl;
//计算过道造价并输出
ConcreteCost=( PoolRim.Area()-Pool.Area() ) * ConcretePrice;
cout<<"过道价格为 ¥"<<ConcreteCost<<endl;
}
P39:
#include <iostream>
using namespace std;
//声明类A及其数据和方法
class A
{
private:
int a,b,c;
public:
A(int i=8, int j=10, int k=12); //给成员函数的参数设置初始值
int aout() { return a; } //返回a
int bout() { return b; } //返回b
int cout() { return c; } //返回c
};
//类成员函数A的实现
A::A(int i,int j,int k)
{
a=i; b=j; c=k;
}
int main( )
{
A X,Y(5),Z(7,9,11);
cout<<"X="<<X.aout()<<','<<X.bout()<<','<<X.cout()<<endl; //X没有设置参数,因此直接打印初始值
cout<<"Y="<<Y.aout()<<','<<Y.bout()<<','<<Y.cout()<<endl; //Y给a设置了参数,因此a为5,其他仍是初始值
cout<<"Z="<<Z.aout()<<','<<Z.bout()<<','<<Z.cout()<<endl; //同理可得,其值不是初始值
}
P50:
#include <iostream>
using namespace std;
class Point //Point类声明
{
private: //私有数据成员
int X,Y;
static int countP; //设置静态成员 countP
public: //外部接口
Point(int xx=0,int yy=0){X=xx;Y=yy;countP++;}
Point(Point &p);//拷贝构造函数
int GetX() {return X;}
int GetY() {return Y;}
static void GetC( ) { cout<<" Object id="<<countP<<endl; } //声明静态成员函数GetC
};
//类的实现
Point::Point(Point &p) {
X=p.X;
Y=p.Y;
countP++;
}
int Point::countP=0; //给静态成员初始化
int main( ) { //主函数实现
Point A(4,5); //声明对象A
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
A.GetC(); //输出对象号,对象名引用
Point B(A); //声明对象B,并且使用对象A的数据
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
Point::GetC(); //输出对象号,类名引用
}
P55:
#include <iostream>
#include <math.h>
using namespace std;
//声明类Point
class Point {
private:
double x,y;
public:
Point(double i,double j)
{ x=i; y=j; }
void Print()
{ cout<<'('<<x<<','<<y<<')'<<endl; }
friend double Distance( Point a, Point b ); //友元声明
};
double Distance ( Point a, Point b ) { // 类体外定义,无需类名
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt( dx*dx + dy*dy );
}
int main( ){
double d1=3, d2=4, d3=6, d4=8;
Point p1( d1, d2 ), p2( d3, d4 );
p1.Print( );
p2.Print( );
double d = Distance( p1, p2 );
// 友元调用,同普通函数
cout<<"Distance="<<d<<endl;
}
P59:
#include <iostream>
using namespace std;
class X //声明类X
{
friend class Y; //声明友元类Y
private:
int x;
static int s;
public:
X(int i) { x=i; }
void Print() { //打印x,s的值
cout<<"x="<<x<<','
<<"s="<<s<<endl;}
};
int X::s=5;
class Y //声明类Y
{
private:
int y;
public:
Y(int i){ y=i; }
void Print(X &r) {
cout<<"x="<<r.x<<','
<<"y="<<y<<endl; }
};
int main()
{
X m( 2 );
m.Print( ) ;
Y n( 8 );
n.Print( m );
}
Ps:
记录自己的作业日常,PPT校方版权所有,我无权上传,还请见谅,但大家也可以看看代码~
注释若有错误还请大家指出~~
谢谢~