Chinaunix首页 | 论坛 | 博客
  • 博客访问: 756371
  • 博文数量: 231
  • 博客积分: 3217
  • 博客等级: 中校
  • 技术积分: 2053
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-04 12:01
文章分类

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: LINUX

2012-07-11 12:40:10

struct声明的类都默认为public,要想分别指明私有成员和共有成员,需要使用private和public。
class声明的类默认都是pubulic,如果想分别指明私有和共有成员,需要使用private和public。
 
如果成员函数在类体内都默认为内置函数。如果成员函数不在类体内定义,而在类体外,系统不把它默认为内置函数(inline),调用这些成员函数跟调用普通函数的过程是一样的。类声明和成员函数定义的分离:
在调用内置成员函数时,系统并不是真正的执行函数的调用过程,而是把函数代码嵌入程序的调用点,这样可以大大减少调用成员函数的时间开销。

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Time{
  4. public:
  5.     void set_time(void);
  6.     void show_time(void);
  7. private:
  8.     int hour;
  9.     int minute;
  10.     int sec;
  11. } ;                  //类声明,公用函数原型声明





  12. int main()
  13. {
  14.     Time t;
  15.     t.set_time();         //使用类函数
  16.     t.show_time();
  17. }

  18. void Time::set_time(void)  //定义类外类函数
  19. {
  20.     cin>>hour;
  21.     cin>>minute;
  22.     cin>>sec;
  23. }

  24. void Time::show_time(void)
  25. {
  26. cout<<hour<<": "<<minute<<": "<<sec<<endl;
  27. }

需要计算一个长方体的体积:

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Volume{
  4. public:
  5.     int tiji(void);
  6.     void show_tiji(void);
  7. private:
  8.     int length;
  9.     int wide;
  10.     int high;
  11.     int v;
  12. } ;





  13. int main()
  14. {
  15.     Time t;
  16.     t.tiji();
  17.     t.show_tiji();
  18. }

  19. int Volume::tiji(void)
  20. {
  21.     cin>>length;
  22.     cin>>wide;
  23.     cin>>high;
  24.     v=length*wide*high;
  25.     return v;
  26. }

  27. void Volume::show_tiji(void)
  28. {
  29. cout<<"tiji: "<<v<<endl;
  30. }

不能在类定义中对数据成员进行初始化工作,因为类并不是一个实体,而是一种抽象类型,并不占用存储空间,无处容纳数据。可以在定义对象时对数据成员初始化:
Time t1={1,2,3}
对private和protected定义的变量都不能定义。使用构造函数来解决问题:构造函数的名字必须与类名同名,不能由用户任意命名,以便编译系统能够识别它并把它作为构造函数来处理。它不具有任何类型,不反悔任何值。构造函数的功能由用户定义,用户根据初始化的要求设计函数体和函数参数。

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Time
  4. {
  5. public:
  6.     Time(){
  7.         hour=0;
  8.         minute=0;
  9.         sec=0;        
  10.             }
  11.     void set_time();
  12.     void show_time();
  13. private:
  14.     int hour;
  15.     int minute;
  16.     int sec;
  17. };

  18. void Time::set_time()
  19. {
  20. cin>>hour;
  21. cin>>minute;
  22. cin>>sec;
  23. }

  24. void Time::show_time()
  25. {
  26. cout<<hour<<":"<<minute<<":"<<sec<<":"<<endl;
  27. }
构造函数Time(){}也可以先声明后在类外定义:Time::Time(){}。
 
带参数函数的构造函数:构造函数首部的一般格式:构造函数 (类型1 形参1 类型2 形参2 。。。)
定义对象的一般格式:类名 对象名(实参1 实参2.。。。)

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. public:
  6.     Box(int l,int w,int h){
  7.         length=l;
  8.         width=w;
  9.         high=h;        
  10.             }
  11.     int volume();
  12. private:
  13.     int length;
  14.     int width;
  15.     int high;
  16. };

  17. int Box::volume()
  18. {
  19. return length*width*high;
  20. }



  21. int main()
  22. {
  23. Box b1(12,25,30);
  24. cout<<b1.volume()<<endl;
  25. Box b2(15,30,21);
  26. cout<<b2.volume()<<endl;
  27. return 0;

 

也可以这么做:

#include
using namespace std;
class Box
{
public:
 Box(int l,int w,int h);
 int volume();
private:
 int length;
 int width;
 int high;
};
Box::Box(int l,int w,int h):length(l),width(w),high(h){};
int Box::volume()
{
return length*width*high;
}

 

int main()
{
Box b1(12,25,30);
cout<Box b2(15,30,21);
cout<return 0;
}

 
在类中使用Box(int =10,int =10,int =10);  //声明一个全部参数都指定默认值的构造函数
          Box();//声明无参数的构造函数,是重载构造函数
          Box();//声明有两个参数的构造函数,是重载函数

 

析构函数是一个特殊的成员函数,它的作用与构造函数相反,在类的名字前加一个“~”符号。
析构函数的作用并不是删除对象,而是撤销对象占用的内存之前完成一些清理工作,这部分内存可以被程序分配给新对象使用。析构函数不反悔任何值,没有函数类型也没有函数参数。由于没有函数参数所以不能被重载,一个类可以有多个构造函数,但只能有一个析构函数。

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Student
  5.     {public:
  6.     Student(int ,string ,char );    
  7.     ~Student()
  8.         {cout<<"destruction "<<endl;}

  9.     void display()
  10. {
  11.     cout<<"num: "<<num<<endl;
  12.     cout<<"name: "<<name<<endl;
  13.     cout<<"***: "<<***<<endl;}
  14.     private:

  15.         int num;
  16.         string name;
  17.         char ***;
  18.         };    

  19. Student::Student(int n,string nam,char s){
  20.         num=n;
  21.         name=nam;
  22.         ***=s;
  23.         cout<<"construct called "<<endl;}
  24. int main()
  25. {
  26. Student stud1(10010,"wang_li",'f');
  27. stud1.display();
  28. Student stud2(10011,"zhang_jun",'m');
  29. stud2.display();
  30. return 0;
  31. }

  32. 输出结果:
  33. root@fengye:~/hello2# g++ f1.cc -o f1
  34. root@fengye:~/hello2# ./f1
  35. construct called
  36. num: 10010
  37. name: wang_li
  38. ***: f
  39. construct called
  40. num: 10011
  41. name: zhang_jun
  42. ***: m
  43. destruction
  44. destruction
调用析构函数的顺序是:先构造的后析构,后构造的先析构。

 
阅读(1256) | 评论(0) | 转发(0) |
0

上一篇:虚拟化之QEMU与KVM

下一篇:c++学习(三)

给主人留下些什么吧!~~