0% found this document useful (0 votes)
12 views7 pages

Inheritance

The document defines three classes - A, B, and C - where B inherits from A and C inherits from B. A, B, and C each contain member variables and pointers. Constructors and destructors are defined to allocate and free memory. Overloaded operators like = are also defined to allow object assignment and copying. The main function creates instances of A, B, and C and calls the display function to output member values.

Uploaded by

Jatin Kumar Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Inheritance

The document defines three classes - A, B, and C - where B inherits from A and C inherits from B. A, B, and C each contain member variables and pointers. Constructors and destructors are defined to allocate and free memory. Overloaded operators like = are also defined to allow object assignment and copying. The main function creates instances of A, B, and C and calls the display function to output member values.

Uploaded by

Jatin Kumar Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Quiz 3

Question 2
inheritance.cpp
#include<iostream>

using namespace std;

class A {
    private:
        int a1;
        int a2;
        int *aPtr;
   
    public:
        int size;
        A(int a1, int a2, int size, int value) {
            this->a1 = a1;
            this->a2 = a2;
            this->size = size;

            aPtr = new int[size];


            for(int i=0; i<size; i++)
                aPtr[i] = value;
        }
        A(const A &copy) {
            a1 = copy.a1;
            a2 = copy.a2;
            size = copy.size;

            aPtr = new int[size];

            for(int i=0; i<size; i++)


                aPtr[i] = copy.aPtr[i];
        }
        virtual void display() {
            for(int i=0; i<size; i++)
                cout<<aPtr[i]<<" ";
            cout<<endl;
        }

        A& operator=(const A &that) {


            this->a1 = that.a1;
            this->a2 = that.a2;
            this->size = that.size;

            for(int i=0; i<size; i++)


                this->aPtr[i] = that.aPtr[i];
           
            return *this;
        }
        ~A() {
            delete[] aPtr;
        }
};

class D {
    int d1;
    int d2;
    int *dPtr;

    public:
        int size;
   
        D(int a1, int a2, int size, int value) {
            this->d1 = a1;
            this->d2 = a2;
            this->size = size;

            dPtr = new int[size];


            for(int i=0; i<size; i++)
                dPtr[i] = value;
        }
        D(const D &copy) {
            d1 = copy.d1;
            d2 = copy.d2;
            size = copy.size;

            dPtr = new int[size];

            for(int i=0; i<size; i++)


                dPtr[i] = copy.dPtr[i];
        }
        virtual void display() {
            for(int i=0; i<size; i++)
                cout<<dPtr[i]<<" ";
            cout<<endl;
        }

        D& operator=(const D &that) {


            this->d1 = that.d1;
            this->d2 = that.d2;
            this->size = that.size;

            for(int i=0; i<size; i++)


                this->dPtr[i] = that.dPtr[i];
           
            return *this;
        }
        ~D() {
            delete[] dPtr;
        }
};

class B : public A
{
    int b1;
    int b2;
    int *bPtr;
    D d;

    public:
        B(int b1, int b2, int size, int value, int a1, int a2, int
sizeOfA, int valueForA, int d1, int d2, int sizeOfD, int valueForD)
        : A(a1, a2, sizeOfA, valueForA), d(d1, d2, sizeOfD, valueForD)
{
            this->b1 = b1;
            this->b2 = b2;

            bPtr = new int[size];

            for(int i=0; i<size; i++)


                bPtr[i] = value;
        }
        B(const B& that): A(that), d(that.d) {
            b1 = that.b1;
            b2= that.b2;
           
            for(int i=0; i<size; i++)
                bPtr[i] = that.bPtr[i];
        }

        virtual void display() {


            for(int i=0; i<size; i++)
                cout<<bPtr[i]<<" ";

            cout<<endl;
        }
        B& operator=(const B& that) {
            A::operator=(that);
            b1 = that.b1;
            b2 = that.b2;
            size = that.size;
            for(int i=0; i<size; i++) {
                bPtr[i] = that.bPtr[i];
            }
            return *this;
        }
        ~B() {
            delete[] bPtr;
        }

};

class C: public B {
    int c1;
    int c2;
    int *cPtr;

    public:
        int size;

        C(  int c1, int c2, int size, int value,


            int b1, int b2, int sizeOfB, int valueForB,
            int a1, int a2, int sizeOfA, int valueForA,
            int d1, int d2, int sizeOfD, int valueForD) : B(b1, b2,
sizeOfB, valueForB, a1, a2, sizeOfA, valueForA, d1, d2, sizeOfD,
valueForD)
        {
            this->c1 = c1;
            this->c2 = c2;
            this->size = size;

            cPtr = new int[size];

            for(int i=0; i<size; i++)  


                cPtr[i] = value;
        }
        virtual void display() {
            for(int i=0; i<size; i++)
                cout<<cPtr[i]<<" ";

            cout<<endl;
        }

        C& operator=(const C& that){


            c1 = that.c1;
            c2 = that.c2;
            size = that.size;
            B::operator=(that);

            for(int i=0; i<size; i++)


                cPtr[i] = that.cPtr[i];
            return *this;
        }
        C(const C& that): B(that) {
            c1 = that.c1;
            c2= that.c2;
           
            for(int i=0; i<size; i++)
                cPtr[i] = that.cPtr[i];
        }

        ~C() {
            delete[] cPtr;
        }
};

int main() {
    C c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    B b(4,4,4,4,4,4,4,4,4,4,4,4);
    A a(3,3,3,3);

    C see(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
    c = see;
    c.display();
    // c.display();
    // b.display();
    // a.display();
}

You might also like