adplus-dvertising

Welcome to the Classes and Operator Overloading in C++ MCQs Page

Dive deep into the fascinating world of Classes and Operator Overloading in C++ with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Classes and Operator Overloading in C++, a crucial aspect of Object Oriented Programming Using C++. In this section, you will encounter a diverse range of MCQs that cover various aspects of Classes and Operator Overloading in C++, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Object Oriented Programming Using C++.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Classes and Operator Overloading in C++. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Object Oriented Programming Using C++.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Classes and Operator Overloading in C++. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Classes and Operator Overloading in C++ MCQs | Page 6 of 39

Q51.
How to declare operator function?
Discuss
Answer: (a).operator operator sign
Q52.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class sample 
    {
        public:
        int x, y;
        sample() {};
        sample(int, int);
        sample operator + (sample);
    };
    sample::sample (int a, int b) 
    {
        x = a;
        y = b;
    }
    sample sample::operator+ (sample param) 
    {
        sample temp;
        temp.x = x + param.x;
        temp.y = y + param.y;
        return (temp);
    }
    int main () 
    {
        sample a (4,1);
        sample b (3,2);
        sample c;
        c = a + b;
        cout << c.x << "," << c.y;
        return 0;
    }
Discuss
Answer: (b).7, 3
Q53.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class Box
    {   
        double length;
        double breadth;
        double height;
        public:
        double getVolume(void)
        {  
            return length * breadth * height;
        }
        void setLength( double len )
        {   
            length = len;
        }
        void setBreadth( double bre )
        {   
            breadth = bre;
        }
        void setHeight( double hei )
        {   
            height = hei;
        }
        Box operator+(const Box& b)
        {  
            Box box;
            box.length = this->length + b.length;
            box.breadth = this->breadth + b.breadth;
            box.height = this->height + b.height;
            return box;
        } 
    };
    int main( )
    {  
        Box Box1;
        Box Box2;
        Box Box3;
        double volume = 0.0;
        Box1.setLength(6.0);
        Box1.setBreadth(7.0);
        Box1.setHeight(5.0);
        Box2.setLength(12.0);
        Box2.setBreadth(13.0);
        Box2.setHeight(10.0);
        volume = Box1.getVolume();
        cout << "Volume of Box1 : " << volume <<endl;
        volume = Box2.getVolume();
        cout << "Volume of Box2 : " << volume <<endl;
        Box3 = Box1 + Box2;
        volume = Box3.getVolume();
        cout << "Volume of Box3 : " << volume <<endl;
        return 0;
    }
Discuss
Answer: (a).Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
Q54.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class Integer 
    {
        int i;
        public:
        Integer(int ii) : i(ii) {}
        const Integer
        operator+(const Integer& rv) const 
        {
            cout << "operator+" << endl;
            return Integer(i + rv.i);
        }
        Integer&
        operator+=(const Integer& rv) 
        {
            cout << "operator+=" << endl;
            i += rv.i;
            return *this;
        }
    };
    int main() 
    {
        int i = 1, j = 2, k = 3;
        k += i + j;
        Integer ii(1), jj(2), kk(3);
        kk += ii + jj;
    }
Discuss
Answer: (a).operator+ operator+=
Q55.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class myclass
    {
        public:
        int i;
        myclass *operator->()
        {return this;}
    };
    int main()
    {
        myclass ob;
        ob->i = 10; 
        cout << ob.i << " " << ob->i;
        return 0;
    }
Discuss
Answer: (a).10 10
Discuss
Answer: (d).adding operation to the existing operators
Q57.
What is the output of this program?
    #include <iostream>
    using namespace std;
    ostream & operator<<(ostream & i, int n)
    {
        return i;
    }
    int main()
    {
        cout << 5 << endl;
        cin.get();
        return 0;
    }
Discuss
Answer: (c).error
Q58.
Which header file is used to declare the complex number?
Discuss
Answer: (b).complex
Q59.
How to declare the complex number?
Discuss
Answer: (b).complex(3,4)
Q60.
How many real types are there in complex numbers?

a.

1

b.

2

c.

3

d.

4

Discuss
Answer: (c).3
Page 6 of 39

Suggested Topics

Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!