0% found this document useful (0 votes)
6 views5 pages

1) Explain About Inheritance? Explain The Types of Inheritance?

Inheritance in C++ allows a derived class to acquire properties and behaviors from a base class, promoting code reusability and reliability. There are five types of inheritance: single, multiple, hierarchical, multilevel, and hybrid. Operator overloading enables custom operations on user-defined data types, following specific rules such as only overloading built-in operators and maintaining their original behavior.
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 views5 pages

1) Explain About Inheritance? Explain The Types of Inheritance?

Inheritance in C++ allows a derived class to acquire properties and behaviors from a base class, promoting code reusability and reliability. There are five types of inheritance: single, multiple, hierarchical, multilevel, and hybrid. Operator overloading enables custom operations on user-defined data types, following specific rules such as only overloading built-in operators and maintaining their original behavior.
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/ 5

1) Explain about Inheritance? Explain the types of Inheritance?

In C++, inheritance is a process in which one object acquires all the properties and behaviors
of its parent object automatically. In such way, you can reuse, extend or modify the attributes
and behaviors which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class. The derived class is the specialized
class for the base class.

Advantage:

✓ Code reusability: Now you can reuse the members of your parent class. So, there is no
need to define the member again. So less code is required in the class.
✓ It increases the code reliability by definite body to the program.

Types of Inheritance:

C++ supports five types of inheritance:

• Single inheritance
• Multiple inheritance
• Hierarchical
• Multilevel
• Hybrid inheritance

Defining Derived Classes:


A derived class is specified by defining its relationship with the base class in addition
to its own details. The general syntax of defining a derived class is as follows:
class derived_classname : Access specifier baseclass name
{
__
__ // members of derived class
};

The colon indicates that the a-class name is derived from the base class name. The access
specifier or the visibility mode is optional and, if present, may be public, private or
protected. By default, it is private. Visibility mode describes the status of derived features.

❖ Single Inheritance: Single inheritance is defined as the inheritance in which a derived class
is inherited from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.

Syntax: class subclass_name : access_mode base_class {

//body of subclass

};

Visibility modes can be classified into three categories:

o Public: When the member is declared as public, it is accessible to all the functions of
the program.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.

❖ Multilevel inheritance : Multilevel inheritance is a process of deriving a class from another


derived class.

Multiple Inheritance: Multiple inheritance is the process of deriving a new class that
inherits the attributes from two or more classes.

Syntax: class subclass_name : access_mode base_class1, access_mode base_class2, .... {


//body of subclass
};
Hierarchical Inheritance: Hierarchical inheritance is defined as the process of deriving more
than one class from a base class.

❖ Hybrid Inheritance: Hybrid inheritance is a combination of more than one type of


inheritance.
2) What is Operator Overloading? Explain the rules for Overloading
operators?
✓ An operator is used to perform some particular operation on operands. But if we want to
add two objects which are instance of the class then compiler will show an error.
Basically operators will work only on predefined datatypes.
✓ With the help of operator overloading we can add two user defined datatypes. Here
operators are used instead of functions to perform a particular operation.
✓ To define an additional task to an operator, we must specify what it means in relation to
the class to which the operator is applied. This is done with the help of a special function
called operator function, which describes the task.

Syntax:- return-type class-name :: operator op( arg-


list)
{ function body ;
}
Where return type is the type of value returned by the specified operation and op is the
operator being overloaded. The op is preceded by the keyword operator, operator op is the
function name. operator functions must be either member function, or friend function.

Rules for operator overloading:

✓ Only built-in operators like (+, -, *, /, etc)can be overloaded.


✓ We cannot overload all of those operators that are not a part of C++ language like ‘$’.
✓ In operator overloading we are passing object as an argument.
✓ We can’t change the meaning of an operator. Operator overloading never overrides the
existing behaviour of an operator. We can’t change the basic behaviour of an operator.
✓ In Operator overloading the left hand side operand should be a user defined data type at
least one user defined datatype should be used.
✓ Some operators cannot be overloaded they are : Scope resolution(::)
Member accessing(. , *)
Ternanry/conditional operator( ?:)
Sizeof( ) operator
✓ The operator / member function should be the member of the same class.
3) Explain the overloading unary operators, overloading binary
operators?
Unary operator: It means performing operations on a single operand.
Eg: a++, --a
✓ To implement operator overloading then we have to define operator function. Operator
function may be either member function or friend function.
✓ If unary function is overloaded using member function, then 0 arguments needed where
as if unary operator is overloaded using friend function then one argument is needed.

Example: #include <iostream> using


namespace std; class
Count {
private: int value;
public: Count()
{ value = 5; }
void operator ++ ( ) { ++value;}
void display() { cout << "Count: " << value << endl; }};
int main() {
Count count1;
++count1;
count1.display();
}

Binary operator: It means performing operations on two operands. If binary operator is


overloaded using member function then 1 argument is needed where as if binary operator is
overloaded using friend function then 2 arguments are needed.
Example:
#include <iostream> using
namespace std;
class Test { int a;
public: void get() {
cout << "Enter a value: ";
cin >> a; }
void compare(Test t2) {
if (a == t2.a)
cout << "Objects are equal"; else
cout << "Objects are not equal"; }};
int main( ) {
Test t1, t2;
cout << "Enter a value for t1: ";
t1.get();
cout << "Enter a value for t2: ";
t2.get(); t1.compare(t2);
return 0;}

You might also like