Login
Order Now
Support
C++ Program into a File Classes

C++ Program into a File Classes

  • 2nd Nov, 2021
  • 16:07 PM
#include
using namespace std;

class A {

public:
    A() {
        x = 0;
    }
    void increment() {
        x++;
    }
    virtual void decrement() {
        x--;
    }
    virtual void print() {
        cout << "x = " << x << endl;
    }
protected:
    int x;

};

class B: public A {
public:
    B() {
        y = 0;
    }
    void increment() {
        x = x + 2;
    }
    virtual void decrement() {
        x = x - 2;
    }
    /**
     * Question 2
     */
    void print() {
        cout << "x = " << x << ". y = " << y << endl;
    }
protected:
    int y;
};

void question4(A a) {
    a.print();
}

A question5(A a) {
    return a;
}

int main() {
    A a;       // a is an object of class A
    A * ptr;   // ptr is a pointer to an object of class A
    ptr = &a;
    ptr->increment();
    ptr->print();
    ptr->decrement();
    ptr->print();

    /**
     * Question 1:
     * In main add an object b of class B
     * Set the pointer ptr to point to b.
     * Call increment() and decrement() on the object pointed to by ptr,
     * print the object after each call. Which method gets called in each case? Why?
     */
    cout << "Question 1:" << endl;
    B b;
    ptr = &b;
    ptr->increment();
    ptr->print();
    ptr->decrement();
    ptr->print();
    cout << "Question 3:" << endl;
    a = b;
    a.print();
    ptr = &a;
    ptr->print();
    cout << "Question 4:" << endl;
    question4(a);
    question4(b);

    cout << "Question 5:" << endl;
    A c = question5(a);
    c.print();
    c = question5(b);
    c.print();

}

Share this post

assignment helpassignment helperassignment expertsassignment writing services