2.2 ÌØÊâ³ÉÔ±º¯ÊýÖ®ÆÕͨ¹¹Ô캯Êý
2.7.1 ÆÕͨ¹¹Ô캯Êý
´´½¨Ò»¸ö¶ÔÏó£¬ÏµÍ³»á×Ô¶¯µ÷Óù¹Ô캯Êý£¬Èç¹û¹¹Ô캯ÊýûÓÐÏÔʽ¶¨Òå(³ÌÐòԱûÓж¨Òå¹¹Ô캯Êý)£¬´Ëʱϵͳ»áÌṩһ¸öĬÈϵÄÎ޲ι¹Ô캯Êý£¬¸ÃÎ޲ι¹Ô캯ÊýÄÚ²¿Êǿյĺ¯ÊýÌ壨ʲô¶¼Ã»ÓÐ×ö£©,µ±¶ÔÏó³·ÏúµÄʱºò£¬´Ëʱ»áÔÚ³·Ïú֮ǰ×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý£¬Èç¹ûÎö¹¹º¯ÊýûÓÐÏÔʽ¶¨Òå(³ÌÐòԱûÓж¨ÒåÎö¹¹º¯Êý)£¬´ËʱϵͳҲ»áÌṩһ¸öĬÈϵÄÎö¹¹º¯Êý£¬¸ÃÎö¹¹º¯ÊýµÄº¯ÊýÌåÊǿյģ¬Îö¹¹º¯ÊýµÄ×÷ÓÃÓÃÓÚÇåÀíÊý¾Ý³ÉÔ±µÄ¿Õ¼ä£¨ÇåÀíµÄ¹¤×÷ÐèÒª³ÌÐòÔ±×Ô¼ºÊµÏÖ£©¡£
2.7.2 ¹¹Ô캯ÊýµÄ·ÖÀà
1£©ÆÕͨ¹¹Ô캯Êý
2£©¸´Öƹ¹Ô캯Êý
2.7.3 ͨ¹¹Ô캯ÊýµÄ¸ñʽ
¹¹Ô캯ÊýµÄ¶¨Òå¸ñʽ£º
ÀàÃû×÷Ϊº¯ÊýÃû£¬¹¹Ô캯ÊýÎÞ·µ»ØÖµÀàÐÍ£¬¿ÉÒÔÓвÎÊý
ÀàÃû(²ÎÊýÁбí);
¹¹Ô캯Êý¿ÉÒÔÔÚÀàµÄÄÚ²¿¶¨Ò壬Ҳ¿ÉÒÔÔÚÀàÍⶨÒå
1. ϵͳÌṩµÄĬÈϵĹ¹Ô캯Êý
µ±³ÌÐòԱûÓÐÏÔʾ¶¨Òå¹¹Ô캯Êý£¬Ôòϵͳ»áÌṩһ¸öĬÈϵÄÎ޲ι¹Ô캯Êý£¬¿Õº¯ÊýÌå
class Point { int xp; int yp; public: Point(); }; int main() { Point a; //´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý } Point::Point() { cout << "ϵͳÌṩµÄĬÈÏÎ޲ι¹Ô캯Êý" << endl; } |
´ø²Î¹¹Ô캯ÊýºÍÎ޲ι¹Ô캯Êý
#include <iostream>
using namespace std;
class Point { int xp; int yp; public: Point(); //Î޲ι¹Ô캯Êý Point(int x , int y ); //´ø²Î¹¹Ô캯Êý void setPoint(int xp = 0, int yp = 0); void showPoint(); }; int main() { Point a;//´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý Point b(11, 22); a.showPoint(); b.showPoint(); }
Point::Point(int x, int y) { xp = x; yp = y; cout << "´ø²Î¹¹Ô캯Êý" << endl; } Point::Point() { //ÔÚ¹¹Ô캯ÊýÌåÄÚ¶Ô¶ÔÏóµÄÊý¾Ý³ÉÔ±½øÐи³Öµ£¬±ÜÃâËæ»úÖµµÄ³öÏÖ xp = 0; yp = 0; cout << "Î޲ι¹Ô캯Êý" << endl; }
void Point::setPoint(int xp, int yp) { this->xp = xp; this->yp = yp; } void Point::showPoint() { cout << "(" << xp << "," << yp << ")" << endl; } |
´ø²ÎÊýȱʡ¹¹Ô캯Êý
#include <iostream>
using namespace std;
class Point { int xp; int yp; public: Point(int x = 0, int y = 0); //´ø²Î¹¹Ô캯Êý void setPoint(int xp = 0, int yp = 0){this->xp = xp;this->yp = yp;} void showPoint(){ cout << "(" << xp << "," << yp << ")" << endl; } }; int main() { Point a;//´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý Point b(11, 22); a.showPoint(); b.showPoint(); } Point::Point(int x, int y) { //¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷ xp = x; yp = y; cout << "´ø²ÎÊýȱʡ¹¹Ô캯Êý" << endl; } |
2.7.4¹¹Ô캯ÊýµÄÌØÐÔ
1. ¸ø¶ÔÏóÒ»¸ö±êʶ·û£¬¹¹Ô캯ÊýÒ»µ©µ÷Óã¬Ôò˵Ã÷¶ÔÏó±»´´½¨
2. ´´½¨¶ÔÏóµÄʱºò£¬×Ô¶¯µ÷Óù¹Ô캯Êý£¬¶ÔÏóµÄÊý¾Ý³ÉÔ±¿Õ¼ä·ÖÅä
3. ¿ÉÒÔ¶Ô¶ÔÏóµÄÊý¾Ý³ÉÔ±½øÐи³Öµ»òÕß³õʼ»¯£¨ÐèÒª³ÌÐòÔ±ÏÔʽ¶¨Òå¹¹Ô캯Êý£¬ÔÚ¹¹Ô캯ÊýµÄÌåÄÚ¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷£©
class Point { int xp; int yp; public: Point(int x = 0, int y = 0); //´ø²Î¹¹Ô캯Êý void setPoint(int xp = 0, int yp = 0){this->xp = xp;this->yp = yp;} void showPoint(){ cout << "(" << xp << "," << yp << ")" << endl; } }; int main() { Point a;//´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý Point b(11, 22); a.showPoint(); b.showPoint(); }
Point::Point(int x, int y) { //¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷ xp = x; yp = y; cout << "´ø²ÎÊýȱʡ¹¹Ô캯Êý" << endl; } |
2.7.5¹¹Ô캯ÊýµÄ×¢ÒâÊÂÏî
˵Ã÷£º
1. Ò»°ã´´½¨¶ÔÏóµÄʱºò£¬ÐèÒª¶Ô¶ÔÏóµÄÊý¾Ý³ÉÔ±½øÐгõʼ»¯»òÕ߸³Öµ²Ù×÷£¬±ÜÃâËæ»úÖµµÄ³öÏÖ£¬ÐèÒª³ÌÐòÔ±ÏÔʾ¶¨Òå¹¹Ô캯Êý
2£®Ò»µ©³ÌÐòÔ±ÏÔʾ¶¨ÒåÒ»¸ö¹¹Ô캯Êý£¬ÔòϵͳÌṩĬÈϵĹ¹Ô캯Êý¾Í²»ÔÚÌṩ£¬Èç¹û³ÌÐòÔ±²»ÏÔʽ¶¨Òå¹¹Ô캯Êý£¬Ôòϵͳ»áÌṩһ¸öĬÈϵÄÎ޲εĿպ¯ÊýÌåµÄ¹¹Ô캯Êý
3. ¹¹Ô캯ÊýÖ»ÓÐ×Ô¶¯±»µ÷Ó㬲»Äܱ»³ÌÐòÔ±Ö÷¶¯µ÷Óã¬Ö»Óд´½¨¶ÔÏóµÄʱºò£¬²Å»áµ÷ÓÃÒ»´Î£¬´´½¨Ò»¸ö¶ÔÏóµÄʱºò£¬¹¹Ô캯ÊýÖ»¿ÉÄÜ×Ô¶¯µ÷ÓÃÒ»´Î
4. ¹¹Ô캯ÊýÖ§³Öº¯ÊýÖØÔØ£¬Ö§³ÖÒýÓÃ×÷Ϊº¯ÊýµÄÐβΣ¬Ö§³Ö²ÎÊýĬÈÏÖµ
5. Ò»°ã³ÌÐòÔ±ÐèÒªÏÔʽ¶¨Òå¹¹Ô캯Êý£¬ÐèÒªÌṩÁ½¸ö¹¹Ô캯Êý£¬Ò»¸öÎ޲ι¹Ô캯Êý(´´½¨¶ÔÏ󣬶ԶÔÏ󲻸³Öµ)£¬´ø²Î¹¹Ô캯Êý(´´½¨¶ÔÏ󣬶ԶÔÏó½øÐгõʼ»¯) £¬»òÕßÖ»ÐèÒªÌṩ´ø²ÎÊýȱʡµÄ¹¹Ô캯Êý
2.7.6³õʼ»¯±í´ïʽ
ÔÚ¹¹Ô캯ÊýÌåÄÚ¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷£¬Èç¹ûÐèÒª¶Ô¶ÔÏóµÄÊý¾Ý³ÉÔ±½øÐгõʼ»¯²Ù×÷ÔòÐèÒª²ÉÓóõʼ»¯±í´ïʽµÄÐÎʽ¶ÔÊý¾Ý³ÉÔ±½øÐгõʼ»¯
1. ³õʼ»¯±í´ïʽµÄ¸ñʽ£º
ÀàÃû(²ÎÊýÁбí):³ÉÔ±Ãû1(³õʼ»¯Êý¾Ý),³ÉÔ±Ãû2(³õʼ»¯Êý¾Ý2)
{
//º¯ÊýÌå
}
2. ³õʼ»¯±í´ïʽֻÄÜÔÚ¹¹Ô캯ÊýÖгöÏÖ
3. ³õʼ»¯±í´ïʽÊǸúÕâ¹¹Ô캯ÊýµÄ¶¨Ò壬º¯ÊýµÄÉùÃ÷²»ÓÃд³õʼ»¯±í´ïʽ
4. ³ýÁËijЩÌØÊâµÄÊý¾Ý³ÉÔ±(ÒýÓóÉÔ±£¬const³ÉÔ±Êý¾Ý)±ØÐë²ÉÓóõʼ»¯±í´ïʽµÄÐÎʽ£¬ÆäËûµÄÊý¾Ý³ÉÔ±¼È¿ÉÒÔ²ÉÓóõʼ»¯±í´ïʽµÄÐÎʽ¶ÔÆä½øÐгõʼ»¯£¬Ò²¿ÉÒÔÔÚ¹¹Ô캯ÊýÌåÄÚ¶ÔÆä½øÐи³Öµ
5. ³õʼ»¯±í´ïʽµÄÖ´ÐÐ˳Ðò£¬²»ÊÇÓɳõʼ»¯±í´ïʽµÄÊéд˳Ðò¾ö¶¨£¬¶øÊÇÓɳÉÔ±Êý¾ÝµÄÉùÃ÷˳Ðò¾ö¶¨£¬ËÏÈÉùÃ÷£¬Ïȳõʼ»¯Ë
#include <iostream> #include <string>
using namespace std;
class Computer { private: string brand; intprice; public: Computer(); Computer(string br ,int pr ); void setBrand(string br); void showComputer(); };
int main() { Computer cmp1; Computer cmp2("HP", 6000);
cmp1.showComputer(); cmp2.showComputer(); }
//string brand = ""; //int price = 0 ; Computer::Computer() :brand(""), price(0) { //brand = ""; //¿Õ×Ö·û´® //price = 0; cout << "Î޲ι¹Ô캯Êý" << endl; } Computer::Computer(string br, int pr) :brand(br), price(pr) { cout << "´ø²Î¹¹Ô캯Êý" << endl; } void Computer::setBrand(string br) { brand = br; } void Computer::showComputer() { cout << "Æ·ÅÆ £º" << brand << endl; cout << "¼Û¸ñ £º" << price << endl; }
|
³õʼ»¯±í´ïʽµÄÖ´ÐÐ˳ÐòÓɳÉÔ±µÄÉùÃ÷˳Ðò¾ö¶¨£¬¶ø²»Êdzõʼ»¯±í´ïʽµÄÊéд˳Ðò
#include <iostream>
using namespace std;
class Point { int xp; int yp; public: Point(int x = 0, int y = 0); //´ø²Î¹¹Ô캯Êý void showPoint(){ cout << "(" << xp << "," << yp << ")" << endl; } }; int main() { Point a;//´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý Point b(11, 22); a.showPoint(); b.showPoint(); }
Point::Point(int x, int y) : yp(y),xp(yp) { //¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷ //xp = x; //yp = y; cout << "´ø²ÎÊýȱʡ¹¹Ô캯Êý" << endl; } |
2.7.7Îö¹¹º¯Êý
1. ¶ÔÏó³·Ïú£¬¶ÔÏóµÄÊý¾Ý³ÉÔ±¿Õ¼ä±»ÊÍ·Å£¬(¶ÔÏ󳬳ö×÷ÓÃÓò£¬Ôò»á±»³·Ïú)£¬£¬ÔÚÊý¾Ý³ÉÔ±¿Õ¼äÊÍ·Å֮ǰ×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý£¬Îö¹¹º¯ÊýÖ´ÐÐÍê³ÉÖ®ºó£¬¿Õ¼ä±»ÊÍ·Å,Îö¹¹º¯ÊýÓÃÓÚ¿Õ¼äµÄÇåÀí¹¤×÷
2. Èç¹û³ÌÐòԱûÓÐÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬Ôòϵͳ»áÌṩһ¸öĬÈϵĿպ¯ÊýÌåÎö¹¹º¯Êý£¬Èç¹û³ÌÐòÔ±ÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÔòϵͳĬÈϵĿպ¯ÊýÌåµÄÎö¹¹º¯Êý²»ÔÚÌṩ
3. Îö¹¹º¯Êý¶¨ÒåµÄ¸ñʽ£º
1£©ÀàÃû×÷Ϊº¯ÊýÃû£¬ÔÚº¯ÊýÃûÇ°ÃæÐèÒª¼Ó~
2£©Îö¹¹º¯ÊýûÓвÎÊýÁÐ±í£¬¿Õ²ÎÊýÁбí
3£©Îö¹¹º¯ÊýÎÞ·µ»ØÖµÀàÐÍ
4£©Îö¹¹º¯Êý²»¿ÉÖØÔØ£¬ÓÐÇÒÖ»ÓÐÒ»¸öÎö¹¹º¯Êý
¸ñʽ£º
//Îö¹¹º¯Êý¿ÉÒÔÔÚÀàµÄÍⲿ¶¨Ò壬Ҳ¿ÉÒÔÔÚÀàµÄÄÚ²¿¶¨Òå
~ˈ̞()
{
}
#include <iostream>
using namespace std;
class Point { int xp; int yp; public: Point(int x = 0, int y = 0); //´ø²Î¹¹Ô캯Êý ~Point(); //Îö¹¹º¯Êý void showPoint(){ cout << "(" << xp << "," << yp << ")" << endl; } }; int main() { Point a;//´´½¨¶ÔÏóͬʱ£¬×Ô¶¯µ÷Óù¹Ô캯Êý Point b(11, 22); a.showPoint(); b.showPoint(); }
Point::~Point() { cout << "Îö¹¹º¯Êý" << endl; } Point::Point(int x, int y) : xp(x), yp(y) { //¶ÔÊý¾Ý³ÉÔ±½øÐи³Öµ²Ù×÷ //xp = x; //yp = y; cout << "´ø²ÎÊýȱʡ¹¹Ô캯Êý" << endl; } |
int main() { Point b(11, 22); { Point a; a.showPoint(); //¶ÔÏ󳬳ö×÷ÓÃÓÚ£¬Ôò×Ô¶¯µ÷ÓÃÎö¹¹º¯Êý£¬Òþʽµ÷Óà } cout << "===============" << endl; b.~Point(); //Îö¹¹º¯Êý¿ÉÒÔ±»ÏÔʽµ÷Óã¬Ò»°ã²»ÏÔʽµ÷Óà cout << "===============" << endl; b.showPoint(); }
//Îö¹¹º¯ÊýÓÃÓÚ×öÇåÀí¿Õ¼ä£¬²»ÊÇÊÍ·Å¿Õ¼ä Point::~Point() { xp = 0; yp = 0; cout << "Îö¹¹º¯Êý" << endl; } |
4. Îö¹¹º¯ÊýµÄ×¢ÒâÊÂÏî
1£©¶ÔÏó³·ÏúµÄʱºò£¬×Ô¶¯µ÷Óã¬Îö¹¹º¯Êý¿ÉÒÔÓɳÌÐòÔ±Ö÷¶¯µ÷Óã¨ÏÔʽµ÷Óã©£¬Ò»°ã²»ÏÔʽµ÷ÓÃ
2£©Ö»Óе±ÀàÄÚÓɶÑÇø¿Õ¼äµÄʱºò£¬¾ÍÐèÒªÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÔÚÎö¹¹º¯ÊýÖÐ×ö¶ÑÇø¿Õ¼äµÄÊͷŹ¤×÷£¬Èç¹ûûÓжÑÇø¿Õ¼ä£¬Ôò¿ÉÒÔ²ÉÓÃϵͳÌṩµÄĬÈϵÄÎö¹¹º¯Êý
3£©Îö¹¹º¯ÊýºÍ¹¹Ô캯ÊýµÄÖ´ÐÐ˳ÐòÏà·´
#include <iostream>
using namespace std;
class Point { int xp; int yp; public: Point(int x = 0, int y = 0); //´ø²Î¹¹Ô캯Êý ~Point(); //Îö¹¹º¯Êý void showPoint(){ cout << "(" << xp << "," << yp << ")" << endl; } }; int main() { Point b(11, 22); Point a; }
//Îö¹¹º¯ÊýÓÃÓÚ×öÇåÀí¿Õ¼ä£¬²»ÊÇÊÍ·Å¿Õ¼ä Point::~Point() { cout << this << endl; //cout << xp << endl; cout << "Îö¹¹º¯Êý" << endl; } Point::Point(int x, int y) : xp(x), yp(y) { cout << this << endl; //cout << xp << endl; cout << "´ø²ÎÊýȱʡ¹¹Ô캯Êý" << endl; } |
2£©Ö»Óе±ÀàÄÚÓɶÑÇø¿Õ¼äµÄʱºò£¬¾ÍÐèÒªÏÔʽ¶¨ÒåÎö¹¹º¯Êý£¬ÔÚÎö¹¹º¯ÊýÖÐ×ö¶ÑÇø¿Õ¼äµÄÊͷŹ¤×÷£¬Èç¹ûûÓжÑÇø¿Õ¼ä£¬Ôò¿ÉÒÔ²ÉÓÃϵͳÌṩµÄĬÈϵÄÎö¹¹º¯Êý
#include <iostream> #include <cstring>
using namespace std;
class Computer { private: char *brand; int price; public: Computer(); Computer(const char * br, int pr); ~Computer(); void setBrand(const char * br); void showComputer(); };
int main() { Computer cmp1; Computer cmp2("HP", 6000); //cmp1.showComputer(); cmp2.showComputer(); cmp2.~Computer();
cmp2.setBrand("sjgdsjd"); }
Computer::Computer() :brand(NULL), price(0) { //brand = NULL; cout << "Î޲ι¹Ô캯Êý" << endl; } Computer::Computer(const char *br, int pr) { //ÉêÇëÒ»¿é¿Õ¼ä brand = new char[strlen(br) + 1]; strcpy_s(brand, strlen(br) + 1, br); price = pr; cout << "´ø²Î¹¹Ô캯Êý" << endl; } void Computer::setBrand(const char * br) { //ÉêÇëÒ»¿é¿Õ¼ä if (brand != NULL) //Èç¹û²»Îª¿Õ£¬ÔòÊÍ·ÅÔÀ´µÄ¿Õ¼ä { delete[]brand; brand = NULL; } //Èç¹ûΪ¿Õ£¬ÔòÖ±½ÓÉêÇëÄÚ´æ¿Õ¼ä brand = new char[strlen(br) + 1]; strcpy_s(brand, strlen(br) + 1, br); }
Computer::~Computer() { if (brand != NULL) { delete[]brand; brand = NULL; } cout << "Îö¹¹º¯ÊýÖ´ÐÐ" << endl; }
void Computer::showComputer() { cout << "Æ·ÅÆ £º" << brand << endl; cout << "¼Û¸ñ £º" << price << endl; } |