1. Îö¹¹º¯ÊýµÄ½éÉÜ
¶ÔÏ󳬳ö×÷ÓÃÓò£¬¶ÔÏó³·Ïú£¬³·Ïú֮ǰ»á×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý£¬Îö¹¹º¯ÊýÖ´ÐÐÍ꣬¶ÔÏó³·Ïú£¬¶ÔÏóµÄÊý¾Ý³ÉÔ±¿Õ¼äÊÍ·Å
¶ÔÏó³·ÏúµÄʱºò£¬»á×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý£¬Èç¹û³ÌÐòԱûÓÐÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬Ôòϵͳ»áÌṩһ¸öĬÈϵģ¬¿Õº¯ÊýÌåµÄÎö¹¹º¯Êý£¬Èç¹û³ÌÐòÔ±ÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÔòϵͳĬÈϵÄÎö¹¹º¯Êý¾Í»á±»ÆÁ±Î£¬Îö¹¹º¯Êý¿ÉÒÔÓÃÓÚ¶ÔÏóµÄÊý¾Ý³ÉÔ±µÄ¿Õ¼äµÄÇåÀí¹¤×÷¡£
1.1Îö¹¹º¯ÊýµÄ¸ñʽ
ʹÓÃÀàÃû×÷Ϊº¯ÊýÃû£¬²¢ÔÚÇ°Ãæ¼Ó~
Îö¹¹º¯Êý²ÎÊýÁбíΪ¿Õ
Îö¹¹º¯Êý²»Ö§³Öº¯ÊýÖØÔØ£¬Ã¿Ò»¸öÀàÖÐÖ»ÓÐÒ»¸öÎö¹¹º¯Êý
Îö¹¹º¯ÊýÔÚÀàÍⶨÒåµÄ¸ñʽ£º
ˈ̞::~ˈ̞()
{
//º¯ÊýÌå
}
1. Îö¹¹º¯ÊýÒ²¿ÉÒÔ±»ÏÔʽµ÷Ó㬲»½¨ÒéÏÔʽµ÷ÓÃ
2. Îö¹¹º¯ÊýÒ»°ãµ±ÀàÄÚÓжÑÇø¿Õ¼äµÄʱºò£¬ÐèÒªÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÓÃÓÚÊͷŶÑÇø¿Õ¼ä
3. Îö¹¹º¯Êý²»ÊÇÊͷſռ䣬ÓÃÓÚ¶Ô³ÉÔ±¿Õ¼ä½øÐÐÇåÀí¹¤×÷
4. µ±ÀàÄÚÈç¹ûûÓжÑÇø¿Õ¼ä£¨Ã»ÓÐÖ¸Õë³ÉÔ±£©£¬Ö±½Ó²ÉÓÃϵͳĬÈϵÄÎö¹¹º¯Êý¼´¿É
5. Îö¹¹º¯Êý²»Ö§³ÖÖØÔØ£¬º¯ÊýµÄ²ÎÊýÁбíΪ¿Õ
#include <iostream> #include <cstring> using namespace std; class Computer { char brand[15]; double price; public: Computer(); Computer(const char *br, int price); void setBrand(const char *brand); void showComputer(); ~Computer(); //Îö¹¹º¯Êý }; int main() { //Computer comp1; //µ÷ÓÃÎ޲ι¹Ô캯Êý //Computer comp2("HP", 60000);
{ Computer comp3("HP", 60000); //comp3³¬³ö×÷ÓÃÓò£¬Ôò³·Ïú£¬µ÷ÓÃÎö¹¹º¯Êý comp3.~Computer(); //¿ÉÒÔÖ÷¶¯µ÷ÓÃÎö¹¹º¯Êý //¶ÔÏó³·ÏúµÄʱºò»áÔÙ´Î×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý cout << "111111111" << endl; comp3.showComputer(); } cout << "33333333333" << endl; //comp2.showComputer();
}
Computer::Computer() { price = 0; strcpy_s(brand, sizeof(brand), "\0"); cout << "Î޲ι¹Ô캯Êý" << endl; } Computer::Computer(const char *br, int price) { this->price = price; strcpy_s(brand, sizeof(brand), br); cout << "Î޲ι¹Ô캯Êý" << endl; } void Computer::setBrand(const char *brand) { strcpy_s(this->brand, sizeof(this->brand), brand); } void Computer::showComputer() { cout << "Æ·ÅÆ£º" << brand <<endl; cout << "¼Û¸ñ£º" << price <<endl; }
//ϵͳÌṩµÄĬÈϵÄÎö¹¹º¯ÊýÊÇ¿Õº¯ÊýÌå Computer::~Computer() { //³ÉÔ±¿Õ¼äÇåÁ㣬¶ÔÏó³·Ïú£¬³ÉÔ±¿Õ¼ä×Ô¶¯ÊÍ·Å(ûÓжÑÇøµÄ¿Õ¼ä) price = 0; strcpy_s(brand, sizeof(brand), "\0"); cout << "Îö¹¹º¯Êý" << endl; } |
µ±ÀàÄÚÓжÑÇø¿Õ¼ä£¬ÔòÐèÒªÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÓÃÓÚÔÚÎö¹¹º¯ÊýÖÐÊͷŶÑÇø¿Õ¼ä
#include <iostream> #include <cstring> using namespace std; class Computer { char *brand; double price; public: Computer(); Computer(const char *br, int price); void setBrand(const char *brand); void showComputer(); ~Computer(); //Îö¹¹º¯Êý }; int main() { Computer comp1; //µ÷ÓÃÎ޲ι¹Ô캯Êý Computer comp2("HP", 60000);
comp1.setBrand("DELL"); comp2.setBrand("Leveon"); } Computer::Computer() { price = 0; brand = NULL; cout << "Î޲ι¹Ô캯Êý" << endl; } Computer::Computer(const char *br, int price) { this->price = price;
brand = new char[strlen(br) + 1]; //ÉêÇëÒ»¿é¶ÑÇø¿Õ¼ä strcpy_s(brand, strlen(br) + 1, br); cout << "´ø²Î¹¹Ô캯Êý" << endl; } void Computer::setBrand(const char *brand) { if (this->brand != NULL) { delete[]this->brand; //ÊÍ·ÅÖ¸Õë֮ǰÉêÇëµÄ¶ÑÇø¿Õ¼ä this->brand = NULL; } this->brand = new char[strlen(brand) + 1]; strcpy_s(this->brand, strlen(brand) + 1, brand); } void Computer::showComputer() { cout << "Æ·ÅÆ£º" << brand <<endl; cout << "¼Û¸ñ£º" << price <<endl; }
Computer::~Computer() {
if (brand == NULL) { return; } delete[]brand; brand = NULL; cout << "Îö¹¹º¯Êý" << endl; } |
2.¸´Öƹ¹Ô캯Êý
2.1¸´Öƹ¹Ô캯Êý¼°Æä¸ñʽ
int a = 10;
int b = a; //ͬÀàÐ͵ıäÁ¿¶ÔÁíÍâÒ»¸ö±äÁ¿½øÐгõʼ»¯
Point a(12,34);
Point b = a; //ͬÀàÐ͵ĶÔÏó¶ÔÁíÍâÒ»¸ö¶ÔÏó½øÐгõʼ»¯
µ±Ò»¸öͬÀàÐ͵ĶÔÏó¶ÔÁíÍâÒ»¸ö¶ÔÏó½øÐгõʼ»¯µÄʱºò£¬ÊÇ¿ÉÒÔʵÏÖ£¬Êµ¼ÊÉϵ÷ÓõÄÊǸ´Öƹ¹Ô캯Êý£¬Èç¹û³ÌÐòԱûÓÐÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý£¬Ôòϵͳ»áÌṩһ¸öĬÈϵĸ´Öƹ¹Ô캯Êý£¬ÏµÍ³ÌṩµÄĬÈϵĸ´Öƹ¹Ô캯ÊýÌåÄÚ²¿Êǽ«Ò»¸öͬÀàÐ͵ĶÔÏóµÄ³ÉÔ±ÖµÒ»Ò»¸³Öµ¸øÁíÍâÒ»¸öͬÀàÐͶÔÏó³ÉÔ±,Èç¹ûÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý£¬ÔòϵͳÌṩµÄĬÈϵĸ´Öƹ¹Ô캯ÊýÔò±»ÆÁ±Î
¸´Öƹ¹Ô캯ÊýµÄ¸ñʽ£º
ÀàÃû(const ÀàÃû&ÐβÎÃû);
È磺Point(const Point &tmp);
//ϵͳÌṩµÄĬÈϵĸ´Öƹ¹Ô캯ÊýÄÚ²¿ //Êǽ«Í¬ÀàÐͶÔÏóµÄ³ÉÔ±ÖµÒ»Ò»¶ÔÓ¦¸³Öµ¸øÁíÍâÒ»¸ö //¶ÔÏóµÄ³ÉÔ± Computer::Computer(const Computer &tmp) { price = tmp.price; strcpy_s(brand, sizeof(brand), tmp.brand);
cout << "¸´Öƹ¹Ô캯Êý" << endl; } |
˵Ã÷£º
Ò»°ãÇé¿öÏ£¬²»ÐèÒªÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý£¬Ö±½Ó²ÉÓÃϵͳÌṩµÄĬÈϵĸ´Öƹ¹Ô캯Êý¼´¿É£¬µ±ÀàÄÚÓжÑÇø¿Õ¼äµÄʱºò£¬Ôò±ØÐëÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý£¬ÒòΪÈç¹ûÓжÑÇø¿Õ¼ä£¬¶ø²ÉÓÃϵͳÌṩµÄĬÈϵĸ´Öƹ¹Ô캯Êý»áÒý·¢¶ÑÇø¿Õ¼äµÄ¶þ´ÎÊÍ·Å£¬Ôì³É³ÌÐòÔËÐбÀÀ££¬ËùÒÔÐèÒªÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý£¬±ÜÃâ¶þ´ÎÊÍ·Å
//ϵͳÌṩµÄĬÈϵĸ´Öƹ¹Ô캯Êý //Ò»¸ö¶ÔÏóµÄ³ÉÔ±ÖµÒ»Ò»¸³Öµ¸øÁíÍâÒ»¸ö¶ÔÏó Computer::Computer(const Computer &tmp) { price = tmp.price; brand = tmp.brand; //brandÊÇÖ¸Õ룬µ¼ÖÂÁ½¸ö¶ÔÏóµÄÖ¸ÕëÖ¸ÏòµÄÊÇͬһ¿é¶ÑÇø¿Õ¼ä //¶ÔÏó³·ÏúµÄʱºòÒý·¢¶þ´ÎÊÍ·Å //¸ÄÕý£ºÈÃÁ½¸öÖ¸ÕëÖ¸Ïò²»Í¬µÄ¶ÑÇø¿Õ¼ä cout << "¸´Öƹ¹Ô캯Êý" << endl; } |
//¸ÄÕý£ºÈÃÁ½¸öÖ¸ÕëÖ¸Ïò²»Í¬µÄ¶ÑÇø¿Õ¼ä
//µ±ÀàÄÚÓжÑÇø¿Õ¼ä£¬±ØÐëÏÔʽ¶¨Ò帴Öƹ¹Ô캯Êý //Ä¿µÄ£ºÈÃÖ¸ÕëÖ¸Ïò²»Í¬µÄ¶ÑÇø¿Õ¼ä Computer::Computer(const Computer &tmp) { brand = new char[strlen(tmp.brand) + 1]; strcpy_s(brand, strlen(tmp.brand) + 1, tmp.brand); price = tmp.price;
cout << "¸´Öƹ¹Ô캯Êý" << endl; } |