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

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 閱讀(1014) 評論(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欧美v日本v亚洲v| 欧美国产成人在线| 久久九九99| 亚洲视频精选在线| 国产综合视频| 欧美本精品男人aⅴ天堂| 亚洲午夜在线视频| 欧美成人国产一区二区| 久久综合电影| 午夜久久久久久久久久一区二区| 亚洲精品你懂的| 永久免费视频成人| 国产亚洲电影| 久久国产精品色婷婷| 亚洲免费小视频| 亚洲激情一区| 亚洲精选91| 亚洲一区二区在线视频| 亚洲国产毛片完整版| 欧美激情一区二区三区不卡| 欧美成人一品| 亚洲激情黄色| 亚洲丝袜av一区| 久久高清国产| 久久综合给合久久狠狠狠97色69| 欧美激情一区三区| 欧美国产成人精品| 欧美中文在线观看| 久久久天天操| 欧美精品麻豆| 亚洲高清在线| 久久激五月天综合精品| 欧美色图五月天| 精品不卡视频| 韩国一区二区三区在线观看| 在线精品亚洲一区二区| 亚洲精品乱码久久久久久日本蜜臀| 欧美一区三区三区高中清蜜桃| 另类天堂av| 亚洲免费一级电影| 欧美在线观看视频一区二区| 久久精品亚洲精品| 亚洲国产精品一区二区第一页| 亚洲免费精品| 欧美日韩国产精品一区| 在线欧美一区| 久久亚洲图片| 久久精品二区| 亚洲素人一区二区| 国产精品porn| 日韩视频中文字幕| 欧美激情一区二区三区全黄| 久久久.com| 欧美午夜剧场| 亚洲美女福利视频网站| 久久九九免费| 欧美亚洲在线观看| 亚洲国产电影| 亚洲欧美日韩另类| 亚洲精品日韩在线观看| 亚洲欧美综合精品久久成人| 亚洲欧洲精品一区二区三区不卡 | 国产日韩在线不卡| 欧美大片一区二区| 欧美日韩国产区| 美女精品国产| 国产日韩欧美视频| 欧美福利视频在线| 国产一区二区日韩精品| 亚洲国产一区二区视频| 欧美片第一页| 男人插女人欧美| 亚洲每日更新| 国产伦精品一区二区三区四区免费| 久久狠狠亚洲综合| 国产伦精品一区二区| 亚洲人妖在线| 午夜一区二区三视频在线观看| 在线日本高清免费不卡| 久久夜色精品国产欧美乱| 久久精品视频一| 亚洲高清在线观看一区| 欧美国产精品劲爆| 国内精品福利| 国产精品jizz在线观看美国| 亚洲一区二区三区777| 欧美一区二区精品| 一区二区三区高清| 老司机午夜精品视频| 亚洲黄色小视频| 裸体歌舞表演一区二区| 亚洲深夜福利网站| 欧美bbbxxxxx| 一区二区视频免费在线观看| 99视频在线观看一区三区| 在线播放亚洲| 午夜久久久久久久久久一区二区| 中文亚洲免费| 欧美视频三区在线播放| 欧美激情亚洲综合一区| 亚洲激情不卡| 欧美一区二区私人影院日本| 欧美激情在线观看| 亚洲精品在线观看视频| 国产日韩三区| 亚洲第一黄网| 在线看一区二区| 你懂的视频一区二区| 亚洲一区bb| 麻豆久久精品| 亚洲精品麻豆| 欧美区日韩区| 亚洲专区一二三| 亚洲肉体裸体xxxx137| 久久精品国产精品 | 欧美高清视频免费观看| 亚洲成在线观看| 欧美在线不卡| 久久深夜福利| 亚洲高清视频的网址| 国产精品v欧美精品∨日韩| 亚洲一区二区三区成人在线视频精品| 国语自产偷拍精品视频偷| 久久亚洲不卡| 欧美在线观看视频在线| 欧美天天影院| 国产人成精品一区二区三| 久久久久国产精品一区| 亚洲视频观看| 米奇777在线欧美播放| 日韩一区二区精品在线观看| 欧美r片在线| 国产精品美女午夜av| 欧美一级片一区| 欧美国产日韩亚洲一区| 欧美日韩在线一区| 国产亚洲精品自拍| 夜夜嗨av一区二区三区网页| 欧美成人a∨高清免费观看| 在线中文字幕日韩| 欧美精品在线看| 免费91麻豆精品国产自产在线观看| 一区二区三区四区五区视频| 性久久久久久久久| 欧美一区二区视频观看视频| 99国产精品久久久久久久成人热 | 日韩视频免费在线| 欧美专区第一页| 午夜电影亚洲| 久久久久久久久久久久久久一区 | 久久成人综合视频| 亚洲男人av电影| 亚洲先锋成人| 久久爱另类一区二区小说| 久久黄金**| 欧美精品系列| 国产精品人人爽人人做我的可爱| 欧美一区二区三区啪啪 | 久久精品国产在热久久| 亚洲激情黄色| 狠狠色噜噜狠狠狠狠色吗综合| 国产精品美女久久| 欧美日韩一区二区三区视频| 久久人91精品久久久久久不卡| 久久久久久久久久久一区| 国产精品男gay被猛男狂揉视频| 老司机免费视频久久| 亚洲日本无吗高清不卡| 久久精品导航| 免费成人性网站| 一本久道久久综合中文字幕| 亚洲欧美三级在线| 欧美日韩成人精品| 亚洲精品美女| 欧美成人午夜剧场免费观看| 欧美国产日本在线| 性做久久久久久| 欧美国产综合| 久久精品一区蜜桃臀影院| 女人天堂亚洲aⅴ在线观看| 欧美视频二区| 亚洲国产毛片完整版 | 欧美va天堂| 国产毛片精品视频| 亚洲国产日韩综合一区| 久久中文字幕一区| 久久成人免费网| 精品不卡视频| 性欧美18~19sex高清播放| 亚洲视频观看| 欧美偷拍一区二区| 亚洲精品欧美一区二区三区| 欧美一区观看|