📄 类.txt
字号:
//**************************
//** ch11_1.cpp **
//**************************
#include <iostream.h>
class Tdate
{
public:
void Set(int m,int d,int y) //set the date
{
month=m; day=d; year=y;
}
int IsLeapYear()
{
return (year%4==0&&year%100!=0)||(year%400==0);
}
void Print()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
private:
int month;
int day;
int year;
};
void main()
{
Tdate a;
a.Set(2,14,2005);
a.Print();
if(a.IsLeapYear())
cout<<"this is a leap year."<<endl;
else
cout<<"It's not a leap year."<<endl;
}
//**************************
//** ch11_2.cpp **
//**************************
//用指针调用成员函数
#include <iostream.h>
#include "Tdate.h"
void someFunc(Tdate* pS)
{
pS->Print();
if(pS->IsLeapYear())
cout<<"oh, this is a leap year!"<<endl;
else
cout<<"oh..., it's not a leap year..."<<endl;
}
void main()
{
Tdate a;
a.Set(2,14,2005);
someFunc(&a);
}
//**************************
//** ch11_3.cpp **
//**************************
//用引用传递来访问成员函数
#include <iostream.h>
#include "Tdate.h"
void someFunc(Tdate &rS)
{
rS.Print();
if(rS.IsLeapYear())
cout<<"oh, this is a leap year!"<<endl;
else
cout<<"oh..., it's not a leap year..."<<endl;
}
void main()
{
Tdate a;
a.Set(2,14,2000);
someFunc(a);
}
//**************************
//** ch11_5.cpp **
//**************************
#include <iostream.h>
#include <math.h>
class Point
{
public:
void Set(double ix,double iy)
{
x=ix;
y=iy;
}
double xOffset()
{
return x;
}
double yOffset()
{
return y;
}
double angle()
{
return (180/3.14159)*atan2(y,x);
}
double radius()
{
return sqrt(x*x+y*y);
}
protected:
double x;
double y;
};
void main()
{
Point p;
double x,y;
while(1)
{
cout<<"Enter x and y:"<<endl;
cin>>x>>y;
if(x<0)
break;
p.Set(x,y);
cout<<"angle = "<<p.angle()
<<",radius = "<<p.radius()
<<",x offset = "<<p.xOffset()
<<",y offset = "<<p.yOffset()<<endl;
}
}
//**************************
//** point2.cpp **
//**************************
#include <math.h>
class Point
{
public:
void Set(double ix,double iy) //interface
{
a=atan2(iy,ix);
r=sqrt(ix*ix+iy*iy);
}
double xOffset() //interface
{
return r * cos(a);
}
double yOffset() //interface
{
return r * sin(a);
}
double angle() //interface
{
return (180/3.14159)*a;
}
double radius() //interface
{
return r;
}
protected:
double a;
double r;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -