Oopsunit 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Unit 2

Q. What is polymorphism? Give example.


Ans: It is one of the important part of OOP. It means one interface many actions. There are two
types of polymorphism:

1.COMPILE TIME POLYMORPHISM:

Overloaded functions are often selected based on prototype of the function. It matches no. of
passing parameters, return type, type of passing parameters. Compile time polymorphism can
be of function or operator overloading. The concept of selecting appropriate overloaded
function by the compiler is known as EARLY BINDING. Early binding is also called as STATIC
BINDING.

Ex .Operator Overloading, Function Overloading

2.RUN TIME POLYMORPHISM:

If the object to the function is called at run time then it is called run time polymorphism.The
concept is called LATE BINDING. It is also known as DYNAMIC BINDING.

Ex. Virtual Functions

Q. Why is it necessary to overload an operator?


Ans: It is necessary to overload an operator due to following reason:

1. Operator overloading allows you to redefine the way operator works for user-defined types
only (objects, structures). It cannot be used for built-in types (int, float, char etc.).
2. Two operators = and & are already overloaded by default in C++. For example: To copy objects
of same class, you can directly use = operator. You do not need to create an operator function.
3. Operator overloading cannot change the precedence and associatively of operators. However,
if you want to change the order of evaluation, parenthesis should be used.
4. There are 4 operators that cannot be overloaded in C++. They are :: (scope
resolution), . (member selection), .* (member selection through pointer to function)
and ?: (ternary operator).
Q.What is operator overloading? Explain unary and binary operator
overloading.
Ans: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 classname::operator op(arglist)
{
Function body;
}
1.UNARY OPERATOR OVERLOADING:
The unary operators operate on a single operand and following are the examples of Unary
operators −

 The increment (++) and decrement (--) operators.


 The unary minus (-) operator.
 The logical not (!) operator.
The unary operators operate on the object for which they were called and normally, this
operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they
can be used as postfix as well like obj++ or obj--.

#include<iostream>
using namespace std;
class complex {

int a, b, c;
public:
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}
void operator++() {
a = ++a;
b = ++b;
}
void display() {
cout << a << "+\t" << b << "i" << endl;
}
};

void main() {
complex obj;
obj.getvalue();
obj++;
}

2.BINARY OPERATOR OVERLOADING:


As the name suggests, those operators which operate on two operands or data are called
binary operators.

#include<iostream>

using namespace std;

class complex

{ private:

int real,imag;

public:

void getvalue()

{ cout<<"Enter the value of real and imaginary number:";

cin>>real>>imag;

complex operator+(complex obj)

complex temp;
temp.real=real+obj.real;

temp.imag=imag+obj.imag;

return(temp);

void display()

cout<<real<<"+"<<"("<<imag<<")"<<"i"<<"\n";

};

int main()

{ complex c1,c2,c3,c4;

c1.getvalue();

c2.getvalue();

c3 = c1+c2;

cout<<"Result is:\n";

c3.display();

return 0;

Q.What are the rules followed in operator overloading.

Ans:Following rules are followed in operator overloading:

1. The first and basic rule of operator overloading is: we can overload unary operator as
only unary operator, it cannot be overload as binary operator and vice versa.
2. We cannot overload those operators that are not a part of C++ language like ‘$’.
3. We can perform operator overloading in only user defined classes. We cannot change
the operators existing functionality.
4. Using operator overloading we cannot change the presidency and associatively of
operators.
5. There are some operators cannot be overloaded.

Q. Which operators cannot be overloaded.


Ans: There are some operators cannot be overloaded that are given below:

o :: Scope resolution operator.


o . Class membership operator.
o ?: ternary or conditional operator.
o .* pointer to member operator.
o ->* pointer to member operator.

Q. How data conversion is possible in C++.


Ans:It means converting one form of data to another.Implicit conversions are performed
automatically when value copies is complatible type.

Ex. Short s=1500;

int a;

a=s;

In the above example s is promoted to integer automatically.

Converting from smaller to larger data types is known as type promotion.

Q.What is friend function. Explain with example.


Ans: A friend function of a class is defined outside that class' scope but it has the right to
access all private and protected members of the class. Even though the prototypes for friend
functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class
template, in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the function prototype in the class
definition with keyword friend as follows −
class Box {
double width;

public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};

Q. When is friend function compulsory? Give example.


Ans: Declaring a function as a friend of a class is required only if that function is not a member
of the class and it needs access to one or more of the class’s private members.

This situation frequently comes up when overloading the inserter << and extractor >> operators
for a class, to allow easy output of an object to a stream, or input of an object from a stream.
Because the object appears on the right side of these operators, the overloaded operator
functions cannot be class member functions; they must be functions defined outside the class.

It’s possible that these functions could get everything they need by accessing only public
members of the class. In that case, the functions don’t need to be declared as friends of the
class, because they don’t need access to private members. But these functions typically need to
access at least one private member, so to gain that access, they need to be declared as friends
of the class.

Q. A friend function cannot be used to overload an assignment operator


(=).Why?
Ans: The assignment operator is explicitly required to be a class member operator. That is a
sufficient reason for the compiler to fail to compile your code. Assignment is one of the special
member functions defined in the standard (like the copy constructor) that will be generated by
the compiler if you do not provide your own.

Unlike other operations that can be understood as external to the left hand side operator, the
assignment is an operation that is semantically bound to the left hand side: modify this
instance to be equal to the right hand side instance (by some definition of equal), so it makes
sense to have it as an operation of the class and not an external operation. On the other hand,
other operators as addition are not bound to a particular instance: is a+b an operation
of a or b or none of them? -- a and bare used in the operation, but the operation acts on
the result value that is returned.
Q10.What is inheritance? What are the types of inheritance? Give an
example of each.
Ans: The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Derived Class: The class that inherits properties from another class is called Sub class or
Derived Class.

Base Class:The class whose properties are inherited by sub class is called Base Class or Super
class.

Types of Inheritance in C++


1. Single Inheritance: In single inheritance, a class is allowed to inherit from only
one class. i.e. one sub class is inherited by one base class only.

Syntax:

class subclass_name : access_mode base_class


{
//body of subclass
};
#include <iostream>
using namespace std;
  
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  class Car: public Vehicle{
  
};
  

int main()
{   Car obj;
    return 0;
}
Output:
This is a vehicle

2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can 


inherit from more than one classes. i.e one sub class is inherited from more than
one base classes.

Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};

#include <iostream>

using namespace std;

  class Vehicle {

  public:

    Vehicle()

    { cout << "This is a Vehicle" << endl;

    }

};

  class FourWheeler {

  public:
    FourWheeler()

    {

      cout << "This is a 4 wheeler Vehicle" << endl;

    }

};

  class Car: public Vehicle, public FourWheeler {

  

};

  int main()

{   Car obj;

    return 0;

Output:
This is a Vehicle
This is a 4 wheeler Vehicle

3.Multilevel Inheritance: In this type of inheritance, a derived class is created from


another derived class.
#include <iostream>
using namespace std;
  
class Vehicle 
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};

class fourWheeler: public Vehicle


{  public:
    fourWheeler()
    {
      cout<<"Objects with 4 wheels are vehicles"<<endl;
    }
};

class Car: public fourWheeler{


   public:
     car()
     {
       cout<<"Car has 4 Wheels"<<endl;
     }
};

int main()
{   Car obj;
    return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4.Hierarchical Inheritance: In this type of inheritance, more than one sub class is
inherited from a single base class. i.e. more than one derived class is created from a
single base class.

#include <iostream>
using namespace std;
  
class Vehicle 
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  class Car: public Vehicle
{
  
};
  
// second sub class
class Bus: public Vehicle
{
      
};
int main()
{    Car obj1;
    Bus obj2;
    return 0;
}
Output:
This is a Vehicle
This is a Vehicle

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than


one type of inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.

  #include <iostream>
using namespace std;
  
class Vehicle 
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  class Fare
{
    public:
    Fare()
    {
        cout<<"Fare of Vehicle\n";
    }
};
class Car: public Vehicle
{
  
};
  
class Bus: public Vehicle, public Fare
{
      
};
  
int main()
{    Bus obj2;
    return 0;
}
Output:
This is a Vehicle
Fare of Vehicle

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy