Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This code is not working as expected.
- //Fix the code and reply with your edited code.
- #include <iostream>
- using namespace std;
- class Line {
- public:
- int getNum() const;
- Line(int value); // overloaded constructor
- Line(const Line &obj); // copy constructor
- ~Line(); // destructor
- Line &operator = (const Line& right) {
- cout << "Assignment operator." << endl;
- if (this != &right) {
- delete ptr;
- ptr = new int;
- *ptr = *right.ptr;
- }
- return *this;
- }
- private:
- int *ptr;
- };
- // Member functions definitions
- Line::Line(int num) {
- cout << "Overloaded constructor." << endl;
- ptr = new int;
- *ptr = num;
- }
- Line::Line(const Line &obj) {
- cout << "Copy constructor." << endl;
- ptr = new int;
- *ptr = *obj.ptr; // copy the value
- }
- Line::~Line() {
- cout << "Freeing memory!" << endl;
- delete ptr;
- ptr = nullptr;
- }
- int Line::getNum() const {
- return *ptr;
- }
- void displayNum(Line obj) {
- cout << "value of num : " << obj.getNum() << endl;
- }
- // Main function for the program
- int main() {
- Line line1(10);
- Line line2 = line1;
- Line line3(30);
- line3 = line2;
- displayNum(line1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement