Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More C++ Tutorial at BrandonHok.com
- #include <iostream>
- class Copy
- {
- public:
- Copy(int x = 5, int y = 5); // default constructor
- Copy(const Copy& cp); // copy constructor
- void paste(); // member function
- private:
- int x_, y_; // private variables to be declared
- };
- // default constructor definition
- Copy::Copy(int x, int y)
- {
- x_ = x;
- y_ = y;
- }
- // copy destructor definition
- Copy::Copy(const Copy& cp)
- {
- x_ = cp.x_;
- y_ = cp.y_;
- }
- // member function defintion
- void Copy::paste()
- {
- std::cout << x_ << std::endl << y_ << std::endl;
- }
- int main()
- {
- // Try printing the original constructor first
- Copy original;
- original.paste();
- // Now we mae a new object and use the copy constructor
- // Basically, all we we did is made a object that copies another
- Copy newNumbers(30, 60);
- // Uncomment each method and see how each works!
- // method #1 - using assignment operator
- //Copy copier = newNumbers;
- // method #2 - passed as a value
- //Copy copier;
- //copier = Copy(newNumbers);
- // method #3 - copied explicitly
- //Copy copier(newNumbers);
- copier.paste();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement