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

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: LINUX

2012-07-13 14:59:45

如果想在同类到多个对象之间实现数据共享,也不要用全局对象,可以使用静态到数据成员。它一关键字static开头。静态数据成员在内存中只占用一份空间,而不是每个对象都分别为它保留一份空间。静态数据成员可以初始化,但只能在类体外进行初始化。一般形式:数据类型名  类名::静态数据成员名=初值;不必在初始化语句中加入static。

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

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

  21. int Box::height=10;

  22. int main()
  23. {
  24. Box a(13,24),b(12,35);
  25. cout<<a.height<<endl;
  26. cout<<b.height<<endl;
  27. cout<<Box::height<<endl;
  28. cout<<a.volume()<<endl;
  29. cout<<b.volume()<<endl;
  30. }

  31. 输出结果:
  32. 10
  33. 10
  34. 10
  35. 3120
  36. 4200
函数可以定义静态的,在类中声明函数的前面加static.静态成员函数是类到一部分,不是对象到一部分。如果在类外调用公用到静态成员函数,需要用类名和域名“::”.
与静态成员,静态成员函数到作用只是为了能处理静态数据成员
与非静态成员函数区别:静态成员函数有this指针,而 静态成员函数没有this指针。由此决定静态成员函数不能访问本类中到非静态成员。

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Student{
  4.     public:
  5.         Student(int n,int a,float s):num(n),age(a),score(s){}
  6.         void total();
  7.         static float average();
  8.     private:
  9.         int num;
  10.         int age;
  11.         float score;
  12.         static float sum;
  13.         static int count;
  14. };
  15. void Student::total(){
  16. sum+=score;
  17. count++;
  18. }
  19. float Student::average()
  20. {
  21. return (sum/count);
  22. }

  23. float Student::sum=0;
  24. int Student::count=0;

  25. int main()
  26. {
  27. Student stud[3]={
  28. Student(1001,18,70),
  29. Student(1002,19,78),
  30. Student(1003,20,98)
  31. };
  32. int n;
  33. cout<<"please input the number of students: "<<endl;
  34. cin>>n;
  35. for(int i=0;i<n;i++)
  36. stud[i].total();
  37. cout<<"the average score of"<<n<<"student is"<<Student::average()<<endl;
  38. return 0;
  39. }

转换构造函数的作用是将一个其他类型的数据转黄成一个指定的类的对象。
默认构造函数:Complex(){real=0;imag=0;}
用于初始化的构造函数:Complex(double r,double i){real=r;imag=i};
转换构造函数:Complex(double r){real=r,imag=0;}
转换构造函数式将double型的参数r转换成Complex类的对象,r作为附属的是不,虚部位0.
归纳起来三点:1、先声明一个类(如Complex类)
2、在这个类中定义一个只有一个参数的构造函数,参数的类型是需要转换的类型。
3、在该类的作用域内用执行形式进行转换。 类名 (指定类型的数据)
Complex (double r)
也可以将另一类的对象转换成转换构造函数所在的对象:
Teacher (Student &s){num=s.num;strcpy(name,s.name);***=s.***;}
 
类型转换函数的作用是将一个类的对象转换成另一个类型的数据
在Complex类中定义类型转换函数:operator double(){return real;} 即函数返回double型变量real的值。作用是将一个Complex类对象转换为一个double型数据。
 

重载单目运算符:单目运算符只有一个操作数,因此运算符重载函数只有一个参数。如果重载函数作为成员函数,则可以省略此参数。如果自增或者自减在后置,增加int型参数。


点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Time{
  4.     public:
  5.         Time(){minute=0;sec=0;}
  6.         Time(int m,int s){minute=m;sec=s;}
  7.         Time operator ++();
  8.         Time operator ++(int);
  9.         void display(){cout<<minute<<":"<<sec<<endl;}
  10.     private:
  11.         int minute;
  12.         int sec;
  13. };

  14. Time Time::operator ++()
  15. {
  16.     if(sec++>=60)
  17.     sec-=60;
  18.     minute++;
  19.     return *this; //返回当前值
  20. }

  21. Time Time::operator ++(int)
  22. {
  23. Time temp(*this); //复制流
  24. sec++;
  25. if(sec>=60)
  26. {sec-=60;
  27. minute++;}
  28. return temp;
  29. }
  30.  int main()
  31. {
  32. Time t1(34,0);
  33. for(int i=0;i<61;i++)
  34. {
  35. ++t1;
  36. t1.display();
  37. cout<<"endl"<<endl;
  38. t1++;
  39. t1.display();
  40. }
  41. }

重载流插入运算和流提取运算符:用户自己定义的数据是不能直接用“<<”和“>>”来输入和输出,如果想用它们输入和输出自己声明的类型的数据必须对它们进行重载:

istream & operator >>(istream &,自定义 &);

ostream & operator <<(ostream &,自定义 &);

只能将重载“>>”和“<<”的函数作为友元函数或普通函数,而不能将它们定义为成员函数。


 

点击(此处)折叠或打开

  1. #include<iostream>
  2. using namespace std;
  3. class Complex{
  4.     public:
  5.         friend ostream & operator <<(ostream &,Complex &);
  6.         friend istream & operator >>(istream &,Complex &);
  7.     private:
  8.         double real;
  9.         double imag;
  10. };

  11. ostream & operator <<(ostream &output,Complex &c)
  12. {
  13. output<<"("<<c.real;
  14. if(c.imag>=0)
  15. output<<"+";
  16. output<<c.imag<<"i)"<<endl;
  17. return output;
  18. }

  19. istream & operator >>(istream &input,Complex &c)
  20. {
  21. cout<<"input part number: ";
  22. input>>c.real>>c.imag;
  23. return input;
  24. }

  25. int main()
  26. {
  27. Complex c1,c2;
  28. cin>>c1>>c2;
  29. cout<<"c1: "<<c1<<endl;
  30. cout<<"c2: "<<c2<<endl;
  31. return 0;
  32. }



 


阅读(546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~