העמסת בנאים
כפי שאתם יודעים אני דיי ניובי, ניסתי לעשות כמה בנאים, כדי לבדוק מה קורה שאני קורא לפונ', כלומר באיזה בנאי הפונ' תבחר: אז זה הקוד
כפי שאתם יודעים אני דיי ניובי, ניסתי לעשות כמה בנאים, כדי לבדוק מה קורה שאני קורא לפונ', כלומר באיזה בנאי הפונ' תבחר: אז זה הקוד
#include <iostream.h> class CAT { public: CAT(); // default constructor CAT (const CAT &); // copy constructor CAT::CAT(int age); ~CAT(); // destructor int GetAge() const {return *itsAge; } int GetWeight() const { return *itsWeight; } void SetAge(int age) { *itsAge = age; } private: int *itsAge; int *itsWeight; }; CAT::CAT() { itsAge = new int; itsWeight = new int; *itsAge = 5; *itsWeight = 9; cout << "Consturctor\n"; } CAT::CAT(const CAT & rhs) { itsAge = new int; itsWeight = new int; cout << "Copy Consturctor\n"; *itsAge = rhs.GetAge(); // public access *itsWeight = *(rhs.itsWeight); // private access } CAT::CAT(int age) { itsAge = new int; itsWeight = new int; cout << "Copy Consturctor2\n"; *itsAge = 28; // public access } CAT::~CAT() { cout << "Disturctor\n"; delete itsAge; itsAge = 0; delete itsWeight; itsWeight = 0; } CAT fun(CAT miri); int main() { CAT frisky; cout << "frisky's age: " << frisky.GetAge() << endl; cout << "Setting frisky to 6...\n"; frisky.SetAge(6); cout << "Creating boots from frisky\n"; CAT boots(frisky); cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boots' age: " << boots.GetAge() << endl; cout << "setting frisky to 7...\n"; frisky.SetAge(7); cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boot's age: " << boots.GetAge() << endl; cout << "Making mizi\n"; CAT mizi = fun(frisky); int s = mizi.GetAge(); cout << "mizi age is:" << s << endl ; return 0; } CAT fun(CAT miri) { cout << "in fun\n"; miri.SetAge(33); return miri; }
בקריאה לפונ' הוא בוחר רק את הבנאי השניה (cat::cat(const cat & rhs... למה הוא לא בוחר בבנאי השני cat::cat (int age)?