Create a class “CString” to represent a string to concatenate and compare two strings also add two matrix(3x3) using Operator Overloading.

 

 Create a class “CString” to represent a string (Hint: Check Book Chapter Examples)   
    a) Overload the + operator to concatenate two strings.
    b) = = to compare 2 strings.

Answer:

#include "stdafx.h"

#include"iostream"

#include"conio.h"

#include"stdio.h"

#include"string.h"

 

using namespace std;

class CString

{

private:

       char s[20];

public:

       void input()

       {

       cout << "Enter String......" << endl;

       cin.getline(s, 20);

       }

       void output()

       {

              cout << "String is :"<<s << endl;

       }

       CString operator +(CString s0)

       {

              CString temp;

              strcpy(temp.s,s);

              strcat(temp.s, s0.s);

              return temp;

       }

       int operator ==(CString s3)

       {

              if (strlen(s3.s) == strlen(s))

              {

                     return 1;

              }

else

{

       return 0;

}

       }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

       CString s1, s2,s3;

       cout << "Enter 1st String....." << endl;

       s1.input();

       cout << "1st String....." << endl;

       s1.output();

       cout << "Enter 2nd String....." << endl;

       s2.input();

       cout << "2nd String....." << endl;

       s2.output();

       cout << "3rd String....." << endl;

       cout << "s3=....." << endl;

       cout << "After Contatenation...." << endl;

       s3 = s1 + s2;

       cout << "s3 = ";

       s3.output();

       if (s1 == s2)

       {

              cout << "Both string are of same lenght " << endl;

       }

else

{

       cout << "Both strings are not of same lenght " << endl;

}

       _getch();

       return 0;

}


Create Two Objects m1 and m2 , Fill both objects matrices and display matrices then Find out m3=m1+m2 (use operator overloading).

Answer:

#include "stdafx.h"


#include"iostream"

#include"conio.h"

 

using namespace std;

class Matrix

{

private:

       int m[3][3];

public:

       void input()

       {

cout << "Enter values in matrix 3x3 :" <<endl;

       for (int i = 0; i < 3; i++)

              {

              for (int j = 0; j < 3; j++)

                     {

                           cin >> m[i][j];

                     }

              }

       }

       void output()

       {

       cout<<"The values in matrix 3x3 :"<<endl;

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

cout << "value in the matrix index [" << i << "][" << j << "]= " << m[i][j] << endl;

                     }

              }

       }

       void operator +(Matrix c)

       {

              Matrix temp;

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

              temp.m[i][j] = m[i][j] + c.m[i][j];

                     }

              }

       cout << "After Addition Matrix 3 is" << endl;

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

cout << "value in the matrix index [" << i << "]["

<< j << "]= " << temp.m[i][j] << endl;

 

                     }

              }

       }

       };

 

       int _tmain(int argc, _TCHAR* argv[])

       {

              Matrix a, b;

              cout << "Enter values in Matrix 1 " << endl;

              a.input();

              cout << "Enter values for Matrix 2 " << endl;

              b.input();

              cout << "Matrix 1 " << endl;


              a.output();

              cout << "Matrix 2 " << endl;

              b.output();

              a + b;

 

 

              _getch();

              return 0;

       }


THANK YOU!!!!!.......


Comments

Popular posts from this blog

Oop(Object Oriented Programming) Project for final year students

Linklist all concepts using Dynamic arrays(using nodes)

Write a C++ program to implement a suitable data structures (using array) of marble plates