Operator Overloading in C++

#include<iostream>

using namespace std;


class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i = 0){

      real = r; imag = i;

}

Complex operator - (Complex const &obj) {

Complex res;

res.real = real - obj.real;

res.imag = imag - obj.imag;

return res;

}

Complex operator + (Complex const &obj) {

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

}

Complex operator * (Complex const &obj) {

Complex res;

res.real = real * obj.real;

res.imag = imag * obj.imag;

return res;

}

Complex operator / (Complex const &obj) {

Complex res;

res.real = real / obj.real;

res.imag = imag / obj.imag;

return res;

}

void print() {

      cout <<"Result is: "<< real << " + i" << imag << '\n';

}

};


int main()

{

Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;

c3.print();

Complex c4 = c1 - c2;

c4.print();

Complex c5 = c1 * c2;

c5.print();

Complex c6 = c1 / c2;

c6.print();

}


Function overriding in C++ (basics)

 #include <iostream>

#include <string>

using namespace std;

class student{

      public:

      void displaystu(){

            cout<<"Hi! My name is Vaibhav Yadav and I'm from ECE Department, First Year' 25"<<endl;

      }

};

class update: public student{

      public:

      void displaystu(){

            student::displaystu();

            cout<<"Details UPDATED"<<endl<<"Hi! My name is Vaibhav Yadav and I'm from 'IT' Department, First Year' 25"<<endl;

      }

};

int main()

{

    update up;

    up.displaystu();

    return 0;

}


Program for different levels inheritance is given here

 #include <iostream>

#include <string>

using namespace std;

class College{

      public:

      int Roll_no;

      string Name, Department;

      College(){

            cout<<"This is MMMUT Gorakhpur"<<endl;

      }

};

class student:public College{

      public:

      student(int R, string n, string d){

            Roll_no = R;

            Name = n;

            Department = d;

      }

      void display(){

            cout<<"Student Name is: "<<Name<<endl<<"Roll No: "<<Roll_no<<endl<<"Department: "<<Department<<endl;

      }

      

};





int main()

{

    student stu1(2021071042, "Vaibhav Yadav", "IT");

    stu1.display();

    student stu2(2021071060, "Ayush Verma", "IT");

    stu2.display();

    student stu3(2021071052, "Anurag Kumar Singh", "IT");

    stu3.display();

//     student stu;

    return 0;

}


Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...