MATRIX USING EXCEPTION HANDELING

Question – WAP TO MULTIPLY TWO MATRIX, GET ROW AND COLUMN FROM USER FOR BOTH MATRIX. IF DIMENSION OF MATRIX ARE NOT SAME THEN RAISE EXCEPTION AND TERMINATE PROGRAM –

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    try
    {
        int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j;
        line:
            cout<<"Enter number of rows of A: ";
        cin>>m;
        cout<<"Enter number of coloumns of A: ";
        cin>>n;
        cout<<endl<<"Enter elements of matrix A: "<<endl;
        for(i=0;i<m;i++)
           {
            for(j=0;j<n;j++)
            {
                cout<<"Enter element a"<<i+1<<j+1<<": ";
                cin>>a[i][j];
            }
        }
        cout<<endl<<"Enter number of rows of B: ";
        cin>>o;
        cout<<"Enter number of coloumns of B: ";
        cin>>p;
        cout<<endl<<"Enter elements of matrix B: "<<endl;
        for(i=0;i<o;i++)
        {
            for(j=0;j<p;j++)
            {
                cout<<"Enter element b"<<i+1<<j+1<<": ";
                cin>>b[i][j];
            }
        }
        cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                cout<<a[i][j]<<" ";
            }
            cout<<endl<<endl;
           }
           cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
        for(i=0;i<o;i++)
        {
            for(j=0;j<p;j++)
            {
                cout<<b[i][j]<<" ";
            }
               cout<<endl<<endl;
           }
           if(m!=p || n!=o)
                   throw m;
           for(i=0;i<m;i++)
           {
              for(j=0;j<p;j++)
            {
                c[i][j]=0;
                 for(int k=0;k<n;k++)
                 {
                    c[i][j]=c[i][j]+a[i][k]*b[k][j];
                 }
              }
        }
        cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl;
        for(i=0;i<m;i++)
        {
            for(j=0;j<p;j++)
            {
                cout<<c[i][j]<<" ";
             }
            cout<<endl<<endl;
         }
      }
      catch(int x)
      {
          cout<<"\n\nMULTIPLICATION NOT POSSIBLE : DIMENSION OF MATRICES ARE NOT SAME";
    }
    cout<<"\n\nProgramming @C#ODE STUDIO";
    getch();
}

mul_mat_1

EXCEPTION RAISED

mul_mat_2

STACKS (USING TEMPLATES AND EXCEPTION H.)

We have posted a program on stacks operation which is done by normal class and function methood. Here is the generic function to make it more useful with different types of data. Go through Program ->

#include"conio.h"
#include"iostream"
using namespace std;
template<class t>
class stack
{
    private:
        t sta[50];
        int siz;
    public:
        stack()
        {
            siz=0;
        }
        t push();
        t pop();
        t display();
        
};
template<class t>
t stack<t>::push() // push fuction
{
    t num;
    if(siz==4)
    {
        cout<<"\nStack is already full.";
        return 0;
    }
    else
    {
        cout<<"\nEnter element : ";
        cin>>num;
        sta[siz]=num;
        siz++;
    }
    return 0;
}
template<class t>
t stack<t>::pop() //pop function;
{
    t num1;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else
    {
        num1=sta[siz-1];
        cout<<"\nElement popped : "<<num1;
        siz--;
    }
    return num1;
}
//c#ode studio appreciates this program;
template<class t>
t stack<t>::display() //display function;
{
    int i,j;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else 
    {
        for(i=siz-1,j=0;i>=0;i--,j++)
        {
            cout<<"\nELEMENT <"<<j<<"> IS : "<<sta[i];
        }
    }
}
int main()
{
    int choice=0;
    cout<<"\nNEED OPERATION WITH INTEGERS OR FLOAT( 1:INTEGER , 2:FLOAT ) : ";
    cin>>choice;
    try
    {
        if(choice==1)
        {
            stack <int> obj;
            int a;
            cout<<"\n_________STACK FUNCTIONS_________ "<<endl;
            int option=1;
            while(option)
            {
                cout<<"\n1:PUSH \n2:POP \n3:DISPLAY ";
                cout<<"\nEnter Choice : ";
                cin>>a;
                switch(a)
                {
                    case 1:
                        obj.push();
                        break;
                    case 2:
                        obj.pop();
                        break;
                    case 3:
                        obj.display();
                        break;
                    default:
                        cout<<"\nEnter correct choice... 404 ERROR <CHOICE NOT FOUND>";
                        return 0;
                }
                cout<<"\n1: Continue, 0:Exit :: ";
                cin>>option;
            }
        }
        else
            if(choice==2)
            {
                stack <float> obj;
                int a;
                cout<<"\n_________STACK FUNCTIONS_________ "<<endl;
                int option=1;
                while(option)
                {
                    cout<<"\n1:PUSH \n2:POP \n3:DISPLAY ";
                    cout<<"\nEnter Choice : ";
                    cin>>a;
                    switch(a)
                    {
                        case 1:
                            obj.push();
                            break;
                        case 2:
                            obj.pop();
                            break;
                        case 3:
                            obj.display();
                            break;
                        default:
                            cout<<"\nERROR 404-CHOICE DOESN'T EXISTS....TERMINATING PROGRAM";
                            return 0;
                    }
                    cout<<"\n1: Continue, 0:Exit :: ";
                    cin>>option;
                }
            }
            else
                throw choice;
    }
    catch(int x)
    {
        cout<<"\n\nERROR 404-CHOICE "<<x<<" DOESN'T EXISTS....TERMINATING PROGRAM";
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}


STACK_EXP

Object Slicing;

/*Size of base class is not changed after the  assignment although derived class object is assigned to it…  that means only the base class part is copied to base class  object and derived class object is ignored.*/

DEFINITION – When a Derived Class object is assigned to Base class, the base class’ contents in the derived object are copied to the base class leaving behind the derived class specific contents. This is referred as Object Slicing.

#include"conio.h"
#include"iostream"
using namespace std;
class base
{
    public:
        int a;
        base()
        {
            a = 10;
        }
};
class derive: public base
{
    public:
        int b;
        derive()
        {
            b=20;
        }
};

int main()
{
    base b;
    derive d;
    cout<< "Size of base class is "<<sizeof(b)<<endl;
    cout<< "Size of derive class is  "<<sizeof(d)<<endl;
    b=d;
    cout<< "Size of b class is "<<sizeof(b)<<endl;
    cout<<"\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}

 

obj_slice

iostream operators (<>) Overloading;

Question – Demonstrate the overloading of istream keyword cin >> and ostream keyword cout<< :

Answer :- Here in the program we use to make operator function as a friend of a class because it will be called without creating a object;

 

#include<iostream>
#include<conio.h>
using namespace std;
class inc
{
    private:
        int count ;
    public:
        inc(int x)
        {
            count=x;
        }
        inc()
        {
            count=0;
        }
        friend istream & operator >>(istream &ip, inc &i)
        {
            ip>>i.count;
            return ip;
        }
        friend ostream & operator <<(ostream &op, inc &j)
        {
            op<<j.count;
            return op;
        }
};
int main()
{
    inc obj1(8),obj2,obj3;
    cout<<"\nENTER THE VALUE IN OBJ3 : ";
    cin >> obj3;
    cout<<"\nVALUE IN OBJ1 : ";
    cout << obj1;
    cout<<"\nVALUE IN OBJ2 : ";
    cout<< obj2;
    cout<<"\nVALUE IN OBJ3 : ";
    cout << obj3;
    cout<<"\n\nPROGRAMMING @C#ODE STUDIO";
    getch();
}

 

op_ovr2

CONSTRUCTOR OVERLOADING

QUESTION- DEMONSTRATE A PROGRAM TO MAKE THE CONCEPT CLEAR OF CONSTRUCTOR OVERLOADING

ANSWER-

 

#include<conio.h>
#include <iostream>
using namespace std;
class Ractangle 
{
    public:
        Ractangle(int j=1):i(j) //constructor to Ractangle class
        {
            cout<<"\nRACTANCLE CLASS's 'i' = "<<i;
        }
    private:
        int i;
};

class Square
{
    public:
        Square(int j=2):i(j) //constructor to square class;
        {
            cout<<"\nSQUARE CLASS's 'i' = "<<i;
        }
        operator Ractangle() //constructor of ractangle class is overloaded
        { 
            Ractangle obj1;
            cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
        }
    private:
            int i;
};

int main()
{
    Ractangle obj = Square(); //overloaded constructor applied on the object of square
    getch();
}

 

CON_OVR

Revolving Ambiguity;

Revolving Ambiguity – This concept is use to overcome the properties of function overriding. By using syntax as <object to derive> . <class name> :: <function name> By using scope resolution operator

 

#include<iostream.h>
#include<conio.h>
using namespace std;
class base
{
    int day,mon,yr;
    public: 
        void getb(int dd, int mm, int yy)
        {
            day=dd;
            mon=mm;
            yr=yy;
        }
        void disp()
        {
            cout<<"\nDay= "<<day
                <<"\tMonth= "<<mon
                <<"\tYear= "<<yr;
        }
};
class derive: public base //derive class to base
{
    int hr,min;
    public:
        void getd(int h,int m, int d, int mo, int y)
        {
            getb(d,mo,y);
            hr=h;
            min=m;
        }
        void disp()
        {
            cout<<"\nHours= "<<hr<<"\tMinutes= "<<min;
            
        }
};
int main()
{
    derive sd;
    sd.getd(10,58,22,10,2013);
    sd.derive::disp();
    sd.base::disp(); //revolving ambiguity 
    cout<<"\n\nPROGRAMMING @C#ODE STUDIO";
    getch();
}

 

revolv

Aggregation:

Aggregation – Here when we make two or more classes, and initialize the object of one class in another class and take the input through the 2nd class. when we delete the object of 2nd class it doesnt affect the object of 1st class which is initialized in 2nd class :

program-

#include<iostream.h>

#include<conio.h>

#include<string.h>

using namespace std;

class date

{

int day,mon,year;

public:

date(int dd, int mm, int yy)

{

day=dd;

mon=mm;

year=yy;

}

void display()

{

cout<<“\n–DATE OF BIRTH–“;

cout<<“\nDay : “<<day

<<“\tMonth : “<<mon

<<“\tYear : “<<year;

}

};

class student

{

int rn;

char name[25];

date dob;

public:

student(int r, char n[], int d, int m, int y):dob(d,m,y)

{

rn=r;

strcpy(name,n);

}

void display()

{

cout<<“\nRoll No. : “<<rn

<<“\nName : “<<name;

dob.display();

}

};

int main()

{

int rl,da,mo,ye;

char nm[10];

cout<<“\nNAME : “;

cin>>nm;

cout<<“\nROLL NO.: “;

cin>>rl;

cout<<“\nDAY : “;

cin>>da;

cout<<“\nMONTH : “;

cin>>mo;

cout<<“\nYEAR : “;

cin>>ye;

student *obj1=new student(rl,nm,da,mo,ye);

obj1->display();

cout<<“\n\n–WHEN OBJECT OF BASE CLASS IS DELETED–\n\n”;

delete obj1; //deleting object of class doesnt affect the object of date class;

obj1->display(); //it gives garbage value only for variables initi. in student class;

cout<<“\n\nPROGRAMMING @ C#ODE STUDIO “;

getch();

}

AGGR