Operator Overloading |C++

Operator Overloading


Operator overloading is one of the important feature in c++. For operator overloading there are certain restriction and limitations such as:
  1. Only existing operator can be overloaded new operator can not be created.
  2. The overloaded operator have at least one operand that is of user define type.
  3. We can not change the basic meaning of operator.
  4. Overloaded operator follow the syntax rule of original operator that can not be violated.
  5. Unary operator overloaded by means of member function, take no explicit argument and return no explicit value.But those operator overloaded by means of friend function take one reference argument i.e the object of relevant class.
  6. Binary operator overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit argument.
  7. When using binary operator overloaded through a member function the left hand operand must be an object of relevant class.
  8. Binary arithmetic operator such as '+' , '-' , '*' , '/' must explicitly to change their own arguments.
  9. There are some operator that can not be overloaded such as
    • size of operator ( sizeof )
    • Membership operator ( . )
    • Pointer to membership operator ( * )
    • Scope resolution operator (: :)
    • Conditional Operator ( ? : )
  10. We can not use friend function to overload certain operator.
    • Assignment operator ( = )
    • Function call operator ( )
    • Sub scripting operator ( [ ] )
    • Class member access operator ( -> )

Unary Operator Overloading


Before understanding unary operator overloading let we discuss about unary operators.
Unary operators operates on a single operand to produce new value.

Type Unary Operator

  • Increment (+ +)
  • Decrement (- -)
  • Not ( ! )
  • Unary minus ( - )
  • Sizeof operator (sizeof( ) )
  • Addressof operator ( & )

Example

Copy Code
#include <iostream>

using namespace std;
class unary
{
    int a;
    public:
    unary operator -- ()
    {
        a = - a;
        
    }
    void read()
    {
        cout<<"Enter Number:";
        cin>>a;
    }
    void print()
    {
        cout<<"a="<<a<<"\n";
    }
};


int main()
{
  unary u1;
  u1.read();
  --u1;
  u1.print();
  return 0;
}

Output:

Enter Number:5
a=-5

Binary Operator Overloading


Before understanding binary operator overloading let we discuss about binary operators.
Binary operators operates on two operand to produce new value.

Type Binary Operator

  • Equal (= =)
  • Not equal to (! =)
  • Less than ( < )
  • Greater than ( > )
  • Less than equal to (< =)
  • Greater than equal to (> =)
  • Logical AND ( && )
  • Logical OR (| |)
  • Plus ( + )
  • Minus ( - )
  • Multiplication ( * )
  • Division ( / )

Example

Copy Code
#include <iostream>

using namespace std;
class binary
{
    int a,b;
    public:
    binary operator + (binary ob)
    {
        a=a+ob.a;
        b=b+ob.b;
    }
    void read()
    {
        cout<<"Enter real:";
        cin>>a;
        cout<<"Enter imagenary:";
        cin>>b;
    }
    void print()
    {
        cout<<a<<"+"<<b<<"i"<<"\n";
    }
};


int main()
{
  binary b1,b2,b3;
  b1.read();
  b1.print();
  b2.read();
  b2.print();
  b3=b1-b2;
  b1.print();

    return 0;
}

Output:

Enter real:2
Enter imagenary:3
2+3i
Enter real:5
Enter imagenary:6
5+6i
7+9i


Constructors and Destructor in C++

Constructors & Destructor in C++

Constructors


A constructor is a special member function whose task is to initialize the object of its class. It is special because its name is the same as the class name. The constructor is call whenever an object of its associated class is created. It is called constructor because it construct the value of data member of class. Constructor must follow following properties.

Properties


  1. Name of constructor is same as the class name.
  2. Constructor will call implicitly at the time of declaration.
  3. Constructor does not have any return type.
  4. Constructor are used to define object that mean to initialize member data.
  5. Constructor must be a public member function of a class. If constructor is private then user is unable to create object of such class.
  6. Constructor can be overload that means more than one constructor is possible in a class.
  7. We can not refer their address.

Type of Constructors


  1. Parameterized Constructor
  2. Default Argument Constructor such as
  3. Copy Constructor

Parameterized Constructor


The constructor that can take argument is called parameterized constructor.
Example:
Copy Code
#include <iostream>

using namespace std;
class add
{
    int a,b;
    public:
    add(); 
    add(int);
    add(int,int);
    void print();
};
add::add()
{
    a=2;
    b=5;
}
add::add(int x)
{
    a=x;
    b=x;
}
add::add(int x,int y)
{
    a=x;
    b=y;
}
void add::print()
{
    cout<<"\n"<<"a="<<a<<"\n"<<"b="<<b<<"\n"<<"Sum="<<a+b<<"\n";
}
int main()
{
    add a1;
    a1.print();
    add a2(10);
    a2.print();
    add a3(10,20);
    a3.print();

    return 0;
}

In the above example show how to use zero argument ,one argument,two argument constructor in any program.

Output

a=2
b=5
Sum=7

a=10
b=10
Sum=20

a=10
b=20
Sum=30

Default Argument Constructor


It is possible to define constructor with default arguments.

Example:


Add(int x,int y=0);
Here the default value of y is 0.

If the statement is Add A1(10);
That means assign the value 10 for x and 0 for y by default.

If the statement is Add A1(10,20);
That means assign 10 for x and 20 fro y. The actual parameter when specified overrides the default value.

Note: It is most important to distinguish between the default constructor Add::Add() and the default argument constructor Add::Add(int=0).The default argument can be called with either by one argument or no argument.When called with no argument it become default constructor.

Example:


Copy Code
#include <iostream>

using namespace std;
class add
{
    int a,b;
    public:
    add(); 
    //add(int);
    add(int,int=0); //Default Argument Constructor
    void print();
};
add::add()
{
    a=2;
    b=5;
}

add::add(int x,int y)
{
    a=x;
    b=y;
}
void add::print()
{
    cout<<"\n"<<"a="<<a<<"\n"<<"b="<<b<<"\n"<<"Sum="<<a+b<<"\n";
}
int main()
{
    add a1;
    a1.print();
    add a2(10);
    a2.print();
    add a3(10,20);
    a3.print();

    return 0;
}

Output

a=2
b=5
Sum=7

a=10
b=0
Sum=10

a=10
b=20
Sum=30

Copy Constructor


It is used to define constructor function which is used to initialize a new object with the help of object of same class in other words copy constructor is a constructor which take a object of same class as an argument.Copy constructor override the process of copy initilization.

Example:


Copy Code
#include <iostream>
using namespace std;
class add
{
    int a,b;
    public:
    add();
    add(int);
    add(int,int);
    add(add &);  // Declare Copy Constructor
    void print()
    {
        cout<<"\n"<<"a="<<a<<"b="<<b<<"\n";
    }
};
add::add(add &x)  // Definition of Copy Constructor
    {
        a=x.a;
        b=x.b;
    }
add::add()
{
    a=0;
    b=0;
}
add::add(int s)
{
    a=s;
    b=s;
}
add::add(int p,int q)
{
    a=p;
    b=q;
}

int main()
{
   add A1;
   add A2(10);
   add A4(10,20);
   add A5=A4; //Call copy constructor implicitly 
   A5.print();
   add A6=A1;  //Call copy constructor implicitly 
   A6.print();
   add A7=A2;  //Call copy constructor implicitly 
   A7.print();
   
    return 0;
}

Output

a=10 b=20
a=0 b=0
a=10 b=10

Destructor


A destructor is used to destroy the object that have been created by the constructor Like a constructor the destructor is a member function whose name is the same as the class name but is preceded by a tilde (~)
A destructor never takes any argument nor does it return any value.It will be called by compiler to cleanup strong that are no longer accessible. It is good practice to declare destructor in a program since it release memory space for future use.

Example:

~Add()
{
cout<<"Iam Destructor";
}

Properties of OOPs | C++

Properties and characteristics of OOPs

Obect Oriented Programming is a Programming methodology which implement the concept of modules with essential properties. Modules means the idea of structured programming. Module encourage the decomposition of any problem in possible sub problems.The language which follow the concept of OOPs is known as Object Oriented Programming Language such as C++,C#,Java etc.

Properties or Characteristics of OOPs

Let we discuss about the properties of Object Oriented Programming Language such as:

  1. Data Abstraction
  2. Encapculation
  3. Inheritance
  4. Polymorphism

Data Abstraction

It is a concept by which can represent any object by its essential fearure only without getting inner details.
In real world we represent any person by its name , instead of giving all information about him.

Encapculation

Process of wrapping of data and function or procedure into single unit is known as encapsulation.
So data encapsulation, we can protect (insulate) data from direct access by any external functionthat is known as data hiding.In prcedural programming accedental use of data was really a big problem that is solved by using the concept of data abstraction and encapsulation.

Inheritance

Inheritance means to acquire the properties from others.In real life we commonly use the concept of classification hierarchy.OOPs supports classification concept in the form of inheritance.
"Inheritance is a process by which object of one class can acquaire the properties or attributes of another class"
The object by which properties are accessed form base class and received by derived class.

Polymorphism

"One interface multiple method"
In real life a particular thing can be use in different manner according to different situation or condition for example a person role is different at different place.
Ploymorphism means the ability to take more than one form.In C++ concept of polymorphism is implemented using function overloading.

Computer Fundamentals

Index | Model Papers

Index Tutorial Questions

Index Practicals

Image Processing | Practical



Lab Assignments


Image Processing


  1. To create a program to display grayscale image using read and write operation.
  2. To create a vision program to find histogram value and display histograph of a grayscale and colour image.
  3. To create a vision program for Nonlinear Filtering technique using edge detection
  4. To create a program to discretize an image using Fourier transformation.
  5. To create a program to eliminate the high frequency components of an image.
  6. To create a colour image and perform read and write operation.
  7. To obtain the R, B, G colour values and resolved colour values from a colour box by choosing any colour.
  8. To create a program performs discrete wavelet transform on image.
  9. To create a program for segmentation of an image using watershed transforms.
  10. To create a vision program to determine the edge detection of an image using different operators.

Image Processing and Computer Vision | Practical



Lab Assignments


Image Processing and Computer Vision


  1. Write a Program to change the Brightness of Image.
  2. Write a Program to Flip the image around the vertical and horizontal line.
  3. Write a Program to display the colour components of the image Red Green Blue Components of Image.
  4. Write a Program to find the negative of an image.
  5. Write a Program for Edge detection with gradient and convolution of an Image.
  6. Write a Program to find threshold of grayscale image.
  7. Write a Program to estimate and subtract the background of an image.
  8. Write a Program to find threshold of RGB image.
  9. Write a program to create a vision program to find histogram value and display histograph of a grayscale and colour image.
  10. Write a program to create a colour image and perform read and write operation.

Block Chain Technologies | Practical



Lab Assignments


Block Chain Technologies


  1. Explain the process of Bitcoin mining; on what type of block chain it is it based and how it works.
  2. Suggest which type of block chain should be used for the security of donations in a charity organization. What benefits does the block chain technology introduce in such a scenario? Explain your answer using an example.
  3. Discuss the difference and similarities between an Ethereum Smart Contract and a traditional contract.
  4. What is the difference between Bitcoin block chain and Ethereum block chain?
  5. Which cryptographic algorithm is used in Block chain?
  6. What do you mean by Coin base transaction?
  7. What is encryption? What is its role in Block chain?
  8. What are Merkle trees? How important are Merkle trees in Block chains?
  9. Explain the components of Block chain Ecosystem?
  10. Name some popular platforms for developing block chain applications.