TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Showing posts with label PROGRAMMING LANGUAGE. Show all posts
Showing posts with label PROGRAMMING LANGUAGE. Show all posts

Monday, 14 September 2020

September 14, 2020

Overloading New And Delete Operator In C++ | Overloading New And Delete Operator In C++ Example | Overloading New And Delete Operator In C++ In Hindi

 

Overloading New And Delete Operator In C++

The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes.


Syntax for overloading the new operator :

void* operator new(size_t size);


Syntax for overloading the delete operator :

void operator delete(void*);


image of Overloading New And Delete Operator In C++

Fig:Overloading New And Delete Operator In C++


Program:

#include<iostream>

#include<stdlib.h>

class student

{

string name;

int age;

public:

student()

{

cout<< "Constructor is called\n" ;

}

student(string name, int age)

{

          this->name = name;

          this->age = age;

          }

          void display()

          {

                   cout<< "Name:" << name << endl;

                   cout<< "Age:" << age << endl;

          }

          void * operator new(size_t size)

          {

                   cout<< "Overloading new operator with size: " << size << endl;

                   void * p = ::new student();

                   return p;

          }

          void operator delete(void * p)

          {

                   cout<< "Overloading delete operator " << endl;

                   free(p);

          }

};

int main()

{

          student * p = new student("Pramod", 22);

          p->display();

          delete p;

}


Output:

Overloading new operator  with size:16

Constructor is called

Name:Pramod

Age:21

Overloading delete operator

Sunday, 30 August 2020

August 30, 2020

Operator Overloading Comma In C++ | Example Of Operator Overloading Comma In C++| Operator Overloading Comma In C++ In Hindi

 

Operator Overloading Comma In C++

In C++, the operator access to a member of an object -> can be overloaded. If the operator -> is overloaded, the call of the class.


image of Operator Overloading Comma In C++

Fig:Operator Overloading Comma In C++


Program:

#include <iostream>

class Double

{

public:

    double d;

   

    Double* operator->()

    {

        return this;    }

};

void main()

{

        Double D;

    double x;

    D.d = 3.85;

    x = D->d;

    x = D.d;

    cout << "x = " << x;

}

 

Output:

x-3.85

August 30, 2020

Overloading Operator Using A Friend Function In C++|Overloading Operator Using A Friend Function In C++ In Hindi

 

Overloading Operator Using A Friend Function In C++

The operator overloading function must precede with friend keyword, and declare a function class scope. Keeping in mind, friend operator function takes two parameters in a binary operator, varies one parameter in a unary operator.

image of Overloading Operator Using A Friend Function In C++
Fig:Overloading Operator Using A Friend Function In C++


Program:

#inlcude<iostream.h>

#include<conio.h>

class bca

{

public:

int i,j;

bca()

{

this->i=0;

this->j=0;

}

bca(int n,int m)

{

this->i=n;

this->j=m;

}

friend bca operator+(bca&,bca&);

};

bca operator+(bca & d1, bca& d2)

{

bca d3;

d3.i=d1.i+d2.i;

d3.j=d1.j+d3.j;

return d3;

}

void main()

{

bca d1(8,9);

bca d2(10,2);

bca d3;

d3=d1+d2;

cout<<”/n total number of i and j<<”d3.i<<”  “<<d3.j;

getch();

}


Output:

Total number of I and j 18’11

 

 

August 30, 2020

Assignment Operators Overloading in C++|Assignment Operators Overloading in C++ Example|Assignment Operators Overloading in C++ In Hindi

 Assignment Operators Overloading in C++

Assignment Operator is Used to assign value to an variable. Assignment operator is denoted by equal to sign.  Assignment operator have Two values L-Value and R-value. Operator copies R-Value into L-Value. It is a binary operator.  C++ Overloading assignment operator can be done in object oriented programming. Assignment operator must be overloaded by a non-static member function only. If the overloading function for the assignment operator is not written in the class, the compiler generates the function to overload the assignment operator.

image of Assignment Operators Overloading in C++
Fig:Assignment Operators Overloading in C++

Syntax:

Return_type operator=(const class_name,&)

 

Program:

#include<iostream.h>

#include<conio.h>

class Distance

{

private:

int feet;

int inches;

public:

Distance()

{

feet=0;

inches=0;

}

Distance(int f,int i)

{

feet=f;

inches=i;

 

 }

void operator=(const Distance &D)

{

feet=D.feet;

inches=D.inches;

}

void displayDistance()

{

cout<<”F: “<<feet<< “ I:”<<inches<<endl;

}

};

void main()

{

Distance D1(11,10),  D2(5,11);

cout<<”first distance”;

D1.displayDistance();

cout<<”second distance”;

D2.displayDistance();

D1=D2;

cout<<”first distance”;

D1.displayDistance();

getch();

}


Output:

First Distance: F:11 I: 10

Second Distance: F:5 I:11

First Distance:F:5 I:11

August 30, 2020

Types of Operator Overloading in C++| Different Types of Operator Overloading in C++| Types of Operator Overloading in C++ In Hindi

 

Types of Operator Overloading in C++

1.overloading unary operator

2.overloading binary operator

 

image of Types of Operator Overloading in C++

Fig:Types of Operator Overloading in C++ 


1.Overloading unary operator

 In unary operator function, no arguments should be passed. It works only with one class objects. It is a overloading of an operator operating on a single operand.


Program:

#include <iostream>

#include<conio.h>

 class base

 {

public:

int a,b;

base(int f, int i)

{

this->a = f;

this->b = i;

} 

void operator-()

{

a--;

b--;

cout << "\n a and b value: " << a << "'" << b;

}

};

void main()

{

Base  d1(8, 9);

-d1; 

getch();

}


Output:

a and b value  7’8

 

2.Overloading binary operator

 In binary operator overloading function, there should be one argument to be passed. It is overloading of an operator operating on two operands.


Program:

#include <iostream>

class Distance {

public:  

int feet, inch;       

Distance()

{

this->feet = 0;

this->inch = 0;

}

Distance(int f, int i)

{

this->feet = f;

this->inch = i;

} 

Distance operator+(Distance& d2) // Call by reference

{

Distance d3;

d3.feet = this->feet + d2.feet;

d3.inch = this->inch + d2.inch;  

return d3;

}

};

int main()

{

Distance d1(8, 9);  

Distance d2(10, 2);

Distance d3;  

d3 = d1 + d2;

cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;

getch();

}


Output:

Total Feet & Inches: 18’11 

August 30, 2020

Operator Overloading In C++| Example Of Operator Overloading In C++| Operator Overloading In C++ In Hindi

 

Operator Overloading In C++

Operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.


image of Operator Overloading In C++
Fig:Operator Overloading In C++


Rules of operator overloading

·       In case of a non-static function, the binary operator should have only one argument and unary should not have an argument.

 

·       In the case of a friend function, the binary operator should have only two argument and unary should have only one argument.

 

·       All the class member object should be public if operator overloading is implemented.

 

·       Operators that cannot be overloaded are . .* :: ?:

 

·       Operator cannot be used to overload when declaring that function as friend function = () [] ->.

 

Synatx:

class baseclass

{

//body of base

return type operator++(argument)

{

 

}

return type operator operator name(argument)

{

 

}

};


Program:

#include<iostream.h>

#include<conio.h>

class base

{

Int n;

public:

base()

{

n=0;

}

void operator++()

{

n=n-2;

}

Void operator—()

{

n=n+2;

}

void show()

{

cout<<”n=”<<n;

}

};

void main()

{

base obj;

obj.show();

++obj;

obj.show();

++obj;

obj.show();

--obj;

obj.show();

--obj;

obj.show();

getch();

}


Output:

n=0

n=-2

n=-4

n=-2

n=0