Vous êtes sur la page 1sur 71

EXPERIMENT 1

1. Write a function using reference variables as arguments to swap the values of pair of integers

Program Code :
#include <iostream>

using namespace std;

void swap(int&x,int&y)

int z=x;

x=y;

y=z;

int main()

intx,y;

cout<<"Enter two numbers : ";

cin>>x>>y;

cout<<"\nOriginal values are :\nx = "<<x<<" y = "<<y;

swap(x,y);

cout<<"\n\nSwapped values are :\nx = "<<x<<" y = "<<y;

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Write an inline function to find largest of three numbers

Program Code :
#include <iostream>

using namespace std;

inlineint max(inta,intb,int c)

return a>b&&a>c?a:b>a&&b>c?b:c;

int main()

inta,b,c;

cout<<"Enter 3 numbers : ";

cin>>a>>b>>c;

cout<<"\nLargest of "<<a<<", "<<b<<" and "<<c<<" is : "<<max(a,b,c);

Output :

ASHISH KUMAR SALVI 0827CS183D02


3. Write a function power() to raise a number m to a power n. The function takes a
double value for m and int value for n and returns the result correctly. Use a default
value of 2 for n to make the function to calculate squares when this argument is
omitted. Write a main that gets the values of m and n from the user to test the
function.

Program Code :
#include <iostream>

using namespace std;

double pow(double m,int n=2)

double p=1.0;

inti;

for(i=1;i<=n;i++)

p*=m;

return p;

int main()

double m;

intn,ch;

cout<<"!! Power Function Menu m^n !!\n1) Only value of m\n";

cout<<"2) Value for both m and n";

cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

{
ASHISH KUMAR SALVI 0827CS183D02
case 1 : cout<<"\nEnter value of m : ";

cin>>m;

cout<<"\nPower function when default argument is used = "<<pow(m);

break;

case 2 : cout<<"Enter value of m and n : ";

cin>>m>>n;

cout<<"\nPower function when both arguments are used = "<<pow(m,n);

break;

default :cout<<"\nExit";

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 2
1. Define a class to represent a bank account which include the following members
as-
Data members :
1. Name of the depositor
2. Account Number
3. Withdraw amount
4. Balance amount in the account
Member Functions:
1. To assign initial values
2. To deposit an amount
3. To withdraw an amount after checking the balance
4. To display name and balance.
Write a main program to test the program.

Program Code :
#include <iostream>

#include <string>

using namespace std;

class Account

string name;

longacno;

stringactype;

doublebal;

public:

Account()

name="nill";

actype="nill";

acno=0;

bal=0.0;
ASHISH KUMAR SALVI 0827CS183D02
}

voidopbal()

cin.ignore();

cout<<"\nEnter name : ";

getline(cin,name);

cout<<"\nEnter A/c type : ";

cin>>actype;

cout<<"\nEnter A/c no. : ";

cin>>acno;

cout<<"\nEnter opening balance : ";

cin>>bal;

void deposit()

floatdepo=0.0;

cout<<"\nEnter deposit amount : ";

cin>>depo;

bal+=depo;

cout<<"\nBalance after deposit : "<<bal;

void withdraw()

float with=0.0;

cout<<"\nEnter Withdraw amount : ";

cin>>with;

if(bal<with)

ASHISH KUMAR SALVI 0827CS183D02


cout<<"\n!! Not enough balance !!";

else

bal-=with;

cout<<"\nBalance after withdraw : "<<bal;

void display()

cout<<"\n\tDETAILS\n"<<"Name : "<<name;

cout<<"\nAccount No. : "<<acno<<"\nAccountType : "<<actype;

cout<<"\nBalance : "<<bal;

};

int main()

Account obj;

intch;

do

cout<<"\n\tChoice list\n1. Open Account\n2. To Deposit\n";

cout<<"3. To Withdraw\n4. To display all details\n5. Exit\n";

cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

case 1 : obj.opbal();

ASHISH KUMAR SALVI 0827CS183D02


break;

case 2 : obj.deposit();

break;

case 3 : obj.withdraw();

break;

case 4 : obj.display();

break;

case 5 : cout<<"\nExit";

default :cout<<"\n!! Enter valid choice !!";

}while(ch!=5);

Output :

ASHISH KUMAR SALVI 0827CS183D02


ASHISH KUMAR SALVI 0827CS183D02
2.Write the above program for handling n number of account holders using array of
objects for data initialize and displaying all records.

Program Code :
#include <iostream>

#include <string>

using namespace std;

class Account

string name;

longacno;

stringactype;

doublebal;

public:

Account()

name="nill";

actype="nill";

acno=0;

bal=0.0;

voidopbal()

cin.ignore();

cout<<"\nEnter name : ";

getline(cin,name);

cout<<"Enter A/c type : ";

ASHISH KUMAR SALVI 0827CS183D02


cin>>actype;

cout<<"Enter A/c no. : ";

cin>>acno;

cout<<"Enter opening balance : ";

cin>>bal;

void display()

cout<<"\n\tDETAILS\n"<<"Name : "<<name;

cout<<"\nAccount No. : "<<acno<<"\nAccountType : "<<actype;

cout<<"\nBalance : "<<bal;

};

int main()

{ intn,i;

cout<<"\nEnter number of records : ";

cin>>n;

Account obj[n];

for(i=0;i<n;i++)

{ cout<<"\nRecord "<<i+1;

obj[i].opbal();

for(i=0;i<n;i++)

{ cout<<"\nRecord "<<i+1;

obj[i].display();

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


3. Write a C++ program to compute area of right angle triangle, equilateral
triangle ,scalene triangle using function overloading concept.

Program Code :
#include <iostream>

#include <cmath>

using namespace std;

float area(inta,int b)

return (0.5)*a*b;

float area(int a)

returnsqrt(3)/4*a*a;

float area(inta,intb,int c)

float s=(float)(a+b+c)/2;

returnsqrt(s*(s-a)*(s-b)*(s-c));

int main()

intch;

cout<<"\tArea for\n1. Right-angled Triangle";

cout<<"\n2. Equilateral Triangle\n3. Scalene Triangle\n";

cout<<"\nEnter your choice : ";

cin>>ch;

ASHISH KUMAR SALVI 0827CS183D02


switch(ch)

case 1 : intl,h;

cout<<"\nEnter length and height : ";

cin>>l>>h;

cout<<"\n\nArea of Right-angled Triangle is : ";

cout<<area(l,h);

break;

case 2 : int s;

cout<<"\nEnter length of side : ";

cin>>s;

cout<<"\n\nArea of Equilateral Triangle is : ";

cout<<area(s);

break;

case 3 : inta,b,c;

cout<<"\nEnter length of the sides : ";

cin>>a>>b>>c;

cout<<"\n\nArea of Scalene Triangle is : ";

cout<<area(a,b,c);

break;

default :cout<<"\n!! Wrong choice !!";

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 3
1. Write a C++ program to swap the values two integer members of different
classes using friend function

Program Code :
#include <iostream>

using namespace std;

class B;

class A

int a;

public:

A()

cout<<"Enter value of a : ";

cin>>a;

intgetdata()

return a;

friend void swap(A &x,B&y);

};

class B

int b;

public:

ASHISH KUMAR SALVI 0827CS183D02


B()

cout<<"Enter value of b : ";

cin>>b;

intgetdata()

return b;

friend void swap(A &x,B&y);

};

void swap(A &x,B&y)

int temp=x.a;

x.a=y.b;

y.b=temp;

int main()

Aaobj;

B bobj;

cout<<"\nOriginal values-\na = "<<aobj.getdata()<<" b = "<<bobj.getdata();

swap(aobj,bobj);

cout<<"\n\nSwapped values-\na = "<<aobj.getdata()<<" b = "<<bobj.getdata();

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


2.Write a C++ program for addition of two complex numbers using friend
function (use constructor function to initialize data members of complex
class)

Program Code :
#include <iostream>

using namespace std;

class Complex

int real;

intimg;

public:

Complex()

real=0;

img=0;

voidgetdata(intx,int y)

real=x;

img=y;

void show()

cout<<real<<" + i"<<img;

friend void sum(Complex,Complex);

};

ASHISH KUMAR SALVI 0827CS183D02


void sum(Complex a,Complex b)

Complex c;

c.real=a.real+b.real;

c.img=a.img+b.img;

c.show();

int main()

Complex a,b,c;

intx,y;

cout<<"Enter real and imaginary part of complex number a : ";

cin>>x>>y;

a.getdata(x,y);

cout<<"\nEnter real and imaginary part of complex number b : ";

cin>>x>>y;

b.getdata(x,y);

cout<<"a = ";

a.show();

cout<<"\nb = ";

b.show();

cout<<"\n\na+b = ";

sum(a,b);

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 4
1. Define a class string and overload == to compare two strings and + operator for
concatenation two strings

Program Code :
#include<iostream>

#include<string.h>

using namespace std;

class String

public:

char *s;

intlen;

String()

s=0;

len=0;

String(char *s1)

len=strlen(s1);

s=new char[len+1];

strcpy(s,s1);

void show()

cout<<s;

ASHISH KUMAR SALVI 0827CS183D02


}

void operator==(String x)

cout<<"calling to overload opeartor =="<<endl;

if(strcmp(x.s,s)==0)

cout<<"Strings are equal"<<endl;

else

cout<<"strings are not equal"<<endl;

String operator+(String y)

cout<<"\ncalling to operator +"<<endl;

String z;

z.len=len+y.len;

z.s=new char[z.len+1];

strcpy(z.s,s);

strcat(z.s,y.s);

return z;

};

int main()

char s1[100],s2[100];

cout<<" Set the value of string through parameterized constructor"<<endl;

cout<<"P=";

cin>>s1;

cout<<endl;

ASHISH KUMAR SALVI 0827CS183D02


cout<<"Q=";

cin>>s2;

cout<<endl;

String obj1(s1);

String obj2(s2);

String obj3;

obj1==obj2;

obj3=obj1+obj2;

obj3.show();

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Write a program for overloading of Unary ++ operator.

Program Code :
#include<iostream>

using namespace std;

classAbc

public:

int m, n;

Abc()

m=8;

n=9;

void show()

cout<<"m= "<<m<<"\nn= "<<n;

int operator ++()

m=m+1;

n=n+1;

cout<<m<<"\n"<<n;

};

int main()

ASHISH KUMAR SALVI 0827CS183D02


Abc o1;

o1.show();

cout<<"\nCalling to overload operator ++ :\n";

cout<<++o1.m<<endl;

cout<<++o1.n;

Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 5
1. Define two classes polar and rectangle to represent points in the polar and rectangle
systems. Use conversion routines to convert from one system to the other

Program Code :
#include<bits/stdc++.h>

const double pi = 3.141592654;

using namespace std;

class Rectangular

double x, y;

public:

Rectangular()

x = 0;

y = 0;

Rectangular(double a, double b)

x = a;

y = b;

doublegetx()

return x;

doublegety()

ASHISH KUMAR SALVI 0827CS183D02


{

return y;

void input()

cout<< " \nENTER THE COORDINATES" <<endl;

cin>> x>>y;

void output()

cout<< "(" << x << "," << y << ")";

};

class Polar

double theta, r;

public:

Polar()

theta = 0;

r = 0;

Polar(Rectangular rect)

r = sqrt(rect.getx() * rect.getx() + rect.gety() + rect.gety());

theta = atan(rect.gety() / rect.getx());

theta = (theta * 180) / pi;

ASHISH KUMAR SALVI 0827CS183D02


}

operator Rectangular()

double x, y;

floatatheta = theta * pi / 180;

x = r * cos(atheta);

y = r * sin(atheta);

return Rectangular(x, y);

void input()

cout<< "\nENTER THE Polar COORDINATE: ";

cin>> r>>theta;

void output()

cout<< "\nr=" << r;

cout<< "\ntheta=" << theta;

};

int main()

Rectangular r1, r2;

Polar p1, p2;

intch, n;

do

ASHISH KUMAR SALVI 0827CS183D02


cout<< "\n\t\t-----------";

cout<< "\n\t\t CONVERSION";

cout<< "\n\t\t-----------";

cout<< "\n1.Rectangular TO Polar \n2.Polar TO Rectangular:" <<endl;

cin>>ch;

switch (ch)

case 1:

r1.input();

p1 = r1;

cout<< "\nRectangular COORDINATES OF R:";

r1.output();

cout<< "\n Polar coordintes of Polar:";

p1.output();

break;

case 2:

p2.input();

r2 = p2;

cout<< "\nRectangular coordinates are:";

r2.output();

break;

cout<< "\nDO U WANT TO CONTINUE:1:yes 2:No";

cin>>n;

}while (n == 1);

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Write a C++ program to perform matrix addition using operator overloading
concept.

Program Code :
#include<iostream>

#include<stdio.h>

using namespace std;

class Matrix

int a[100][100],m,n;

public:

voidtakedata()

cout<<"Enter values of rows and columns : "<<endl;;

cin>>m>>n;

cout<<"enter values : "<<endl;

for(inti=0;i<m;i++)

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

cout<<"enter value ofa["<<i<<"]["<<j<<"]"<<endl;


cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

ASHISH KUMAR SALVI 0827CS183D02


{

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

cout<<a[i][j]<<" ";

cout<<"\n";

Matrix operator+(Matrix &x)

if(x.m==m &&x.n==n)

for(inti=0;i<m;i++)

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

a[i][j] = x.a[i][j]+a[i][j];

else

cout<<"\nOrder of matrix is not same they cannot be added";

exit(0);

};

int main()

Matrix a,b;

a.takedata();

a.show();

b.takedata();

ASHISH KUMAR SALVI 0827CS183D02


b.show();

a+b;

cout<<"\nSum is :"<<endl;

a.show();

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 6:
1. C++ Program to calculate the area and perimeter of rectangles using concept of
inheritance.

Program Code :
#include<iostream>

using namespace std;

class Area

public:

float area1(double l, double b)

return l * b;

};

classPeri

public:

float peri1(double l,double b)

return 2 * ( l + b );

};

class Rectangle : public Area , public Peri

doublelength,breadth;

public:

ASHISH KUMAR SALVI 0827CS183D02


voidgetdata()

cout<<"Enter length : ";

cin>>length;

cout<<"Enter breadth : ";

cin>>breadth;

voidcalc()

cout<<"Area : "<<area1(length,breadth)<<endl;

cout<<"Perimeter : "<<peri1(length,breadth);

};

int main()

Rectangle rect;

rect.getdata();

rect.calc();

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Consider an example of declaring the examination result. Design four classes student,
exam, sports and result. The student has data members such as rollno ,name. Create
the class exam by inheriting the student class. The exam class adds data members
representing the marks scored in 2 subjects and sport class contains sports mark.
Derive the result from exam-class,sports class and it has own data members like total,
avg. Write the interactive program into model this relationship.

Program Code :
#include<iostream>

using namespace std;

class Student

public:

intrno;

string nm;

voidget_n(intrno,string nm)

this->nm=nm;

this->rno=rno;

voidput_n()

cout<<"Name = "<<nm;

cout<<"\nRoll number = "<<rno;

};

classTest:public Student

public:

ASHISH KUMAR SALVI 0827CS183D02


int part1,part2;

voidget_m(int part1,int part2)

this->part1=part1;

this->part2=part2;

voidput_m()

cout<<"\nPart1 = "<<part1;

cout<<"\nPart2 = "<<part2;

};

class Sports :public Student

public:

int score;

voidget_s(int score)

this->score=score;

voidput_s()

cout<<"\nScore = "<<score<<endl;

};

class Result : public Test,public Sports

ASHISH KUMAR SALVI 0827CS183D02


public:

int total;

floatavg;

void show()

total = part1+part2+score;

avg = (float)total/3.0;

Test::put_n();

put_m();

put_s();

cout<<"Total = "<<total<<"\nAverage = "<<avg;

};

int main()

Result res;

res.Test::get_n(12,"CHANDU");

res.get_m(30,35);

res.get_s(7);

res.show();

return 0;

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 7:
1. Create a base class called shape, this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called
triangle and rectangle from the base shape. Add tp the base class, a member
function getdata() to initialize base class data members and another member
function display_area() to compute and display area of figures.. Make display_area
as a virtual function and redefine the function in the derived class to suit their
requirements.Using these three classes, design a program that will accept dimensions
of a triangle or a rectangle interactively and display area.

Program Code :
#include<iostream>

#include<math.h>

using namespace std;

class shape

public:

doublex,y;

voidgetxy()

cout<<"Enter x and y: ";

cin>>x>>y;

voiddisplay_Area()

cout<<"Displaying Area of shape...";

};

class Triangle : public shape

ASHISH KUMAR SALVI 0827CS183D02


public:

double z;

voidgetz()

cout<<"Enter z: ";

cin>>z;

voiddisplay_Area()

double s;

s = x+y+z/2;

cout<<sqrt(s*(s-x)*(s-y)*(s-z))<<endl;

};

class Rectangle : public shape

public:

voiddisplay_Area()

cout<<x*y<<endl;

};

int main()

intch;

area:

cout<<"select 1. Triangle"<<endl;

ASHISH KUMAR SALVI 0827CS183D02


cout<<"select 2. Rectangle"<<endl;

cout<<"Select any one Option: ";

cin>>ch;

switch(ch)

case 1:

Triangle t;

t.getxy();

t.getz();

cout<<"Area of Triangle is : ";

t.display_Area();

break;

case 2:

Rectangle obj;

obj.getxy();

cout<<"Area of Rectangle is : ";

obj.display_Area();

break;

default: exit(0);

goto area;

return 0;

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


2.Run the above program with following modification-
i. Make shape class as abstract class with display_area() as pure virtual function.
ii. Use constructor function to initialize the data members of base class not through the
getdata().

Program Code :
#include<iostream>

#include<math.h>

using namespace std;

class Shape

public:

doublex,y;

Shape(double x1,double y1)

x=x1;

y=y1;

virtual double display_Area()=0;

};

classTriangle:public Shape

public:

double z;

Triangle(double x1,double y1,double z1):Shape(x1,y1)

z=z1;

ASHISH KUMAR SALVI 0827CS183D02


}

doubledisplay_Area()

double s;

s=(x+y+z)/2;

cout<<"Area of Triangle is :"<<sqrt(s*(s-x)*(s-y)*(s-z))<<endl;

};

classRectangle:public Shape

public:

Rectangle(double x1,double y1):Shape(x1,y1)

doubledisplay_Area()

cout<<"Area of recatngle is:"<<x*y<<endl;

};

int main()

cout<<"Enter option 1.)Triangle 2.)Rectangle"<<endl;

doublech,z,x,y;

do

cout<<"Option ";

cin>>ch;

ASHISH KUMAR SALVI 0827CS183D02


if(ch==1)

cout<<"Enter x:";

cin>>x;

cout<<"Enter y:";

cin>>y;

cout<<"Enter z:";

cin>>z;

Triangle t(x,y,z);

t.display_Area();

else if(ch==2)

cout<<"Enter x:";

cin>>x;

cout<<"Enter y:";

cin>>y;

Rectangle r(x,y);

r.display_Area();

else

cout<<"Invalid choice";

}while(ch==1||ch==2);

return 0;

ASHISH KUMAR SALVI 0827CS183D02


Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 8:
1. Write an interactive program to compute square root of a number. The input value
must be tested for validity . If it is negative, the user defined function my_sqrt() should
raise an exception.

Program Code :
#include<iostream>

#include<math.h>

using namespace std;

intmysqrt(int n)

try

if(n>=0)

cout<<"Square root is "<<sqrt(n);

else

throw n;

catch(int e)

cout<<"Number is negative";

int main()

int n;

ASHISH KUMAR SALVI 0827CS183D02


cout<<"Enter the no :"<<endl;

cin>>n;

mysqrt(n);

Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Write a program in C++ that illustrate the mechanism of validating array element
references.

Program Code :
#include <iostream>

using namespace std;

class array

int max;

int a[];

public:

array()

cout<<"Enter size of array :";

cin>>max;

cout<<"\nEnter array elements : ";

for(inti=0;i<max;i++)

cin>>a[i];

operator[](inti)

try

if (i<0 || i>=max)

throw i;

else

ASHISH KUMAR SALVI 0827CS183D02


cout<<a[i];

catch(inti)

cout<<"out of range in array references ";

};

int main()

arrayar;

inti;

cout<<"Enter index to refer :";

cin>>i;

ar[i];

Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 9:
1. Write a c++ program to find maximum of two data items using function template.

Program Code :
#include <iostream>

using namespace std;

template<class T>

T Max(T a, T b)

{ if(a>b)

return a;

else

return b;

int main()

int i1, i2;

float f1, f2;

char c1, c2;

cout<< "\nEnter two characters:\n";

cin>> c1 >> c2;

cout<< Max(c1, c2)<<endl;

cout<< "\nEnter two integers a and b:\n";

cin>> i1 >> i2;

cout<< Max(i1, i2)<<endl;

cout<< "\nEnter two floating-point numbers a and b:\n";

cin>> f1 >> f2;


ASHISH KUMAR SALVI 0827CS183D02
cout<< Max(f1, f2)<<endl;

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Write a class template to represent a generic vector. Include member functions to
perform the following task-
a. To create a vector
b. Sort the elements in ascending order
c. Display the vector

Program Code :
#include <iostream>

using namespace std;

template<typename Temp>

class Array

public:

int n;

Temp *a;

voidget_data();

voidput_data();

void sort( );

};

template<typename Temp>

void Array<Temp>::get_data()

inti;

cout<<"\nEnter how many no:"<<endl;

cin>>n;

a=new Temp[n];

cout<<"Enter numbers:"<<endl;

ASHISH KUMAR SALVI 0827CS183D02


for(i=0;i<n;i++)

cin>>a[i];

template<typename Temp>

void Array<Temp>::put_data()

for(inti=0;i<n;i++)

cout<<a[i]<<" ";

template<typename Temp>

void Array<Temp>::sort()

Temp k;

inti,j;

cout<<"After sorting:"<<endl;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

if (a[i]<a[j])

k=a[i];

a[i]=a[j];

ASHISH KUMAR SALVI 0827CS183D02


a[j]=k;

int main()

Array<int> a;

Array<float> b;

a.get_data();

a.sort();

a.put_data();

b.get_data();

b.sort();

b.put_data();

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


Experiment 10:

1. Write a c++ program for matrix multiplication with following specifications-


a. Use constructor dynamic memory allocation for matrix
b. Use getdata() function to input values for matrix
c. Use show() to display the matrix
d. Use mul() to multiply two matrices

Program Code :
#include <iostream>

using namespace std;

class matrix

int **a;

intm,n;

public :

matrix(intr,int c)

{
ASHISH KUMAR SALVI 0827CS183D02
m=r; n=c;

a=new int*[m];

for(inti=0;i<m;i++)

a[i]=new int[n];

voidgetdata()

for(inti=0;i<m;i++)

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

cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

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

cout<<a[i][j]<<" ";

cout<<endl;

~matrix()

for(inti=0;i<m;i++)

delete a[i];

delete a;

matrixmul(matrix &y)

ASHISH KUMAR SALVI 0827CS183D02


{

matrix z(m,y.n);

for(inti=0;i<m;i++)

for(int j=0;j<y.n;j++)

z.a[i][j]=0;

for(int k=0;k<n;k++)

z.a[i][j]+=a[i][k]*y.a[k][j];

return z;

};

int main()

intm,n;

cout<<"Enter no. of rows and columns for Matrix X :";

cin>>m>>n;

matrix x(m,n);

cout<<"Enter no. of rows and columns for Matrix Y :";

cin>>m>>n;

matrix y(m,n);

cout<<"\nEnter Matrix X :\n";

x.getdata();

cout<<"\nEnter Matrix Y :\n";

y.getdata();

cout<<"\nMatrix X :\n";

x.show();

ASHISH KUMAR SALVI 0827CS183D02


cout<<"\nMatrix Y :\n";

y.show();

cout<<"\nMultiplication of X and Y is : \n";

matrix z=x.mul(y);

z.show();

Output :

ASHISH KUMAR SALVI 0827CS183D02


2. Modify the above program as follows-
a. Use operator*() for matrix multiplication instead of mul()
b. Make operator*() as friend function

ASHISH KUMAR SALVI 0827CS183D02


Program Code :
#include <iostream>

using namespace std;

class matrix

int **a;

intm,n;

public :

matrix()

matrix(intr,int c)

m=r; n=c;

a=new int*[m];

for(inti=0;i<m;i++)

a[i]=new int[n];

voidgetdata()

for(inti=0;i<m;i++)

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

cin>>a[i][j];

void show()

for(inti=0;i<m;i++)

ASHISH KUMAR SALVI 0827CS183D02


{

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

cout<<a[i][j]<<" ";

cout<<endl;

~matrix()

for(inti=0;i<m;i++)

delete a[i];

delete a;

friend matrix operator*(matrix &x,matrix&y);

};

matrix operator*(matrix &x,matrix&y)

matrix z(x.m,y.n);

for(inti=0;i<x.m;i++)

for(int j=0;j<y.n;j++)

z.a[i][j]=0;

for(int k=0;k<y.n;k++)

z.a[i][j]+=x.a[i][k]*y.a[k][j];

return z;

int main()

ASHISH KUMAR SALVI 0827CS183D02


{

intm,n;

cout<<"Enter no. of rows and columns for Matrix X :";

cin>>m>>n;

matrix x(m,n);

cout<<"Enter no. of rows and columns for Matrix Y :";

cin>>m>>n;

matrix y(m,n);

cout<<"\nEnter Matrix X :\n";

x.getdata();

cout<<"\nEnter Matrix Y :\n";

y.getdata();

cout<<"\nMatrix X :\n";

x.show();

cout<<"\nMatrix Y :\n";

y.show();

cout<<"\nMultiplication of X and Y is : \n";

matrix z = x*y;

z.show();

Output :

ASHISH KUMAR SALVI 0827CS183D02


3. To perform the write operation within a file.

Program Code :
#include <iostream>

ASHISH KUMAR SALVI 0827CS183D02


#include <fstream>

using namespace std;

int main()

charc,fname[20];

cout<<"Enter file name :";

cin>>fname;

ofstreamfout;

fout.open(fname);

cout<<"\nEnter contents to store in file(Enter # at end) :\n";

while((c=getchar())!='#')

fout<<c;

fout.close();

Output :

4. Program for read the content of a file.

Program Code :
#include <iostream>

ASHISH KUMAR SALVI 0827CS183D02


#include <fstream>

using namespace std;

int main()

charc,fname[20];

cout<<"Enter file name :";

cin>>fname;

ifstream fin(fname);

if(!fin)

cout<<"\n! File does not exist !";

return 0;

else

while(fin.eof()==0)

fin.get(c);

cout<<c;

return 0;

Output :

ASHISH KUMAR SALVI 0827CS183D02


ASHISH KUMAR SALVI 0827CS183D02

Vous aimerez peut-être aussi