[C++]实验五:掌握类的声明和对象的定义
2006-01-22 21:19
627 查看
/*
1.构造计时类
要求:建立Watch类,用来仿效秒表跟踪消逝的时间。
提供两个成员函数Start()与Stop(),分别打开与关闭计时器。
在该类中还包含一个Show()的成员函数,显示消逝的时间(以秒为单位)。
*/
#include <time.h>
#include <iostream>
using namespace std;
class watch {
public:
void show();
void start();
void stop();
private:
double time;
};
inline void watch::show()
{
cout << time << endl;
}
inline void watch::start()
{
clock();
}
inline void watch::stop()
{
clock_t t;
t = clock();
time = (double)t / CLOCKS_PER_SEC; /* 转换成秒 */
}
void main()
{
watch p1;
int i=0,j=0;
p1.start();
for (; i<100000000 ; i++)
for (; j<10000000 ; j++)
(i>j)?i:j;
p1.stop();
p1.show();
}
// 清翔兔
// 2005/11/01
/*
2.设计一个立方体类Box,它能计算并输出立方体的体积与表面积。
*/
#include <iostream>
using namespace std;
class Box {
public:
void init (double,double,double);
inline double area (){return 2*(l*w+h*l+w*h);};
double vol(){return w*l*h;};
private:
double l,w,h;
};
inline void Box::init( double a =0, double b = 0, double c = 0 )
{
l = a; w = b; h = c;
}
void main ()
{
Box l1;
l1.init (1,2,3);
cout << l1.area() << "/n"
<< l1.vol() << endl;
}
// 清翔兔
// 2005/11/01
/*
3.设计一个采用类结构的方式求n!的程序。
*/
#include <iostream>
using namespace std;
class factorial {
public:
factorial(unsigned int num): _n(num){};
unsigned long int facrun (unsigned int);
unsigned long int fac() {return facrun (_n);};
private:
unsigned int _n;
};
unsigned long int factorial::facrun (unsigned int n)
{
unsigned long int result;
if(n==1)
result=1;
else
result=facrun(n-1)*n;
return result;
}
void main()
{
factorial f1(4);
cout << f1.fac() << endl;
}
// 清翔兔
// 2005/11/01
1.构造计时类
要求:建立Watch类,用来仿效秒表跟踪消逝的时间。
提供两个成员函数Start()与Stop(),分别打开与关闭计时器。
在该类中还包含一个Show()的成员函数,显示消逝的时间(以秒为单位)。
*/
#include <time.h>
#include <iostream>
using namespace std;
class watch {
public:
void show();
void start();
void stop();
private:
double time;
};
inline void watch::show()
{
cout << time << endl;
}
inline void watch::start()
{
clock();
}
inline void watch::stop()
{
clock_t t;
t = clock();
time = (double)t / CLOCKS_PER_SEC; /* 转换成秒 */
}
void main()
{
watch p1;
int i=0,j=0;
p1.start();
for (; i<100000000 ; i++)
for (; j<10000000 ; j++)
(i>j)?i:j;
p1.stop();
p1.show();
}
// 清翔兔
// 2005/11/01
/*
2.设计一个立方体类Box,它能计算并输出立方体的体积与表面积。
*/
#include <iostream>
using namespace std;
class Box {
public:
void init (double,double,double);
inline double area (){return 2*(l*w+h*l+w*h);};
double vol(){return w*l*h;};
private:
double l,w,h;
};
inline void Box::init( double a =0, double b = 0, double c = 0 )
{
l = a; w = b; h = c;
}
void main ()
{
Box l1;
l1.init (1,2,3);
cout << l1.area() << "/n"
<< l1.vol() << endl;
}
// 清翔兔
// 2005/11/01
/*
3.设计一个采用类结构的方式求n!的程序。
*/
#include <iostream>
using namespace std;
class factorial {
public:
factorial(unsigned int num): _n(num){};
unsigned long int facrun (unsigned int);
unsigned long int fac() {return facrun (_n);};
private:
unsigned int _n;
};
unsigned long int factorial::facrun (unsigned int n)
{
unsigned long int result;
if(n==1)
result=1;
else
result=facrun(n-1)*n;
return result;
}
void main()
{
factorial f1(4);
cout << f1.fac() << endl;
}
// 清翔兔
// 2005/11/01
相关文章推荐
- C++语言基础 例程 类的声明和对象的定义
- C/C++中使用static关键字定义或声明静态对象
- C++的声明和定义(针对变量,const变量,C++函数,C函数,类,类的静态成员等)
- 用c++编写一个不能被继承的类(但是可以在类外部定义该类的对象)
- C++中声明与定义的区别
- C++ 变量和函数的声明和定义
- c++中的定义和声明
- [初]C++ 的 声明&定义&初始化
- [原创]Enterprise Architecture V7.5 C++代码生成时,头文件中函数声明没有注释,CPP中函数定义却有注释。
- C++变量的声明和定义 终于搞明白了
- C++中的声明和定义的区别1
- Objective-C 基础,类和对象,方法和消息,已声明的属性和存取方法,块对象,协议和范畴类,预定义类型和编码策略
- C++全局变量的声明和定义
- c++模板声明和定义编译过程的分析
- 定义、声明、初始化、赋值 小记(C++)
- C++定义一个对象和new一个对象的区别与联系
- C/C++定义与声明
- c++的类中,声明一个对象好还是用指针申请一块空间好?
- C++ 关于声明,定义,类的定义,头文件作用,防止头文件在同一个编译单元重复引用,不具名空间
- C++ 全局变量的声明和定义