KEMBAR78
C++ Unit 1 Answers | PDF
0% found this document useful (0 votes)
6 views3 pages

C++ Unit 1 Answers

The document provides important questions and answers related to C++ operator overloading and friend functions. It covers definitions, rules, examples of overloading operators, and comparisons between friend functions and member functions. Additionally, it discusses the merits and demerits of friend functions and provides code snippets for practical understanding.

Uploaded by

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

C++ Unit 1 Answers

The document provides important questions and answers related to C++ operator overloading and friend functions. It covers definitions, rules, examples of overloading operators, and comparisons between friend functions and member functions. Additionally, it discusses the merits and demerits of friend functions and provides code snippets for practical understanding.

Uploaded by

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

C++ IMPORTANT QUESTIONS WITH ANSWERS

Unit 1: Operator Overloading and Friend Function

Q: What is operator overloading? Why is it necessary?

A: Operator overloading allows us to redefine the way operators work for user-defined types. It is

necessary to make operations on objects intuitive (e.g., using + for adding complex numbers).

Q: Rules for overloading operators.

A: 1. Only existing operators can be overloaded.

2. New operators cannot be created.

3. Overloaded operators must have at least one user-defined operand.

4. Precedence and associativity cannot be changed.

Q: Which operators cannot be overloaded?

A: Operators that cannot be overloaded include: :: (scope resolution), . (member access), .*

(pointer-to-member), sizeof, typeid, and ?: (ternary conditional).

Q: Program to overload binary + for complex numbers.

A: class Complex {

public:

int real, imag;

Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Complex operator+(const Complex& obj) {

return Complex(real + obj.real, imag + obj.imag);

};

Q: Overload unary operators (any three).

A: class Sample {

int a;

public:
Sample(int x) : a(x) {}

Sample operator-() { return Sample(-a); }

Sample operator++() { a++; return *this; }

Sample operator--(int) { Sample temp = *this; a--; return temp; }

};

Q: Explain friend function with example.

A: Friend functions can access private members of a class. Example:

class A {

int x;

friend void show(A);

};

void show(A a) { cout << a.x; }

Q: Merits & demerits of friend functions.

A: Merits:

- Access private data.

- Useful for operator overloading.

Demerits:

- Breaks encapsulation.

- Tight coupling between classes.

Q: Overload << and >> operators with syntax.

A: friend ostream& operator<<(ostream &out, const ClassName &obj);

friend istream& operator>>(istream &in, ClassName &obj);

Q: Compare friend function with member function.

A: Friend: Defined outside class, no 'this' pointer, not inherited.

Member: Defined inside class, has 'this' pointer, supports inheritance.

Q: Program to overload == operator.

A: bool operator==(const ClassName& obj) {


return this->data == obj.data;

Q: Overload unary -- operator.

A: ClassName operator--() {

--data;

return *this;

You might also like