C++ 简单代码,语法参照

遍历进程:

#include <windows.h>                                                                                                      
#include <TCHAR.h>                                                                                                        
#include <tlhelp32.h>                                                                                                     
#include <locale.h>                                                                                                       
#include <stdio.h>                                                                                                        
                                                                                                                          
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int nCmdShow)                              
{                                                                                                                         
    AllocConsole();//分配控制台                                                                                           
    freopen("CONOUT{1}quot.txt", "w", stdout);                                                                                    
                                                                                                                          
    PROCESSENTRY32 pe32;                                                                                                  
    pe32.dwSize=sizeof(pe32);                                                                                             
    HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);                                                   
    BOOL bMore=Process32First(hProcessSnap,&pe32);                                                                        
    setlocale(LC_ALL, "chs");                                                                                             
    while(bMore)                                                                                                          
    {                                                                                                                     
        _tprintf(TEXT("进程名称:%s\n"),pe32.szExeFile);                                                                   
        _tprintf(TEXT("进程ID:%u \n\n"),pe32.th32ProcessID);                                                              
        bMore=Process32Next(hProcessSnap,&pe32);                                                                          
    }                                                                                                                     
                                                                                                                          
    CloseHandle(hProcessSnap);                                                                                            
                                                                                                                          
                                                                                                                          
    Sleep(3000);  //睡眠3秒钟                                                                                             
    FreeConsole();  //关闭释放控制台                                                                                      
    return 0;                                                                                                             
}    
View Code

类的构造析构顺序:

#include <iostream>

using namespace std;

class A {
public:
    A(){cout<<"A is created!"<<endl;}
    ~A(){cout<<"A is destroyed!"<<endl;system("pause");}
    void func1() {
        cout << "A::func1" << endl;
    }
    void func2() {
        func1();
        cout << "A::func2" << endl;
    }
    void func3() {
        cout << "A::func3" << endl;
    }
};
class B: public A {
public:
    B(){cout<<"B is created!"<<endl;}
    ~B(){cout<<"B is destroyed!"<<endl;system("pause");}
    void func1() {
        cout << "B::func1" << endl;
    }
    void func3() {
        //A::func3();
        cout << "B::func3" << endl;
        //func2();
    }
};
int main() {
    B b;

    b.func1();
    b.func2();
    b.func3();
    system("pause");
}
View Code

单目运算符重载:

 1 #include<iostream>
 2 using namespace std;
 3 class myTime
 4 {
 5     int minute;
 6     int second;
 7 public:
 8     myTime(){minute=second=0;};
 9     myTime(int m,int s)
10     {minute=m;second=s;}
11     void operator++();
12     void display()
13     {cout<<"minute "<<minute<<" second "<<second<<endl;};      
14 };
15 void myTime::operator++()
16 {
17     second++;
18     while(second>=60)
19     {
20         minute+=second/60;
21         second=second%60;            
22     }    
23     return;                
24 }
25 int main()
26 {
27     myTime o;
28     int n=65;
29     while(n--)
30     {
31        o.display();
32        ++o;
33     }
34     system("pause");
35 }
View Code

双目运算符重载:

#include<iostream>
using namespace std; 
class myString 
{
  private:
          char *p;
  public:
  myString(){p=NULL;}
  myString(char* str) {p=str;};
  
  void display();    
  friend bool operator > (myString s1,myString s2);

};
bool operator > (myString s1,myString s2)
{
       if(strcmp(s1.p,s2.p)>0)
           return true;
       else
           return false;       
  };
void myString::display()
{
    cout<<p<<endl;     
}
int main()
{
    myString a;
    myString b("xiaoqiang"); 
    myString c("xiaohong");
    cout<<(b>c)<<endl;
    //a.display();   
    b.display();   
    c.display();   
    system("pause"); 
}
View Code

模板类试用:

// xy.h
template<int Base, int Exponent>
class XY
{
    public:
    enum { result_ = Base * XY<Base, Exponent-1>::result_ };
};
template<int Base>
class XY<Base, 0> 
{
    public:
    enum { result_ = 1 };
};

//main.cpp
#include <iostream>
#include "xy.h"
using namespace std;

int main(int argc, char *argv[])
{
 
    cout << "X^Y<5, 4>::result_ = " << XY<5,4>::result_;
    system("PAUSE");
     return EXIT_SUCCESS;
}
View Code

 

posted @ 2014-01-26 20:41  黄QQ  阅读(623)  评论(0编辑  收藏  举报