青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 29,comments - 10,trackbacks - 0
1、operator long ()
      成員轉換函數把類的對象轉換為其他數據類型的對象。
      在表達式的內部,如果某個類型和需要的類型不一致,就會發生轉換。
 1 #include <iostream>
 2 
 3 class Date
 4 {
 5     int mo, da, yr;
 6 
 7 public:
 8     Date(int m, int d, int y) { mo = m; da = d; yr = y; }
 9     operator long();    // member conversion function.
10 };
11 
12 Date::operator long()
13 {
14     static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
15     long days = yr - 1900;
16     days *= 365;
17     days += yr / 4;
18     for (int i = 0; i < mo-1; i++)
19         days += dys[i];
20     days += da;
21     
22     return days;
23 }
24 
25 int main()
26 {
27     Date xmas(12251997);
28     long since = xmas;
29     std::cout << since << std::endl;
30 
31     Date today(2121990);
32     const long ott = 123;
33     long sum = ott + today;   // today is converted to long.
34     std::cout << ott << " + " << (long) today << " = " << sum;
35     
36     return 0;
37 }
2、operator CustomDate()
      對類進行轉換
      調用轉換函數三種方法:一是隱式轉換,二是強制類型轉換,三是顯示調用轉換構造函數和成員轉換函數。分別為程序的64、68、72行
 1 #include <iostream>
 2 
 3 class CustomDate
 4 {
 5 public:
 6     int da, yr;
 7     CustomDate(int d = 0int y = 0) { da = d; yr = y;}
 8     void display(){ std::cout << std::endl
 9                               << yr << '-' << da; }
10 };
11 
12 class Date
13 {
14     int mo, da, yr;
15 
16 public:
17     // Constructor.
18     Date(int m = 0int d = 0int y = 0)
19         { mo = m; da = d; yr = y; }
20 
21     // Constructor conversion function.
22     Date(const CustomDate&);
23 
24     // Member conversion function.
25     operator CustomDate();
26 
27     void display(){std::cout << std::endl
28                              << mo << '/' << da
29                              << '/' << yr;}
30 };
31 
32 static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};
33 
34 // Constructor conversion function (Date <- CustomDate).
35 Date::Date(const CustomDate& jd)
36 {
37     yr = jd.yr;
38     da = jd.da;
39     for (mo = 0; mo < 11; mo++)
40         if (da > dys[mo])
41             da -= dys[mo];
42         else
43             break;
44         mo++;
45 }
46 
47 // Member conversion function (CustomDate <- Date)
48 Date::operator CustomDate()
49 {
50     CustomDate cd(0, yr);
51     for (int i = 0; i < mo-1; i++)
52         cd.da += dys[i];
53     cd.da += da;
54     
55     return cd;
56 }
57 
58 int main()
59 {
60     Date dt(11,17,97);
61     CustomDate cd;
62     
63     // Convert Date to CustomDate. Convert Date to CustomDate via implicit conversion.
64     cd = dt;
65     cd.display();
66     
67     // Convert Date to CustomDate via cast.
68     cd = (CustomDate) dt;
69     cd.display();
70     
71     // Convert Date to CustomDate via constructor
72     cd = CustomDate(dt);
73     cd.display();
74     
75     // Convert CustomDate to Date
76     dt = cd;
77     dt.display();
78     
79     return 0;
80 }
3、重載賦值運算符函數(Date& operator=(const Date&);)
#include <iostream>
#include 
<cstring>

class Date
{
    
int mo, da, yr;
    
char *month;

public:
    Date(
int m = 0int d = 0int y = 0);
    
~Date();

    
// Overloaded assignment operator.
    Date& operator=(const Date&);

    
void display() const;
};

// The constructor definition.
Date::Date(int m, int d, int y)
{
    
static char *mos[] =
    {
        
"January""February""March""April""May",
        
"June""July""August""September""October",
        
"November""December"
    };

    mo 
= m; da = d; yr = y;
    
if (m != 0)
    {
        month 
= new char[strlen(mos[m-1])+1];
        strcpy(month, mos[m
-1]);
    }
    
else
        month 
= 0;
}

// The destructor definition.
Date::~Date()
{
    delete [] month;
}

// Display member function.
void Date::display() const
{
    
if (month != 0)
        std::cout 
<< month << ' ' << da
                  
<< "" << yr << std::endl;
}

// Overloaded Date assignment.
Date& Date::operator=(const Date& dt)
{
    
if (this != &dt)
    {
        mo 
= dt.mo;
        da 
= dt.da;
        yr 
= dt.yr;
        delete [] month;
        
if (dt.month != 0)
        {
            month 
= new char [strlen(dt.month)+1];
            strcpy(month, dt.month);
        }
        
else
            month 
= 0;
    }

    
return *this;
}

int main()
{
    
// Original date.
    Date birthday(6,24,40);
    
    Date oldday, newday;
    
    
// Assign first to second to third.
    oldday = newday = birthday;
    
    birthday.display();
    oldday.display();
    newday.display();
    
    
return 0;
}
      if (this != &dt)是為了防止同時對一個指針進行兩次刪除操作
4、在類內部重載new和delete運算符
      void* operator new(size_t) throw(std::bad_alloc)
      void operator delete(void* p) throw()

      可以重載全局new和delete運算符,但一般不這么做。不過采用重載可以獲得較高的效率,避免使用全局new和delete運算符帶來的額外開銷。
#include <iostream>
#include 
<cstring>
#include 
<cstddef>
#include 
<new>

const int maxnames = 5;

class Names
{
    
char name[25];
    
static char Names::pool[];
    
static bool Names::inuse[maxnames];

public:
    Names(
char* s)
        { strncpy(name, s, 
sizeof(name)); }
    
void* operator new(size_t) throw(std::bad_alloc);
    
void operator delete(void*throw();
    
void display() const
        { std::cout 
<< name << std::endl; }
};

// Simple memory pool to handle fixed number of Names.
char Names::pool[maxnames * sizeof(Names)];
bool Names::inuse[maxnames];

// Overloaded new operator for the Names class.
void* Names::operator new(size_t) throw(std::bad_alloc)
{
    
for (int p = 0; p < maxnames; p++)
    {
        
if (!inuse[p])
        {
            inuse[p] 
= true;
            
return pool+p*sizeof(Names);
        }
    }
    
throw std::bad_alloc();
}

// Overloaded delete operator for the Names class.
void Names::operator delete(void* p) throw()
{
    
if (p != 0)
        inuse[((
char*)p - pool) / sizeof(Names)] = false;
}

int main()
{
    Names
* nm[maxnames];
    
int i;

    
for (i = 0; i < maxnames; i++)
    {
        std::cout 
<< std::endl << "Enter name # "
                  
<< i+1 << "";
        
char name[25];
        std::cin 
>> name;
        nm[i] 
= new Names(name);
    }

    
for (i = 0; i < maxnames; i++)
    {
        nm[i]
->display();
        delete nm[i];
    }

    
return 0;
}
5、重載new[]和delete[]運算符
#include <iostream>
#include 
<cstring>
#include 
<cstddef>
#include 
<new>

const int maxnames = 5;

class Names
{
    
char name[25];
    
static char Names::pool[];
    
static short int Names::inuse[maxnames];

public:
    Names(
char* s = 0)
    {
        
if (s)
            strncpy(name, s, 
sizeof(name));
    }
    
void* operator new[](size_t) throw(std::bad_alloc);
    
void operator delete[](void*throw();
    
void display() const
        { std::cout 
<< name << std::endl; }
};

// Simple memory pool to handle fixed number of Names.
char Names::pool[maxnames * sizeof(Names)];
short int Names::inuse[maxnames];

// Overloaded new[] operator for the Names class.
void* Names::operator new[](size_t size) throw(std::bad_alloc)
{
    
int elements = size / sizeof(Names);

    
// Find the first empty element (if any).
    int p = -1;
    
int i = 0;
    
while ((i < maxnames) && (p == -1))
    {
        
if (!inuse[i])
            p 
= i;
        
++i;
    }

    
// Not enough room.
    if ((p == -1|| ((maxnames - p) < elements))
        
throw std::bad_alloc();

    
// Mark the elements as used.
    for (int x=0; x<elements; ++x)
        inuse[p
+x] = elements;

    
// Return pointer to memory.
    return pool+p*sizeof(Names);
}

// Overloaded delete[] operator for the Names class.
void Names::operator delete[](void* b) throw()
{
    
if (b != 0)
    {
        
int p = ((char*)b - pool) / sizeof(Names);
        
int elements = inuse[p];
        
for (int i = 0; i < elements; i++)
            inuse[p 
+ i] = 0;
    }
}

int main()
{
    Names
* np = new Names[maxnames];
    
    
int i;
    
for (i = 0; i < maxnames; i++)
    {
        std::cout 
<< std::endl << "Enter name # "
            
<< i+1 << "";
        
char name[25];
        std::cin 
>> name;
        
*(np + i) = name;
    }
    
    
for (i = 0; i < maxnames; i++)
        (np 
+ i)->display();
    
    delete [] np;
    
    
return 0;
}

6、重載+運算符
這里分別用了成員函數和非成員函數對“+”符號進行了重載
Date Date::operator+(int) const
Date operator+(int n, Date& dt)

#include <iostream>

class Date
{
    
int mo, da, yr;
    
static int dys[];

public:
    Date(
int m=0int d=0int y=0)
        { mo 
= m; da = d; yr = y; }
    
void display() const
        { std::cout 
<< mo << '/' << da << '/' << yr; }

    
// Overloaded + operator.
    Date operator+(intconst;
};

int Date::dys[]={31,28,31,30,31,30,31,31,30,31,30,31};

// Overloaded + operator: Date + int.
Date Date::operator+(int n) const
{
    Date dt 
= *this;
    n 
+= dt.da;
    
while (n > dys[dt.mo-1])
    {
        n 
-= dys[dt.mo-1];
        
if (++dt.mo == 13)
        {
            dt.mo 
= 1;
            dt.yr
++;
        }
    }
    dt.da 
= n;

    
return dt;
}

Date 
operator+(int n, Date& dt)
{
    
return dt + n;
}

int main()
{
    Date olddate(
2,20,1997);
    Date newdate;
    newdate 
= 11 + olddate + 10;  // three weeks hence
    newdate.display();

    
return 0;
}

7、重載關系運算符(“==”“< ”“+=”)
Date operator+=(int n)
int operator==(Date& dt) const
int operator<(Date&) const

#include <iostream>

class Date
{
    
int mo, da, yr;

public:
    Date(
int m=0int d=0int y=0)
        { mo 
= m; da = d; yr = y; }
    
void display() const
        { std::cout 
<< mo << '/' << da << '/' << yr; }
    
// Overloaded + operator.
    Date operator+(intconst;
    
    
// Overloaded += operator.
    Date operator+=(int n)
        { 
*this = *this + n; return *this; }

    
// Overloaded operators.
    int operator==(Date& dt) const;
    
int operator<(Date&const;
};

int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};

// Overloaded + operator definition.
Date Date::operator+(int n) const
{
    Date dt 
= *this;
    n 
+= dt.da;
    
while (n > dys[dt.mo-1])
    {
        n 
-= dys[dt.mo-1];
        
if (++dt.mo == 13)
        {
            dt.mo 
= 1;
            dt.yr
++;
        }
    }
    dt.da 
= n;
    
    
return dt;
}

// Overloaded equality operator definition.
int Date::operator==(Date& dt) const
{
    
return (this->mo == dt.mo &&
            
this->da == dt.da &&
            
this->yr == dt.yr);
}

// Overloaded less-than operator definition.
int Date::operator<(Date& dt) const
{
    
if (this->yr == dt.yr)    {
        
if (this->mo == dt.mo)
            
return this->da < dt.da;
        
return this->mo < dt.mo;
    }
    
return this->yr < dt.yr;
}

int main()
{
    Date date1(
12,7,1941),
         date2(
2,22,1990),
         date3(
12,7,1941);

    Date olddate(
2,20,1997);
    olddate 
+= 21;            // three weeks hence
    olddate.display();
    std::cout
<<std::endl;

    
if (date1 < date2)
    {
        date1.display();
        std::cout 
<< " is less than ";
        date2.display();
    }

    std::cout 
<< '\n';
    
    
if (date1 == date3)
    {
        date1.display();
        std::cout 
<< " is equal to ";
        date3.display();
    }
    
    
return 0;
}
8、重載自增運算符(++)
Date operator++():重載++i
Date operator++(int):重載i++
#include <iostream>

class Date
{
    
int mo, da, yr;
    
static int dys[];

public:
    Date(
int m=0int d=0int y=0)
        { mo 
= m; da = d; yr = y; }
    
void display() const
        { std::cout 
<< '\n' << mo << '/' << da << '/' << yr;}

    
// Overloaded + operator.
    Date operator+(intconst;

    
// Overloaded prefix ++ operator.
    Date operator++()
        { 
*this = *this + 1return *this; }

    
// Overloaded postfix ++ operator.
    Date operator++(int)
        { Date dt
=*this*this=*this+1return dt; }
};

int Date::dys[]={31,28,31,30,31,30,31,31,30,31,30,31};

// Overloaded + operator definition.
Date Date::operator+(int n) const
{
    Date dt 
= *this;
    n 
+= dt.da;
    
while (n > dys[dt.mo-1])
    {
        n 
-= dys[dt.mo-1];
        
if (++dt.mo == 13)
        {
            dt.mo 
= 1;
            dt.yr
++;
        }
    }
    dt.da 
= n;

    
return dt;
}

int main()
{
    Date olddate(
2,20,1997);
    olddate
++;
    olddate.display();
    
++olddate;
    olddate.display();

    
return 0;
}
9、重載單目負運算符(-i)
int operator-() const
#include <iostream>
#include 
<cstring>

class ItemQty
{
    
int onhand;
    
char desc[25];

public:
    ItemQty(
int oh, char *d)
        { onhand 
= oh; strcpy(desc, d); }
    
void display() const
        { std::cout 
<< '\n' << desc <<"huhu"<<strlen(desc)<< "" << onhand; }

    
// Overloaded unary - operator.
    int operator-() const
        { 
return -onhand; }
};

int main()
{
    ItemQty item1(
100"crankshaft");
    ItemQty item2(
-50"driveshaft");

    item1.display();
    std::cout 
<< '\n' << -item1;    // invoke the overloaded -

    item2.display();
    std::cout 
<< '\n' << -item2;    // invoke the overloaded -

    
return 0;
}
10、重載下標運算符([])
 char& operator[](int n)
 const char& operator[](int n) const
      只有當所有的String類對象都是非常量的時候,才能用第一個重載[]運算符函數來得到字符串中的字符。為了支持常量String對象類中還設計了第二個重載[]運算符。
#include <iostream>
#include 
<cstring>

class String
{
    
char* sptr;

public:
    String(
char* s = 0);
    
~String() { delete sptr; }
    
void display()
        { std::cout 
<< sptr << std::endl; }

    
// Overloaded [] operator.
    char& operator[](int n)
        { 
return *(sptr + n); }
    
const char& operator[](int n) const
        { 
return *(sptr + n); }
};

// The String class constructor.
String::String(char* s)
{
    
if (s)
    {
        sptr 
= new char[strlen(s)+1];
        strcpy(sptr, s);
    }
    
else
        sptr 
= 0;
}

int main()
{
    String string1(
"The Ides of March");
    string1.display();
    
    
// Change some string characters.
    string1[4= '1';
    string1[
5= '5';
    string1[
6= 't';
    string1[
7= 'h';
    string1.display();
    
    
// Change a substring.
    strncpy(&string1[4], "21st"4);
    string1.display();
    
    
// const string, cannot be modified.
    const String string2("Et tu, Brute?");
    
for (int i = 0; i < 13; i++)
        std::cout 
<< string2[i];
    
    
return 0;
}
11、成員指針運算符(—>)
Date* operator->()
      為了防止由于疏忽,沒有把一個有效地Date對象的地址復制給指針,而使指針指向了無意義的地方,會導致程序崩潰。
      DatePtr對象是一個指針,它知道自身是否已經被賦值。即使沒有任何Date類對象的地址賦給指針,指針不會指向一個空的Date對象。
#include <iostream>

class Date
{
    
int mo, da, yr;

public:
    Date(
int m=0int d=0int y=0)
        { mo 
= m; da = d; yr = y; }
    
void display()
        { std::cout 
<< '\n' << mo << '/' << da << '/' << yr; }
};

class DatePtr
{
    Date
* dp;

public:
    DatePtr(Date
* d = 0) { dp = d; }
    Date
* operator->()
    {
        
static Date nulldate(0,0,0);
        
if (dp == 0)           // if the pointer is null
            return &nulldate;  // return the dummy address
        return dp;             // otherwise return the pointer
    }
};

int main()
{
    
// Date pointer with nothing in it.
    DatePtr dp;

    
// Use it to call display function.
    dp->display();

    Date dt(
3,17,90);

    
// Put address of date in pointer.
    dp = &dt;

    
// Display date through the pointer.
    dp->display();

    
return 0;
}
posted on 2009-06-22 20:31 The_Moment 閱讀(1013) 評論(0)  編輯 收藏 引用 所屬分類: C\C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            中文在线一区| 欧美成人午夜视频| 亚洲第一福利在线观看| 亚洲私拍自拍| 国产精品成人v| 亚洲国产精品久久久久| 欧美在线视频一区二区| 欧美激情va永久在线播放| 亚洲在线一区二区| 91久久精品国产| 国产精品揄拍500视频| 免费在线成人av| 亚洲专区在线视频| 日韩视频二区| 亚洲国产天堂久久国产91| 亚洲成人在线网| 久久久久国色av免费观看性色| 亚洲精品国产精品国自产观看浪潮| 国产视频一区二区在线观看| 欧美激情网站在线观看| 久久久精品免费视频| 亚洲天堂成人在线视频| 亚洲精品影视在线观看| 亚洲国产精彩中文乱码av在线播放| 亚洲欧美国产精品专区久久| 日韩视频一区二区三区| 最近中文字幕mv在线一区二区三区四区| 亚洲七七久久综合桃花剧情介绍| 亚洲视频一二区| 老鸭窝91久久精品色噜噜导演| 久久久久九九九| 亚洲精品一区在线观看香蕉| 久久国产夜色精品鲁鲁99| 亚洲女性喷水在线观看一区| 久久综合九色综合欧美就去吻| 欧美综合国产| 欧美日韩国产在线观看| 欧美日韩在线视频一区二区| 欧美日本在线一区| 欧美日韩精品| 在线不卡中文字幕| 亚洲激情女人| 亚洲另类黄色| 久热精品视频在线观看| 亚洲一区二区三区在线| 欧美亚洲一区二区在线| 久久久久久亚洲综合影院红桃| 久久免费视频观看| 欧美黄色一区二区| 国产精品sm| 亚洲精品一二| 欧美激情一区二区三区不卡| 午夜精品福利一区二区蜜股av| 欧美自拍偷拍| 国产精品制服诱惑| 亚洲主播在线播放| 韩日精品视频| 亚洲国产精品一区| 亚洲丝袜av一区| 欧美日韩国产一区二区| 亚洲免费成人av| 欧美成人有码| 久热精品在线| 亚洲国产成人精品久久久国产成人一区| 亚洲激情二区| 欧美电影免费观看| 久久亚洲精品中文字幕冲田杏梨| 国内不卡一区二区三区| 99天天综合性| 欧美一区二区视频在线| 欧美国产日韩在线| 久久亚洲春色中文字幕| 亚洲国产小视频| 亚洲国产欧美另类丝袜| 欧美激情综合在线| 亚洲一二三区精品| 欧美ed2k| 亚洲综合欧美| 国产精品欧美日韩| 亚洲精品国产精品国自产在线| 欧美顶级艳妇交换群宴| 免费视频一区二区三区在线观看| 91久久精品国产91久久性色| 亚洲精品视频免费观看| 国产精品美女久久久免费| 亚洲激情第一区| 亚洲免费观看高清在线观看| 欧美午夜性色大片在线观看| 欧美亚洲在线| 日韩亚洲成人av在线| 国产精品久久999| 久久久精品性| 美日韩免费视频| 伊人久久大香线蕉av超碰演员| 欧美 日韩 国产精品免费观看| 欧美激情久久久久| 午夜欧美电影在线观看| 亚洲一级片在线看| 韩国久久久久| 亚洲精品久久在线| 欧美第十八页| 亚洲国产精品第一区二区| 亚洲精品国产品国语在线app| 国产伦精品一区二区| 亚洲二区视频| 国产手机视频精品| 亚洲全黄一级网站| 精东粉嫩av免费一区二区三区| 久久www免费人成看片高清| 亚洲一区欧美二区| 亚洲黄网站在线观看| 亚洲伊人伊色伊影伊综合网| 亚洲黄色免费网站| 欧美一级视频免费在线观看| 一区二区三区视频观看| 亚洲精品国久久99热| 国产日韩精品在线观看| 国产精品www色诱视频| 黄色精品在线看| 一区二区三区蜜桃网| 欧美午夜片在线观看| 欧美成人精品在线视频| 国产麻豆9l精品三级站| 亚洲美女在线一区| 亚洲精选一区| 欧美1级日本1级| 免费成人av在线看| 欧美精品一区二区久久婷婷| 久久久蜜桃一区二区人| 久久久久久久波多野高潮日日| 亚洲欧美日韩综合aⅴ视频| 欧美国产一区二区| 欧美激情一区二区三区成人| 1024精品一区二区三区| 久久av在线| 久久亚洲精品欧美| 欧美精品乱人伦久久久久久 | 日韩午夜在线观看视频| 日韩写真在线| 日韩亚洲不卡在线| 欧美日韩国产免费| 亚洲美女av电影| 99视频精品全部免费在线| 欧美国产精品| 亚洲激情校园春色| 国产欧美视频一区二区| 欧美成人a视频| 亚洲电影免费观看高清完整版 | 国产精品xxxxx| 亚洲视频播放| 欧美专区亚洲专区| 国产主播一区二区三区| 欧美在线一二三| 欧美成人一区二区三区在线观看 | 久久久久女教师免费一区| 看欧美日韩国产| 日韩亚洲精品在线| 国产精品久久国产三级国电话系列| 亚洲深夜影院| 久久影院亚洲| 夜夜爽av福利精品导航 | 亚洲狠狠丁香婷婷综合久久久| 亚洲精品欧洲| 国产精品草草| 久久精品视频在线看| 亚洲高清视频一区二区| 亚洲午夜精品久久久久久浪潮 | 香蕉成人久久| 欧美福利一区二区| 欧美成人一区二区三区在线观看| 欧美激情在线观看| 国产精品入口福利| 亚洲第一区色| 欧美精品色综合| 午夜视频一区二区| 在线精品视频一区二区三四| 久久综合色播五月| 99re在线精品| 伊人精品视频| 欧美成人第一页| 亚洲午夜伦理| 欧美国产在线视频| 欧美一区二区三区四区在线观看地址| 伊人色综合久久天天| 欧美视频在线观看一区二区| 久久精品色图| 亚洲伦理精品| 久久男人av资源网站| 国产精品99久久久久久人| 黄色欧美日韩| 国产农村妇女毛片精品久久麻豆| 欧美顶级少妇做爰| 久久精品视频在线看| 亚洲一区二区三区欧美| 欧美成人免费在线观看| 久久www成人_看片免费不卡| 一区二区三区日韩在线观看| 亚洲电影毛片| 国产在线欧美| 国产啪精品视频|