|  | 
				
					
		
			  
	2011年10月30日
	
 
			
			     摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->  1  2  3  4#include <stdio.h>  5#include...  阅读全文 
			
			     摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->  1  2  3  4#include <stdio.h>  5#define&...  阅读全文 
			  
	2011年10月16日
	
 
			
			 1 2
  #include<stdio.h> 3
  #include<setjmp.h> 4
  5
  void get(int **p) 6
    { 7
  int *pointer; 8
  pointer = (int *)malloc(sizeof(int) * 10); 9
  strcpy(pointer,"hello\n"); 10
  *p = pointer; 11
  } 12
  13
  14
  void main() 15
    { 16
  int *pt = NULL; 17
  get(&pt); 18
  printf("%s", pt); 19
  } 20
  21
  
			  
	2011年10月13日
	
 
			
			1       main() 2       {int i=3;
 3       int j;
 4       j = sizeof(++i+ ++i);
 5       printf("i=%d j=%d\n", i ,j);}
 
			  
	2011年7月17日
	
 
			
			
 1 2
  #include<iostream> 3
  #include <cmath> 4
  using namespace std; 5
  6
  class B1    //基类B1,构造函数有参数 7
    {public: 8
   B1(int i)  {cout<<"constructing B1 "<<i<<endl;} 9
  }; 10
  class B2    //基类B2,构造函数有参数 11
    {public: 12
   B2(int j)  {cout<<"constructing B2 "<<j<<endl;} 13
  }; 14
  class B3    //基类B3,构造函数无参数 15
    {public: 16
  //B3(){cout<<"constructing B3 *"<<endl;} 17
   void B5()  {cout<<"constructing B3 *"<<endl;} 18
  }; 19
  20
  class C: public B2, public B1, public B3 21
    { 22
  public:    //派生类的公有成员 23
  C(int a, int b, int c, int d): 24
   B2(b),B1(a),memberB1(c),memberB2(d)  {} 25
   /**//* 26
  说明:此处B3 类中没有参数,所以在此处不用对B3进行构造函数赋值操作, 27
  这里出现的“:”后的定义必须在类的继承后面“:”中已经定义了 28
  即B2和B1均继承了,不过B3虽然继承了,但是构造函数中没有值,所以就不用在这里多加说明 29
  另外的memberB1(c),memberB2(d),但这种是对本类中的成员进行初始化操作。memberB1(c) 30
  不过这里感觉会有点怪: 31
  不是类似的 x = a;类的形式赋值。 32
  */ 33
  private:    //派生类的私有对象成员 34
  B1 memberB1; 35
  B2 memberB2; 36
  //B3 memberB3; 37
  B3 t; 38
  }; 39
  void main() 40
    {    C obj(1,2,3,4);  } 41
   /**//* 42
  记住以下规定: 43
  当基类中声明有默认形式的构造函数或未声明构造函数时,派生类构造函数可以不向基类构造函数传递参数。 44
  若基类中未声明构造函数,派生类中也可以不声明,全采用缺省形式构造函数。 45
  当基类声明有带形参的构造函数时,派生类也应声明带形参的构造函数,并将参数传递给基类构造函数。 46
  47
  48
  1. 调用基类构造函数,调用顺序按照它们被继承时声明的顺序(从左向右)。 49
  2. 调用成员对象的构造函数,调用顺序按照它们在类中声明的顺序。 50
  3. 派生类的构造函数体中的内容。 51
  52
  派生类名::派生类名(基类1形参,基类2形参,  基类n形参,本类形参):基类名1(参数), 基类名2(参数),  基类名n(参数),对象数据成员的初始化 53
  { 54
  本类成员初始化赋值语句; 55
  }; 56
  57
  */ 
			
			 1  /**//* 2
  友元: 3
  类的主要特点是数据隐藏,即类的私有部分在该类的作用域之外是不可见的,有时候需要在类的外部访问类的私有部分, 4
  一般来说,全局函数不能访问类的私有成员, 5
  而friend关键字超出了这一界限,它允许类里 所有 成员的访问,在 6
  类的内部,友元被作为该类的成员看待, 并且对对象公用部分的访问没有任何限制; 7
  8
  可以这样认为,写了一个友元函数,就是做了这个东西(访问了私有变量) 9
  其他的没什么用了吧。 10
  */ 11
  #include<iostream> 12
  #include <cmath> 13
  using namespace std; 14
  15
  class Point    //Point类声明 16
    { public:    //外部接口 17
   Point(int xx=0, int yy=0)  {X=xx;Y=yy;} 18
   int GetX()  {return X;} 19
   int GetY()  {return Y;} 20
  friend float Distance(Point &a, Point &b); 21
  private:    //私有数据成员 22
  int X,Y; 23
  }; 24
  float Distance( Point& a, Point& b) 25
    { 26
  double dx=a.X-b.X; 27
  double dy=a.Y-b.Y; 28
  return sqrt(dx*dx+dy*dy); 29
  } 30
  int main() 31
    { 32
  Point p1(3.0, 5.0), p2(4.0, 6.0); 33
  double d=Distance(p1, p2); 34
  cout<<"The distance is "<<d<<endl; 35
  return 0; 36
  } 37
  
			  
	2011年7月16日
	
 
			
			 #include <iostream> 
  #include <string> 
  using namespace std; 
  
  class stud 
    { 
  protected: 
  int num; 
  char name[10]; 
  char sex; 
  public: 
  stud(int n,char nam[],char s) 
     { 
  num = n; 
  strcpy(name,nam); 
  sex = s; 
  } 
  //~stud(){} 
  }; 
  
  class student : public stud 
    { 
  private: 
  int age; 
  char addr[30]; 
  public: 
  student(int n,char nam[],char s,int a,char ad[]):stud(n,nam,s) 
     { 
  age = a; 
  strcpy(addr,ad); 
  } 
  void show() 
     { 
  cout << "num:" << num << endl; 
  cout << "name:" << name << endl; 
  cout << "sex:" << sex << endl; 
  cout << "age:" << age << endl; 
  cout << "address:" << addr << endl; 
  } 
   ~student()  {} 
  }; 
  void main() 
    { 
  student a(10010,"wangli",'f',19,"shanghai"); 
  student b(10011,"zhangfun",'m',21,"hangzhou"); 
  a.show(); 
  cout << endl; 
  b.show(); 
  } 
			
			 1 #include <iostream> 2
  #include <string> 3
  using namespace std; 4
  5
  class A 6
    { 7
  public: 8
  A(int i) 9
     { 10
  cout << "A constructore" << endl; 11
  } 12
  ~A() 13
     { 14
  cout << "A destructore" << endl; 15
  } 16
  }; 17
  class B 18
    { 19
  public: 20
  B(int i) 21
     { 22
  cout << "B construtore" << endl; 23
  } 24
  ~B() 25
     { 26
  cout << "B destructore" << endl; 27
  } 28
  }; 29
  class sample 30
    { 31
  public: 32
  sample():b(20),a(10) 33
     { 34
  cout << "Sample Constructore" << endl; 35
  } 36
  ~sample() 37
     { 38
  cout << "sample destructore" << endl; 39
  } 40
  private: 41
  A a; 42
  B b; 43
  }; 44
  void main() 45
    { 46
  sample s; 47
  } 48
   /**//*当一个类中包含子对象时,构造函数的调用顺序是先调用子对象的构造函数(按他们在类中定义时出现的顺序,而不是构造列表中的顺序) 49
  然后再调用当前对象的构造函数的代码,调用析构函数的顺序与构造函数的顺序刚好相反 50
  */ 
			
			 1 #include <iostream> 2
  #include <string> 3
  using namespace std; 4
  5
  6
  class complex 7
    { 8
  double a; 9
  double b; 10
  public: 11
  complex() 12
     { 13
  a = 0; 14
  b = 0; 15
  } 16
  complex operator + (complex another) 17
     { 18
  complex add; 19
  add.a = a + another.a; 20
  add.b = b + another.b; 21
  return add; 22
  } 23
  void Putln() 24
     { 25
  cin >> a >> b; 26
  return; 27
  } 28
  void show() 29
     { 30
  //输出复数 31
  cout <<"("<<a<<"+"<<b<<"i)"; 32
  return; 33
  } 34
  }; 35
  class summator 36
    { 37
  public: 38
  int addition(int a,int b); 39
  float addition(float a,float b); 40
  string addition(string a,string b); 41
  complex addition(complex a,complex b); 42
  }; 43
  complex summator :: addition(complex a,complex b) 44
    { 45
  return a + b; 46
  } 47
  48
  void main() 49
    { 50
  summator sr; 51
  //int inta ,intb; 52
  //float fa ,fb; 53
  //string stra,strb; 54
  complex ca,cb; 55
   56
   57
  cout << "请输入两个复数:"<<endl; 58
  ca.Putln(); 59
  cb.Putln(); 60
  61
  ca.show(); 62
  cout<< "+"; 63
  cb.show(); 64
  cout<< "="; 65
  66
  sr.addition(ca,cb).show(); 67
  cout<<endl; 68
  69
  return; 70
  } 71
  
			
			 1 2 /*
 3 通过调用基类的指针作为形参的函数来显示桥对象的信息
 4 */
 5 #include <iostream>
 6 #include <string>
 7 using namespace std;
 8
 9 class cbuilding
 10 {
 11
 12     string name;
 13     //char * name;
 14 public:
 15     void set(string strName);
 16     /*
 17     我们在main函数定义了一个派生类的对象,这里用到了基类指针的调用方法,
 18     如果我们不加virtual,桥的长度就不能打印出来,
 19     从结果上看,长度信息没有输出,所以调用的是基类的display,
 20     可见通过指向派生类对象的基类指针来调用派生类对象中覆盖基类的方法是不行的,调用的仍然是基类的方法,
 21
 22   我们希望对不同的对象发出相同的消息而产生不同的响应,浙江就是所说的多态,
 23   这里用show(相同的消息)分别施加于building和bridge不同的对象
 24     */
 25     //void display()
 26     virtual void display()
 27     {
 28         cout<<"建筑是"<< name<<endl;
 29     }
 30 };
 31
 32 void cbuilding::set(string str)
 33 {
 34     name = str;
 35 }
 36
 37 class bridge:public cbuilding
 38 {
 39     float bridgeLength;
 40 public:
 41     void setlength(float length)
 42     {
 43     bridgeLength = length;
 44     }
 45     void display()
 46     {
 47         cbuilding::display();
 48         cout<<"桥的长度是"<<bridgeLength<<endl;
 49     }
 50 };
 51
 52 /* 通过指向基类的指针进行显示 */
 53 void show(cbuilding *b)
 54 {
 55     b->display();
 56     return;
 57 }
 58 void main()
 59 {
 60     bridge b;
 61     b.set("杭州钱塘江三桥");
 62     b.setlength(200.003);
 63     show (&b);
 64     return;
 65 }
 |