Showing posts with label Madan Mohan Malaviya University of Technology. Show all posts
Showing posts with label Madan Mohan Malaviya University of Technology. Show all posts

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();

}


Program to catch all type of Exceptions in C++

 #include <iostream>

using namespace std;

#include <string>

int main()

{

      string Name = "Vaibhav_Yadav", R_Name;

      int Roll = 2021071042, roll_no;

      cout<<"Authentication Please Verify Yourself"<<endl;

      try{

            cout<<"Enter Your Name: ";

            getline(cin, R_Name);

            cout<<"Enter Your Roll No: ";

            cin>>roll_no;

            if(R_Name == Name && Roll == roll_no){

                  cout<<"Login Success"<<endl;

            }else{

                  throw 401;

            }

      }

      catch(...){

            cout<<"Authentication failed"<<endl;

            cout<<"Access Not Allowed"<<endl;

      }


    return 0;

}


Input file Writing and Reading in C++

 #include <iostream>

#include  <bits/stdc++.h>

#include <fstream>

#include <string>

using namespace std;


int main()

{

      while(1){

            int ch;

      cout<<endl;

      cout<<"Enter Your Choice: "<<endl;

      cout<<"1. Write Into file"<<endl;

      cout<<"2. Read From The file"<<endl;

      cin>>ch;

      switch(ch){

            case 1:{

            string Inputline;

            cout<<"Enter What you want to insert to file"<<endl;

            cout<<"Default Input will be 'This is Vaibhav Yadav and today I'm learning file handling in c++ with fstream'"<<endl;

            // getline(cin, Inputline);

            Inputline = "This is Vaibhav Yadav and today I'm learning file handling in c++ with fstream";

            ofstream W_myfile("Name.txt");

            W_myfile<<Inputline;

            W_myfile.close();

            }

            break;

            

            case 2:    

            {

            string NameTxt;

            ifstream R_myfile("Name.txt");

            while(getline(R_myfile, NameTxt)){

            cout<<NameTxt;

            }

            R_myfile.close();

            }

            break;

            

            default:

            cout<<"Wrong Input Yrr :("<<endl;

            

      }

      }

    return 0;

}


Class Constructor and Destructor for this Class

 #include<iostream>

using namespace std;

class Student

{

string name;

public:

Student(){

    name = "Vaibhav Yadav";

    cout<<"Constructor of Student Class has been Called"<<endl;

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

}

~Student(){

    cout<<"---Destructor Of Student Class has been called---"<<endl;

    cout<<"---Memory for 'obj name' has been deallocated and memory has been released---"<<endl;

}

};


main()

{

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...