Object Oriented Programming and Object Oriented Design Programming Languages Robert Dewar.
C++ & Object Oriented Programming Lab Manualcse.gec.edu.in/wp-content/uploads/2017/03/C.docx · Web...
Transcript of C++ & Object Oriented Programming Lab Manualcse.gec.edu.in/wp-content/uploads/2017/03/C.docx · Web...
C++ & Object Oriented Programming Lab Manual
LAB MANUALC++ & Object Oriented
Programming Lab(3rd sem CSE)
PREPARED BY
Asst.Prof.Santosh Kumar Satapathy
DEPARTMENT OF CSE
GANDHI ENGINEERING COLLEGE Page 1
C++ & Object Oriented Programming Lab Manual
BECS7212 C++ & Object Oriented Programming Lab
1. Programs on concept of classes and objects.(1 class)
2. Programs using inheritance. (1 class)
3. Programs using static polymorphism. (1 class)
4. Programs on dynamic polymorphism. (1 class)
5. Programs on operator overloading. (1 class)
6. Programs on dynamic memory management using new, delete operators.(1 class)
7. Programs on copy constructor and usage of assignment operator. (1 class)
8. Programs on exception handling. (1 class)
9. Programs on generic programming using template function & template class.(1 class)
10. Programs on file handling. (1 class)
Lab Objective
At the end of the course students should be familiar with the main features of the C++ language.
Be able to write a C++ program to solve a well specified problem. Understand a C++ program written by someone else. Be able to debug and test C++ programs; Understand how to read C++ doc library documentation and reuse library
code. To make the students understand the features of object oriented principles
and familiarize them with virtual functions, templates and exception handling.
To make the students to develop applications using C++.
GANDHI ENGINEERING COLLEGE Page 2
C++ & Object Oriented Programming Lab Manual
Lab Outcome
Students will be able to apply the computer programming techniques to solve practical problems.
Students will be able to understand the concepts and implementation of constructors and destructors.
Students will be able to develop software applications using object oriented programming language in C++
Student can be able to understand and use the basic programming constructs of C++
Students are able to learn C++ data types, memory allocation/deallocations, functions and pointers.
Students are able to apply object oriented programming concepts to software problems in C++
List of Experiment beyond Syllabus (At least 2)
To perform the write operation within a file.
Program for read the content of a file.
GANDHI ENGINEERING COLLEGE Page 3
C++ & Object Oriented Programming Lab Manual
3. To perform the write operation within a file.
GANDHI ENGINEERING COLLEGE Page 4
Exp. No Exp. to be conducted
1
1. Write a program to enter mark of 6 different subjects and find out the total mark (Using cin and cout statement) 2. Write a function using reference variables as arguments to swap the values of pair of integers.3. Write a inline function to find largest of three numbers.4 Write a program to find the factorial of a number using recursion.
2
1. Define a class to represent a bank account which includes the following members as Data members:a) Name of the depositor b)Account Number c)Withdrawal amount d)Balance amount in the accountMember Functions:a) To assign initial values b)To deposit an amount c) To withdraw an amount after checking the balance d) To display name and balance.2. Write the above program for handling n number of account holders using array of objects.3. Write a C++ program to compute area of right angle triangle, equilateral triangle, isosceles triangle using function overloading concept.
3
1. Write a C++ program to swap the values two integer members of different classes using friend function .2. Write a C++ program for addition of two complex numbers using friend function (use constructor function to initialize data members of complex class) .
41. Define a class string and overload = = to compare two strings and + operator for concatenation of two strings.2. Write a program for overloading of Unary ++ operator.
C++ & Object Oriented Programming Lab Manual
4.Program for read the content of a file.
Experiment 1:
1. Write a function using reference variables as arguments to swap the values of pair of integersAns:-Pseudo code:
swap(x,y)
begin
set z=xset x=yset y=z
end
Output:enter a,b
GANDHI ENGINEERING COLLEGE Page 5
C++ & Object Oriented Programming Lab Manual
5 7a=7 b=5
2. Write a inline function to find largest of three numbersAns:-Pseudo code max(a, b, c) begin
return a>b&&a>c?a:b>a&&b>c?b:c end
Output:enter x,y,z
5 7 9
maximum=9
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.Ans:-
Pseudocode:power(m,n)beginp=1for i=0 to n by 1
dop=p*mend forreturn(p) end
Output:
GANDHI ENGINEERING COLLEGE Page 6
C++ & Object Oriented Programming Lab Manual
CHOICES
1) Only Value of M2) Value for both M and N
ENTER YOUR CHOICE:-1Enter Value for M:-2.5Power function when default argument is used =6.25ENTER YOUR CHOICE:-2Enter Value for M and N:-2.5 3Power function when actual argument is use =15.625
VIVA QUESTIONS
1.List the different types of parameter passing techniques.2.What are reference variable?3. What is an inline function ?4. what is a default argument ?5. Define Inline Function?
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 Number3. Withdraw amount4. Balance amount in the account
Member Functions:
1. To assign initial values2. To deposit an amount3. To withdraw an amount after checking the balance4. To display name and balance.
Write a main program to test the program.
GANDHI ENGINEERING COLLEGE Page 7
C++ & Object Oriented Programming Lab Manual
Pseudo code:
Bankname
acno
actype
bal
opbal()
deposit()
withdraw()
display()
opbal()beginPRINT"Enter Name :-"INPUT namePRINT"Enter A/c no. :-"input acnoprint"Enter A/c Type :-"input actype;print "Enter Opening Balance:-"input bal
end deposit()
beginprint"Enter Deposit amount :-"set deposit=0input depositset deposit=deposit+balprint depositset bal=deposit
endwithdraw()
begin withdrawprint balprint"\nEnter Withdraw Amount :-"input withdrawset bal=bal-withdrawprint bal;
GANDHI ENGINEERING COLLEGE Page 8
C++ & Object Oriented Programming Lab Manual
end display()
beginprint "DETAILS"print nameprint acnoprint actypeprint bal
end
Output:Choice List1) To assign Initial Value2) To Deposit3) To Withdraw4) To Display All Details5) EXIT
Enter your choice :-1
call to opbal member functionEnter Name :-Suren kumar SahuEnter A/c no. :-17717Enter A/c Type :-SavingEnter Opening Balance:-5000
Enter your choice :-2
call to deposit member function Enter Deposit amount :-1000Deposit Balance =6000
Enter your choice :-3
call to withdraw member function Balance Amount =6000Enter Withdraw Amount :-2000After Withdraw Balance is 4000
Enter your choice :-4
call to display member function "DETAILS"
name Suren kumar SahuA/c. No 17717A/c Type SavingBalance 4000
Enter your choice :-5Exit from programme contorl
2. Write the above program for handling n number of account holders using array of objects for data initialize and displaying all records.
Outputenter how many records 5
call to opbal member function 5 timesEnter Name :-Suren kumar Sahu
GANDHI ENGINEERING COLLEGE Page 9
C++ & Object Oriented Programming Lab Manual
Enter A/c no. :-17717Enter A/c Type :-SavingEnter Opening Balance:-5000
Enter Name :-Santosh kumar SatapathyEnter A/c no. :-17718Enter A/c Type :-SavingEnter Opening Balance:-3000
Enter Name :-Ramesh kumar SamantrayEnter A/c no. :-17719Enter A/c Type :-SavingEnter Opening Balance:-4000
Enter Name :-Rasmita PandaEnter A/c no. :-17720Enter A/c Type :-SavingEnter Opening Balance:-5000
Enter Name :-Rajesh Kumar MohantyEnter A/c no. :-17721Enter A/c Type :-SavingEnter Opening Balance:-5000
call to display member function 5 times"DETAILS"
name Suren kumar SahuA/c. No 17717A/c Type SavingBalance 5000
name Santosh Kumar SatapathyA/c. No 17718A/c Type SavingBalance 3000
name Ramesh Kumar SamantrayA/c. No 17719A/c Type SavingBalance 4000
name Rasmita PandaA/c. No 17720A/c Type SavingBalance 5000
name Rajesh Kumar MohantyA/c. No 17721A/c Type SavingBalance 5000
3. Write a C++ program to compute area of right angle triangle, equilateral triangle ,scalene triangle using function overloading concept.
Pseudo code:
GANDHI ENGINEERING COLLEGE Page 10
C++ & Object Oriented Programming Lab Manual
area(a,b)
beginreturn 0.5*a*b
end
area( a)
beginreturn sqrt(3)/4)*a*a
end
area( a,b,c)
beginfloat sset s=(float)(a+b+c)/2return sqrt s*(s-a)*(s-b)*(s-c)
end
Outputarea of scalene triangle sqrt 8.4375area of equilateral triangle area of right angled triangle 4.375
VIVA QUESTIONS1. What are the basic concepts of OOPS?2. What are objects?3. What is a class?4. List out the characteristics of OOP.5. Write the syntax of class declaration
GANDHI ENGINEERING COLLEGE Page 11
C++ & Object Oriented Programming Lab Manual
Experiment 3:
1. Write a C++ program to swap the values two integer members of different classes using friend function
Pseudo code:
GANDHI ENGINEERING COLLEGE Page 12
class1value1indata( int a)display(void)
exchange (class1 &,class2 &)
C++ & Object Oriented Programming Lab Manual
exchange ( &x, &y)
begin
set temp=x.value1;set x.value1=y.value2;set y.value2=temp;
end
Outputvalues before exchange:value1=100value2=200values after exchange :value1=200value2=100
2. Write a C++ program for addition of two complex numbers using friend function (use constructor function to initialize data members of complex class)
Pseudocode:
complexxy
Complex()
GANDHI ENGINEERING COLLEGE Page 13
Class2Value2
indata( a)display(void)
exchange (class1 &,class2 &)
C++ & Object Oriented Programming Lab Manual
complex( real, imag)complex sum( complex , complex)
show(complex )
complex()begin
set x=0set y=0
endcomplex( real, imag)
beginset x=realset y=imag
end
sum( c1, c2)begin
set complex c3set c3.x=c1.x+c2.xset c3.y=c1.y+c2.yreturn c3
endshow( c)
beginprint c.xprint c.y
end
OutputSet the value of real and imag through parameterized constructor
a=3.1+j5.65
b=2.75+j1.2
c=5.85+j 6.85
VIVA QUESTIONS
1. What is a friend function? 2. What are the special characteristics of friend function ? 3. How the objects are used as function argument? 4. What are Friend functions? Write the syntax
GANDHI ENGINEERING COLLEGE Page 14
C++ & Object Oriented Programming Lab Manual
5. Write some properties of friend functions.
Experiment 4:
1. Define a class string and overload == to compare two strings and + operator for concatenation two strings
Pseudo code:
String*sl
string()string(char *s1)void show()string operator+( &x, &y)
operator==( &x, &y)
GANDHI ENGINEERING COLLEGE Page 15
C++ & Object Oriented Programming Lab Manual
string()begin
set s=0set l=0
endstring(char *s1)
beginset l=strlen(s1)set s=new char[l+1]strcpy(s,s1)
endshow()
beginprint s
end
operator==( &x,&y)
beginif(strcmp(x.s,y.s)==0)
return 1else
return 0end if
endoperator +( &x,&y)
beginset string zset z.l=x.l+y.lset z.s=new char[z.l+1]strcpy(z.s,x.s)strcat(z.s,y.s)return z
endOutput Set the value of string through parameterized constructor
P=GECq=BBSRCalling to overload operator ==
both strings are not equal
GANDHI ENGINEERING COLLEGE Page 16
C++ & Object Oriented Programming Lab Manual
Calling to overload operator + GECBBSR
2. Write a program for overloading of Unary ++ operator.
Pseudo code:
abc()begin
set m=8set n=9
end
void show()begin
print m,nend
operator++()begin
set m=m+1set n=n+1
end
Output
Set the value of m and n through default constructor
m=8 n=9
GANDHI ENGINEERING COLLEGE Page 17
abcm,n
abc()void show()
operator++()
C++ & Object Oriented Programming Lab Manual
call to show function
8 9
calling to overload operator ++9 10
VIVA QUESTIONS1. What is operator overloading?2. What is the purpose of using operator function? Write its syntax.3. Write at least four rules for Operator overloading.4. How will you overload Unary & Binary operator using member functions?5. How will you overload Unary and Binary operator using Friend functions?
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
Pseudo code:
rectangularx,y
rectangular()polar(rectangular rect)
GANDHI ENGINEERING COLLEGE Page 18
C++ & Object Oriented Programming Lab Manual
operator rectangular()void input()void output()
rectangular()begin
set x=0set y=0
endrectangular(a,b)begin
Set x=aSet y=b
enddouble getx()begin
return xenddouble gety()begin
return yendvoid input()begin
print “ENTER THE COORDINATES"input x,y
endvoid output()begin
print xprint y
end
polar
theta,r
polar ()polar(rectangular rect)operator rectangular()void input()void output()
polar ()begin
GANDHI ENGINEERING COLLEGE Page 19
C++ & Object Oriented Programming Lab Manual
set theta=0set r=0
endpolar(rectangular rect)begin
Set r=sqrtrect.getx()*rect.getx()+rect.gety()+rect.gety()Set theta=atan(rect.gety()/rect.getx())Set theta=(theta*180)/pi
endoperator rectangular()begin
set x,yset atheta=theta*pi/180set x=r*cos(atheta)set y=r*sin(atheta)return rectangular(x,y)
end
void input()begin
print”ENTER THE POLAR COORDINATE: "input r,theta
end
void output()begin
print rinput theta
end
Output-----------
CONVERSION-----------
2.POLAR TO RECTANGULAR:
ENTER THE COORDINATES 2.0 9.0rectangular coordinates are:(0.0,20)
2. Write a C++ program to perform matrix addition using operator overloading concept.
Pseudo code:
Matrix
a[100][100], m,n
void getdata()
GANDHI ENGINEERING COLLEGE Page 20
C++ & Object Oriented Programming Lab Manual
void show()matrix operator+(matrix &x,matrix &y)
getdata()beginset i,j;print"enter m,n:"input m,n
for i=0 to m by 1do
for j=0 to n by 1 do print "enter a number:"input a[i][j]end for
end forend
show()beginint i,j;for i=0 to m by 1
dofor j=0 to n by 1
do print a[i][j]end for
end forend
operator+(&x,&y) begin int i,j
matrix zfor i=0 to m by 1
dofor j=0 to n by 1
doz.a[i][j]=z.a[i][j]+x.a[i][j]*y.a[i][j]
end forend for
end
Output
enter m,n:2 2
FOR p OBJECT
GANDHI ENGINEERING COLLEGE Page 21
C++ & Object Oriented Programming Lab Manual
------------------
enter a number:a[0][0] = 5
enter a number:a[0][1] = 6
enter a number:a[1][0] = 8
enter a number:a[1][1] = 10
FOR q OBJECT
------------------
enter a number:a[0][0] = 2
enter a number:a[0][1] = 7
enter a number:a[1][0] = 12
enter a number:a[1][1] = 15
Calling to overload operator +
7 13
20 25
VIVA QUESTIONS
1.How an overloaded operator can be invoked using member functions?
2. How an overloaded operator can be invoked using Friend functions?
3. List out the operators that cannot be overloaded using Friend function.
4. Explain basic to class type conversion with an example.
GANDHI ENGINEERING COLLEGE Page 22
C++ & Object Oriented Programming Lab Manual
Experiment 6:1. C++ Program to calculate the area and perimeter of rectangles using concept of inheritance.
Pseudocode:
GANDHI ENGINEERING COLLEGE Page 23
Area
area(float l,float b)
Perimeter
float peri(float l,float b)
C++ & Object Oriented Programming Lab Manual
area( l, b)
begin
return l*b
end
peri(l,b)
begin
return 2*(l+b) end
get_data( )
begin print "Enter length: " input length print"Enter breadth: " input breadth
end
calc()
beginprint area(length,breadth)print peri(length,breadth)
GANDHI ENGINEERING COLLEGE Page 24
Rectangle
length, breadth
void get_data( ) void calc()
C++ & Object Oriented Programming Lab Manual
end
OutputEnter length: 5Enter breadth:7area=35perimeter=24
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.
Pseudocode:
GANDHI ENGINEERING COLLEGE Page 25
studentrno;
nm[30];get_nput_n
sportsscore
void get_s (int a)
testpart1part2
get_m (int x, int y)put.m (void)
C++ & Object Oriented Programming Lab Manual
STUDENT CLASS
get_n ( a, *n) begin
set rno = astrcpy(nm,n)
end
put_n (void) begin
print rno,nmend
TEST CLASS
get_m ( x, y)
begin Set part1= x Set part2=y
end
put.m (void)begin
print part1,part2end
SPORTS CLASS
get_s (a) beginset score = a end
put_s (void)beginprint scoreend
RESULT CLASS
GANDHI ENGINEERING COLLEGE Page 26
resultint totalfloat avg
void show (void)
C++ & Object Oriented Programming Lab Manual
show (void)
begin
set total = part1 + part2 + score
set avg=(float)(total/3)
put_n ( )put_m ( )put_s ( ) print totalprint avg
endOutput:Roll No.12 name Satyamarks obtained:part1 = 30part2 = 35sports wt.: 7total score= 72avg score= 24.0VIVA QUESTIONS
1. What is meant by inheritance?
2. What is meant by single inheritance?
3. What is multilevel inheritance?
4. What are the Access specifier used in Inheritance?
5.Write the uses of virtual base class?
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.
GANDHI ENGINEERING COLLEGE Page 27
C++ & Object Oriented Programming Lab Manual
Pseudo code:
CLASS SHAPEgetxy()begin
print"enter x,y:"input x,y
end
display_area()begin
print"displaying area of shape..."endCLASS TRIANGLEgetz()begin
print "enter z:"input z
enddisplay_area()begin
set double sset s=(x+y+z)/2print sqrt s*(s-x)*(s-y)*(s-z)
GANDHI ENGINEERING COLLEGE Page 28
shapex,ygetxy()display_area()
trianglezgetz()
display_area()
rectangle
display_area()
C++ & Object Oriented Programming Lab Manual
end
CLASS RECTANGLEdisplay_area()begin
print "area of rectangle="print x*y
end
Output:enter option 1.triangle 2.rectangleoption: 1enter x,y: 5 7enter z:9area of traingle=17.41option : 2enter x,y: 5 6area of rectangle=3
2. Run the above program with following modificationi. 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()
GANDHI ENGINEERING COLLEGE Page 29
shapex,yshape(int x1,int y1)display_area()
C++ & Object Oriented Programming Lab Manual
triangleztriangle(x1,y1,z1)display_area()
CLASS SHAPE
shape(x1,y1)begin
set x=x1set y=y1
enddisplay_area()begin
print"displaying area of shape..."end
CLASS TRIANGLEtriangle(x1,y1,z1):shape(x1,y1)begin
set z=z1end
display_area()begin
double sset s=(x+y+z)/2print "area of traingle="print sqrt(s*(s-x)*(s-y)*(s-z))
end
CLASS RECTANGLErectangle(x1,y1):shape(x1,y1)beginenddisplay_area()begin
Print "area of rectangle="
GANDHI ENGINEERING COLLEGE Page 30
rectangle
rectangle(x1,y1)display_area()
C++ & Object Oriented Programming Lab Manual
Print x*yend
Output:enter option 1.triangle 2.rectangle
option 1
enter x,y:5 7
enter z:9
area of traingle=17.41
area of rectangle=12
VIVA QUESTIONS
1. What is meant by Abstract base class?2. Define Virtual Functions3. Define an abstract class.4. What is a virtual function?5. What is a pure virtual function?
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.
Pseudo code:my_sqrt( a)
begintrybegin
if (a<0) throw a
else
GANDHI ENGINEERING COLLEGE Page 31
C++ & Object Oriented Programming Lab Manual
print sqrt(a)end trycatch(x)begin
print ”number must not be negative for finding square root”end catch
end
Output:enter a number:25
square root =5
enter a number: -4
number must not be negative for finding square root
2. Write a program in C++ that illustrate the mechanism of validating array element references
Pseudo code:
operator []( i)beginif (i<0 || i>=max)
throw i
elsereturn a[i]
end
Output:trying to refer a[1]…
Calling to overload []
3
trying to refer a[13]…
Calling to overload []
out of range in array references…
GANDHI ENGINEERING COLLEGE Page 32
arraya[max]
operator[](int i)
C++ & Object Oriented Programming Lab Manual
VIVA QUESTION
1. Define Exception Handling2. What are the type of Error?3. Define try and catch.4. What are the type of Error?
Experiment 9:
1. Write a c++ program to find maximum of two data items using function template
Pseudo code:
T max(T a,T b)begin
if (a>b)return a
elsereturn b
end
GANDHI ENGINEERING COLLEGE Page 33
C++ & Object Oriented Programming Lab Manual
Output:
enter two characters: a ccenter a,b:5 77enter p,q:5.2 7.57.5
2. Write a class template to represent a generic vector. Include member functions to perform the following task
a. To create a vectorb. Sort the elements in ascending orderc. Display the vector
Pseudo code:
arrayT *a;int n;
void getdata()void putdata()void sort( )
getdata()begin
int iprint ”enter how many no:”input nset a=new T[n]for i=0 to n by 1do
print”enter a number:”input a[i]
end forend
putdata()begin
for i=0 to n by 1do
Print a[i]end for
end sort( )
GANDHI ENGINEERING COLLEGE Page 34
C++ & Object Oriented Programming Lab Manual
begin T k;int i,j;
for i=0 to n by 1do
for j=0 to n by 1do
if (a[i]>a[j])do
set k=a[i]set a[i]=a[j]set a[j]=k
end if end for end forend
Output:enter how many no:5
enter a number:
5 8 2 4 1
After Sorting
1 2 4 5 8
enter a number:
5.2 8.7 2.3 4.5 1.1
After Sorting
1.1 2.3 4.5 5.2 8.7
VIVA QUESTIONS
1. Define Template.2. What is meant by Function Template.3. Write the syntax for Function template?4. What is use of Template?
GANDHI ENGINEERING COLLEGE Page 35
C++ & Object Oriented Programming Lab Manual
Experiment 10:
1. Write a c++ program for matrix multiplication with following specificationsa. Use constructor dynamic memory allocation for matrixb. Use getdata() function to input values for matrixc. Use show() to display the matrixd. Use mul() to multiply two matrices
Pseudo code:
GANDHI ENGINEERING COLLEGE Page 36
matrix**a m,n
matrix()void getdata()void show()~matrix()mul(matrix &x,matrix &y)
C++ & Object Oriented Programming Lab Manual
matrix()beginset iprint "enter m,n:"input m,nset a=new int*[m]for i=0 to m by 1set a[i]=new int[n]end
getdata()beginset i,j;for i=0 to m by 1do
for j=0 to n by 1 do print "enter a number:"input a[i][j]end for
end forend
show()beginset i,jfor i=0 to m by 1do
for j=0 to n by 1 do
print a[i][j]end for
end forend
~matrix()begin
int i;for i=0 to m by 1delete a[i]delete a
end
matrix mul(matrix &x,matrix &y) begin int i,j,k
matrix zfor i=0 to m by 1do
GANDHI ENGINEERING COLLEGE Page 37
C++ & Object Oriented Programming Lab Manual
for j=0 to n by 1 do z[i][j]=0;
for k=0 to n by 1 do
z[i][j]=z[i][j]+x[i][k]*y[k][j] end for
end for end for
return zend
Output:enter m,n:2 2Enter the matrix of size2x22 32 3Enter the matrix of size2x24 54 5The matrix for object p:2 32 3The matrix for object q:4 54 5The resultant matrix20 2520 25
2. Modify the above program as followa. Use operator*() for matrix multiplication instead of mul()b. Make operator*() as friend function
Pseudo code:
GANDHI ENGINEERING COLLEGE Page 38
matrix**a;m,n;
matrix()void getdata()void show()~matrix()operator*(matrix &x,matrix &y)
C++ & Object Oriented Programming Lab Manual
matrix()beginint iprint"enter m,n:"input m,nset a=new int*[m]for i=0 to m by 1set a[i]=new int[n]end
getdata()beginint i,jfor i=0 to m by 1do
for j=0 to n by 1doprint"enter a number:"input a[i][j]end for
End forend
show()beginint i,jfor i=0 to m by 1do
for j=0 to n by 1
doprint a[i][j]end for
end forend
~matrix()begin
int i;for i=0 to m by 1
delete a[i]delete a
endmatrix operator*(&x,&y) begin int i,j,k
matrix zfor i=0 tox. m by 1do
forj=0toy.n by 1do
set z.a[i][j]=0for k=0 to x.m by 1doset z.a[i][j]=z.a[i][j]+x.a[i][k]*y.a[k][j]end for
end forend for
GANDHI ENGINEERING COLLEGE Page 39
C++ & Object Oriented Programming Lab Manual
return end
Output:enter m,n:2 2Enter the matrix of size2x22 32 3Enter the matrix of size2x24 54 5The matrix for object p:2 32 3The matrix for object q:4 54 5Calling to overload *20 2520 25
3. To perform the write operation within a file.
main()
begin
c,fname[10]
ofstream out
print"Enter File name:"
input fname
out.open(fname)
print "Enter contents to store in file (Enter # at end):\n"
while((c=getchar())!='#')
do
print out.c;
end while
out.close();
endOutput: Enter File name: one.txt Enter contents to store in file (enter # at end)
4.Program for read the content of a file.
GANDHI ENGINEERING COLLEGE Page 40
C++ & Object Oriented Programming Lab Manual
main()begin c,fname[10]; print "Enter file name:" input fname; ifstream in(fname) if(!in) do print "File Does not Exist" getch(); return; end if while(in.eof()==0) do in.get(c); print c; end while getch();}
Output: Enter File name: one.txt Master of Computer Applications
VIVA QUESTIONS
1. What are free store operators (or) Memory management operators?
2. What is the purpose of using operator function? Write its syntax
3. What is operator overloading?
4. How will you overload Unary & Binary operator using member functions?
5. How will you overload Unary and Binary operator using Friend functions?
GANDHI ENGINEERING COLLEGE Page 41