LAB Practical IV

a) Write a program to show overriding Base class members in a Derived class
b) Write a program to show Complexity and ambiguity in Inheritance  

Comments

  1. AIM : TO create a program to show overriding Base class members in a Derived class.
    PROCEDURE :
    If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.
    PROGRAM :
    #include
    using namespace std;

    class Base
    {
    public:
    Base() : b(0) {}
    int get();
    virtual void sayhello()
    {
    cout << "Hello from Base with b: " << b << endl;
    }
    private:
    int b;
    };

    int Base::get()
    {
    sayhello();
    return b;
    }

    class Derived : public Base
    {
    public:
    Derived(double b_):b(b_){}
    void sayhello()
    {
    cout << "Hello from Derived with b: " << b << endl;
    }
    private:
    double b;
    };

    int main()
    {
    Derived d(10.0);
    Base b = d;

    cout << "Derived b: " << d.get() << endl;
    cout << "Base b: " << b.get() << endl;
    }
    Error : 0
    Output :
    Hello from Derived with b: 10
    Derived b: 0
    Hello from Base with b: 0
    Base b: 0
    Result :
    I have successfully done my program.

    ReplyDelete
  2. AIM: To create a program using overriding base class in a derived
    PROCEDURE: In this program we inherit base class member function (void introduce) in a derived class .If we inherit base class into derived class then it is called overriding.
    PROGRAM:
    #include
    using namespace std;
    class person
    {
    public:
    void introduce()
    {
    cout<<"I am a person"<<endl;
    }
    };
    class student:public person
    {
    public:
    void introduce()
    {
    cout<<"I am a student and i am awesome"<<endl;
    }
    };
    int main()
    {
    student s;
    s.introduce();
    return 0;
    }
    ERROR:0
    OUTPUT: I am a student and i am awesome.
    RESULT: I have done my program successfully.

    ReplyDelete
  3. AIM: To create a program to show complexity and ambiguity in inheritance.
    PROCEDURE:
    In this c++ program there are two base class(one base class is teacher and another base class is student) and one derived class(person).Ambiguity arises in c++ when general rules leave the choice of more than one parent without a ambiguity criteria.
    PROGRAM:
    #include
    using namespace std;
    class teacher
    {
    protected:
    int x;
    public:
    void display()
    {
    cout<<"Enter the value of x:";
    cin>>x;
    }
    };
    class student
    {
    protected:
    int y;
    public:
    void display()
    {
    cout<<"Enter the value of y:";
    cin>>y;
    }
    };
    class person:public teacher,public student
    {
    public:
    void add()
    {
    cout<<"sum="<<x+y<<endl;
    }
    };
    int main()
    {
    person obj;
    obj.student::display();
    obj.teacher::display();
    obj.add();
    return 0;
    }
    ERROR:0
    OUTPUT:
    Enter the value of x:22
    Enter the value of y:12
    Sum:34
    RESULT:I have done my program successfully.

    ReplyDelete
  4. AIM: To create a program to show complexity and ambiguity in inheritance.
    PROCEDURE:
    Step 1 : Start the program.
    Step 2 : In this program, there are two base class i.e. sample and max.
    Step 3 : And one derived class i.e. derived.
    Step 4: when general rules leave the choice of more than one parent without a ambiguity criteria.
    Step 5 : End the program.
    PROGRAM:
    #include
    using namespace std;
    class sample
    {
    protected:
    int a;
    public:
    void getdata()
    {
    cout<<"Enter the value of a:";
    cin>>a;
    }
    };
    class max
    {
    protected:
    int b;
    public:
    void getdata()
    {
    cout<<"Enter the value of b:";
    cin>>b;
    }
    };
    class derived:public sample , public max
    {
    public:
    void product()
    {
    cout<<"product="<<a*b<<endl;
    }
    };
    int main()
    {
    derived d;
    d.max::getdata();
    d.sample::getdata();
    d.product();
    return 0;
    }
    ERROR : 0
    OUTPUT :
    Enter the value of a:10
    Enter the value of b:15
    Product:150
    RESULT:I have successfully done my program.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. AIM:- To create a program to show overriding base class member in drived class.
    Procedure:- the both base class and derived class have a member function with same name and argument.
    If you create an object of the drive class and called member function which exist in both class(base,derived).
    PROGRAM:-
    #include
    using namespace std;
    class base
    {
    public:
    void show()
    {
    cout<<"it is a base class"<<endl;
    }
    };
    Class derived :public base
    {
    public:
    void show()
    {
    cout<<"it is derived class and inherite the property of base class"<<endl;
    }
    };
    int main()
    {
    derived prince;
    prince.show();
    return 0;
    }
    ERROR:0
    OUTPUT: it is a derived class and inherit the property of base class
    RESULT:I have successfully done my program.

    ReplyDelete
  7. AIM:- To create a program to show overriding base class member in drived class.

    Procedure:-
    the both base class and derived class have a member function with same name and argument.
    If you create an object of the drive class and called member function which exist in both class(base,derived).
    PROGRAM:-
    #include
    using namespace std;
    class base
    {
    public:
    void show()
    {
    cout<<"it is a base class"<<endl;
    }
    };
    Class derived :public base
    {
    public:
    void show()
    {
    cout<<"it is derived class and inherite the property of base class"<<endl;
    }
    };
    int main()
    {
    derived prince;
    prince.show();
    return 0;
    }
    ERROR:0
    OUTPUT: it is a derived class and inherit the property of base class
    RESULT:I have successfully done my program.

    ReplyDelete
  8. AIM: Write a program to show complexity and ambugity in inheritance.
    Procedure: In this program we have used multilevel inheritance with same member function name and to solve the complexity faced by the compiler during run time we use scope resolution operator.
    PROGRAM:
    #include
    using namespace std;
    class grandpa
    {
    private:
    int height;
    public:
    void heightdata()
    {
    cout<<"grandpa is tall"<<endl;

    }
    public:
    void petlove()
    {
    cout<<"grandpa loves pet"<<endl;
    }

    };
    class daddy:public grandpa
    {

    public:
    void heightdata()
    {
    cout<<"Daddy is also tall"<<endl;

    }

    };
    class me: public daddy
    {

    public:
    void heightdata()
    {
    cout<<"But i am not so tall"<<endl;
    }
    void sports()
    {
    cout<<"I love sport"<<endl;
    }
    };
    int main()
    {
    me obj;
    grandpa obj2;
    daddy obj3;
    obj2.petlove();

    obj.grandpa::heightdata();
    obj.daddy::heightdata();
    obj.heightdata();
    obj.sports();
    }
    OUTPUT:
    grandpa is tall
    grandpa loves pet
    Daddy is also tall
    But i am not so tall
    I love sport
    ERROR:
    0
    RESULT: Executed program successfully


    ReplyDelete
  9. AIM : TO create a program to show overriding Base class members in a Derived class.
    Procedure: In this C++ program if we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.
    Program:
    #include
    using namespace std;
    class base
    {
    public:
    int a,b,c;
    void input()
    {
    cout<<"Enter two numbers"<>a>>b;
    }
    void calculation()
    {
    c=a+b;
    cout<<"Sum is: "<<c<<endl;
    }
    };
    class derived: public base
    {
    public:
    void calculation()
    {
    c=a*b;
    cout<<"Product is: "<<c;
    }
    };
    int main()
    {
    derived d;
    base b;
    d.input();
    b.calculation(); //calling base class functions
    d.calculation(); //overriding the function
    return 0;
    }
    Error: 0
    Output: Enter two numbers
    45
    45
    Sum is: 4883904
    Product is: 2025
    Result: This program execute successfully

    ReplyDelete
  10. AIM: To create a program to show complexity and ambiguity in inheritance.
    Procedure: In this program we have used multilevel inheritance with same member function name and to solve the complexity faced by the compiler during run time we use scope resolution operator.
    PROGRAM:
    Program:
    #include
    using namespace std;
    class base1
    {
    public:
    void display()
    {
    cout<<"Myself Zaira Zahid,"<<endl;
    }
    };
    class base2
    {
    public:
    void display()
    {
    cout<<"pursuing B-tech from NIMS University"<<endl;
    }
    };
    class derived: public base1, public base2
    {
    public:
    void show()
    {
    cout<<"Showing ambiguity is multiple inheritance"<<endl;
    }
    };
    int main()
    {
    derived d;
    d.show();
    d.base1::display();
    d.base2::display();
    return 0;
    }
    Error: No error found.
    Output: Showing ambiguity is multiple inheritance
    Myself Zaira Zahid,
    pursuing B-tech from NIMS University.
    Result: This program executed successfully.

    ReplyDelete

Post a Comment

Popular posts from this blog

INTRODUCTION OF HADOOP & SPARK

B.TECH DIP- System Programming