Вы находитесь на странице: 1из 59

C++ & Object Oriented Programming Lab Manual

CS-304 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++.
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++

NAME OF STUDENT ENROLLMENT NUMBER Page 1


C++ & Object Oriented Programming Lab Manual

NAME OF STUDENT ENROLLMENT NUMBER Page 2


C++ & Object Oriented Programming Lab Manual
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.

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 account
Member Functions:
a) To assign initial values b)To deposit an amount c) To
2 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.

1. Write a C++ program to swap the values two integer members


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

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

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.
5
2. Write a C++ program to perform matrix addition using
operator overloading concept.

NAME OF STUDENT ENROLLMENT NUMBER Page 3


C++ & Object Oriented Programming Lab Manual

1. Consider a publishing company that markets both book and


audio cassette version to its works. Create a class Publication
that stores the title (a string) and price (type float) of a
publication. Derive the following two classes from the above
Publication class: Book which adds a page count (int) and Tape
which adds a playing time in minutes(float). Each class should
have get_data() function to get its data from the user at the
keyboard.Write the main() function to test the Book and Tape
classes by creating instances of them asking the user to fill in
data with get_data() and then displaying it using put_data().
6
2. Consider an example of declaring the examination result.
Design three classes student, exam and result. The student has
data members such as rollno ,name. Create the lass exam by
inheriting the student class. The exam class adds data members
representing the marks scored in 5 subjects. Derive the result
from exam-class and it has own data members like total, avg.
Write the interactive program into model this relationship.

1. Create a base class called shape, Use 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 acdept
dimensions of a triangle or a rectangle interactively and display
7
area.

2. Run the above program with following modification


a)Make shape class as abstract class with display_area() as pure
virtual function
b) Use constructor function to initialize the data members of
base class not through the getdata().

NAME OF STUDENT ENROLLMENT NUMBER Page 4


C++ & Object Oriented Programming Lab Manual

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

2. Write a program in C++ that illustrate the mechanism of


validating array element references

1.Write a c++ program to find maximum of two data items using


9 function template
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

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


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

2. Modify the above program as follow


a) Use operator*() for matrix multiplication instead of mul( )
b)Make operator*() as friend function

NAME OF STUDENT ENROLLMENT NUMBER Page 5


C++ & Object Oriented Programming Lab Manual

Experiment 1:
1. Write a function using reference variables as arguments to swap the values of pair
of integers
Ans:-
Pseudo code:
swap(x,y)
begin
set z=x
set x=y
set y=z
end
Output:
enter a,b
57
a=7
b=5

NAME OF STUDENT ENROLLMENT NUMBER Page 6


C++ & Object Oriented Programming Lab Manual

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


Ans:-
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

NAME OF STUDENT ENROLLMENT NUMBER Page 7


C++ & Object Oriented Programming Lab Manual

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)
begin
p=1
for i=0 to n by 1
do
p=p*m
end for
return(p)
end
Output:
CHOICES
1) Only Value of M
2) Value for both M and N
ENTER YOUR CHOICE:-1
Enter Value for M:-2.5
Power function when default argument is used =6.25
ENTER YOUR CHOICE:-2
Enter Value for M and N:-2.5 3
Power function when actual argument is use =15.625

NAME OF STUDENT ENROLLMENT NUMBER Page 8


C++ & Object Oriented Programming Lab Manual

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?

NAME OF STUDENT ENROLLMENT NUMBER Page 9


C++ & Object Oriented Programming Lab Manual

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.
Pseudo code:

Bank

name
acno
actype
bal

opbal()
deposit()
withdraw()
display()

opbal()
begin
PRINT"Enter Name :-"
INPUT name
PRINT"Enter A/c no. :-"
input acno
print"Enter A/c Type :-"
input actype;
print "Enter Opening Balance:-"

NAME OF STUDENT ENROLLMENT NUMBER Page 10


C++ & Object Oriented Programming Lab Manual

input bal
end
deposit()
begin
print"Enter Deposit amount :-"
set deposit=0
input deposit
set deposit=deposit+bal
print deposit
set bal=deposit
end
withdraw()
begin
withdraw
print bal
print"\nEnter Withdraw Amount :-"
input withdraw
set bal=bal-withdraw
print bal;
end
display()
begin
print "DETAILS"
print name
print acno
print actype
print bal
end
Output:
Choice List
1) To assign Initial Value
2) To Deposit
3) To Withdraw

NAME OF STUDENT ENROLLMENT NUMBER Page 11


C++ & Object Oriented Programming Lab Manual

4) To Display All Details


5) EXIT
Enter your choice :-1
call to opbal member function
Enter Name :-Suren kumar Sahu
Enter A/c no. :-17717
Enter A/c Type :-Saving
Enter Opening Balance:-5000
Enter your choice :-2
call to deposit member function
Enter Deposit amount :-1000
Deposit Balance =6000
Enter your choice :-3
call to withdraw member function
Balance Amount =6000
Enter Withdraw Amount :-2000
After Withdraw Balance is 4000
Enter your choice :-4
call to display member function
"DETAILS"
name Suren kumar Sahu
A/c. No 17717
A/c Type Saving
Balance 4000
Enter your choice :-5
Exit from programme contorl

NAME OF STUDENT ENROLLMENT NUMBER Page 12


C++ & Object Oriented Programming Lab Manual

2. Write the above program for handling n number of account holders using array of
objects for data initialize and displaying all records.
Output
enter how many records 5
call to opbal member function 5 times
Enter Name :-Suren kumar Sahu
Enter A/c no. :-17717
Enter A/c Type :-Saving
Enter Opening Balance:-5000

Enter Name :-Santosh kumar Satapathy


Enter A/c no. :-17718
Enter A/c Type :-Saving
Enter Opening Balance:-3000

Enter Name :-Ramesh kumar Samantray


Enter A/c no. :-17719
Enter A/c Type :-Saving
Enter Opening Balance:-4000

Enter Name :-Rasmita Panda


Enter A/c no. :-17720
Enter A/c Type :-Saving
Enter Opening Balance:-5000

Enter Name :-Rajesh Kumar Mohanty


Enter A/c no. :-17721
Enter A/c Type :-Saving
Enter Opening Balance:-5000

call to display member function 5 times


"DETAILS"
name Suren kumar Sahu

NAME OF STUDENT ENROLLMENT NUMBER Page 13


C++ & Object Oriented Programming Lab Manual

A/c. No 17717
A/c Type Saving
Balance 5000

name Santosh Kumar Satapathy


A/c. No 17718
A/c Type Saving
Balance 3000

name Ramesh Kumar Samantray


A/c. No 17719
A/c Type Saving
Balance 4000

name Rasmita Panda


A/c. No 17720
A/c Type Saving
Balance 5000

name Rajesh Kumar Mohanty


A/c. No 17721
A/c Type Saving
Balance 5000

NAME OF STUDENT ENROLLMENT NUMBER Page 14


C++ & Object Oriented Programming Lab Manual

3. Write a C++ program to compute area of right angle triangle, equilateral triangle
,scalene triangle using function overloading concept.
Pseudo code:
area(a,b)
begin
return 0.5*a*b
end
area( a)
begin
return sqrt(3)/4)*a*a
end
area( a,b,c)
begin
float s
set s=(float)(a+b+c)/2
return sqrt s*(s-a)*(s-b)*(s-c)
end

Output
area of scalene triangle sqrt 8.4375
area of equilateral triangle
area of right angled triangle 4.375

NAME OF STUDENT ENROLLMENT NUMBER Page 15


C++ & Object Oriented Programming Lab Manual

VIVA QUESTIONS
1. 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.

NAME OF STUDENT ENROLLMENT NUMBER Page 16


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:

class1

value1

indata( int a)
display(void)
exchange (class1 &,class2
&)

Class2

Value2

indata( a)
display(void)
exchange (class1 &,class2
&)

exchange ( &x, &y)


begin
set temp=x.value1;
set x.value1=y.value2;
set y.value2=temp;
end
Output
values before exchange:
value1=100
value2=200
values after exchange :

NAME OF STUDENT ENROLLMENT NUMBER Page 17


C++ & Object Oriented Programming Lab Manual

value1=200
value2=100

NAME OF STUDENT ENROLLMENT NUMBER Page 18


C++ & Object Oriented Programming Lab Manual

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:

complex

x
y

Complex()
complex( real, imag)
complex sum( complex , complex)
show(complex )

complex()
begin
set x=0
set y=0
end
complex( real, imag)
begin
set x=real
set y=imag
end

sum( c1, c2)


begin
set complex c3
set c3.x=c1.x+c2.x
set c3.y=c1.y+c2.y
return c3
end
show( c)
begin
print c.x
print c.y

NAME OF STUDENT ENROLLMENT NUMBER Page 19


C++ & Object Oriented Programming Lab Manual

end
Output
Set 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

NAME OF STUDENT ENROLLMENT NUMBER Page 20


C++ & Object Oriented Programming Lab Manual

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
5. Write some properties of friend functions.

NAME OF STUDENT ENROLLMENT NUMBER Page 21


C++ & Object Oriented Programming Lab Manual

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

String

*s
L

string()
string(char *s1)
void show()
string operator+( &x, &y)
operator==( &x, &y)

string()
begin
set s=0
set l=0
end
string(char *s1)
begin
set l=strlen(s1)
set s=new char[l+1]
strcpy(s,s1)
end
show()
begin
print s
end
operator==( &x,&y)
begin
if(strcmp(x.s,y.s)==0)
return 1
else

NAME OF STUDENT ENROLLMENT NUMBER Page 22


C++ & Object Oriented Programming Lab Manual

return 0
end if
end
operator +( &x,&y)
begin
set string z
set z.l=x.l+y.l
set z.s=new char[z.l+1]
strcpy(z.s,x.s)
strcat(z.s,y.s)
return z
end
Output
Set the value of string through parameterized constructor
P=GEC
q=BBSR
Calling to overload operator ==

both strings are not equal

Calling to overload operator +


GECBBSR

NAME OF STUDENT ENROLLMENT NUMBER Page 23


C++ & Object Oriented Programming Lab Manual

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


Pseudo code:

Abc

m,n

abc()
void show()
operator++()

abc()
begin
set m=8
set n=9
end
void show()
begin
print m,n
end
operator++()
begin
set m=m+1
set n=n+1
end
Output
Set the value of m and n through default constructor
m=8 n=9
call to show function
8 9
calling to overload operator ++
9 10

NAME OF STUDENT ENROLLMENT NUMBER Page 24


C++ & Object Oriented Programming Lab Manual

VIVA QUESTIONS
1. 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?

NAME OF STUDENT ENROLLMENT NUMBER Page 25


C++ & Object Oriented Programming Lab Manual

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:

rectangular

x,y

rectangular()
polar(rectangular rect)
operator rectangular()
void input()
void output()

rectangular()
begin
set x=0
set y=0
end
rectangular(a,b)
begin
Set x=a
Set y=b
end
double getx()
begin
return x
end
double gety()
begin
return y
end
void input()
begin
print “ENTER THE COORDINATES"

NAME OF STUDENT ENROLLMENT NUMBER Page 26


C++ & Object Oriented Programming Lab Manual

input x,y
end
void output()
begin
print x
print y
end

Polar

theta,r

polar ()
polar(rectangular rect)
operator rectangular()
void input()
void output()

polar ()
begin
set theta=0
set r=0
end
polar(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
end
operator rectangular()
begin
set x,y
set atheta=theta*pi/180
set x=r*cos(atheta)

NAME OF STUDENT ENROLLMENT NUMBER Page 27


C++ & Object Oriented Programming Lab Manual

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 r
input theta
end
Output
-----------
CONVERSION
-----------
2.POLAR TO RECTANGULAR:
ENTER THE COORDINATES 2.0 9.0
rectangular coordinates are:(0.0,20)

NAME OF STUDENT ENROLLMENT NUMBER Page 28


C++ & Object Oriented Programming Lab Manual

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


concept.
Pseudo code:

Matrix

a[100][100], m,n

void getdata()
void show()
matrix operator+(matrix &x,matrix &y)

getdata()
begin
set i,j;
print"enter m,n:"
input m,n

for i=0 to m by 1
do
for j=0 to n by 1
do
print "enter a number:"
input a[i][j]
end for
end for
end

show()
begin
int i,j;
for i=0 to m by 1
do
for j=0 to n by 1
do

NAME OF STUDENT ENROLLMENT NUMBER Page 29


C++ & Object Oriented Programming Lab Manual

print a[i][j]
end for
end for
end

operator+(&x,&y)
begin
int i,j
matrix z
for i=0 to m by 1
do
for j=0 to n by 1
do
z.a[i][j]=z.a[i][j]+x.a[i][j]*y.a[i][j]
end for
end for

end
Output
enter m,n:2 2
FOR p OBJECT
------------------
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

NAME OF STUDENT ENROLLMENT NUMBER Page 30


C++ & Object Oriented Programming Lab Manual

Calling to overload operator +


7 13
20 25

NAME OF STUDENT ENROLLMENT NUMBER Page 31


C++ & Object Oriented Programming Lab Manual

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.

NAME OF STUDENT ENROLLMENT NUMBER Page 32


C++ & Object Oriented Programming Lab Manual

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

Area Perimeter

area(float l,float b) float peri(float l,float


b)

Rectangle

length, breadth

void get_data( )
void calc()

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

NAME OF STUDENT ENROLLMENT NUMBER Page 33


C++ & Object Oriented Programming Lab Manual

print"Enter breadth: "


input breadth
end
calc()
begin
print area(length,breadth)
print peri(length,breadth)
end
Output
Enter length: 5
Enter breadth:7
area=35
perimeter=24

NAME OF STUDENT ENROLLMENT NUMBER Page 34


C++ & Object Oriented Programming Lab Manual

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: student

rno;
nm[30];

get_n
put_n

test sports

part1 score
part2

get_m (int x, int y) void get_s (int a)


put.m (void)

result

int total
float avg

void show (void)

STUDENT CLASS
get_n ( a, *n)
begin
set rno = a
strcpy(nm,n)
end

NAME OF STUDENT ENROLLMENT NUMBER Page 35


C++ & Object Oriented Programming Lab Manual

put_n (void)
begin
print rno,nm
end
TEST CLASS
get_m ( x, y)
begin
Set part1= x
Set part2=y
end

put.m (void)
begin
print part1,part2
end

SPORTS CLASS
get_s (a)
begin
set score = a
end
put_s (void)
begin
print score
end
RESULT CLASS
show (void)
begin
set total = part1 + part2 + score
set avg=(float)(total/3)
put_n ( )
put_m ( )

NAME OF STUDENT ENROLLMENT NUMBER Page 36


C++ & Object Oriented Programming Lab Manual

put_s ( )
print total
print avg
end
Output:
Roll No.12 name Satya
marks obtained:
part1 = 30
part2 = 35
sports wt.: 7
total score= 72
avg score= 24.0

NAME OF STUDENT ENROLLMENT NUMBER Page 37


C++ & Object Oriented Programming Lab Manual

VIVA 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?

NAME OF STUDENT ENROLLMENT NUMBER Page 38


C++ & Object Oriented Programming Lab Manual

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.
Pseudo code:

shape

x,y

getxy()
display_area()

triangle rectangle

getz() display_area()
display_area()

CLASS SHAPE
getxy()
begin
print"enter x,y:"
input x,y
end

NAME OF STUDENT ENROLLMENT NUMBER Page 39


C++ & Object Oriented Programming Lab Manual

display_area()
begin
print"displaying area of shape..."
end
CLASS TRIANGLE
getz()
begin
print "enter z:"
input z
end
display_area()
begin
set double s
set s=(x+y+z)/2
print sqrt s*(s-x)*(s-y)*(s-z)
end
CLASS RECTANGLE
display_area()
begin
print "area of rectangle="
print x*y
end

Output:
enter option 1.triangle 2.rectangle
option: 1
enter x,y: 5 7
enter z:9
area of traingle=17.41
option : 2
enter x,y: 5 6
area of rectangle=3

NAME OF STUDENT ENROLLMENT NUMBER Page 40


C++ & Object Oriented Programming Lab Manual

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

shape

x,y

shape(int x1,int y1)


display_area()

Triangle rectangle

triangle(x1,y1,z1) rectangle(x1,y1)
display_area() display_area()

CLASS SHAPE
shape(x1,y1)
begin
set x=x1
set y=y1
end
display_area()
begin
print"displaying area of shape..."
end

NAME OF STUDENT ENROLLMENT NUMBER Page 41


C++ & Object Oriented Programming Lab Manual

CLASS TRIANGLE
triangle(x1,y1,z1):shape(x1,y1)
begin
set z=z1
end

display_area()
begin
double s
set s=(x+y+z)/2
print "area of traingle="
print sqrt(s*(s-x)*(s-y)*(s-z))
end
CLASS RECTANGLE
rectangle(x1,y1):shape(x1,y1)
begin
end
display_area()
begin
Print "area of rectangle="
Print x*y
end

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

NAME OF STUDENT ENROLLMENT NUMBER Page 42


C++ & Object Oriented Programming Lab Manual

VIVA QUESTIONS
1. What is meant by Abstract base class?
2. Define Virtual Functions
3. Define an abstract class.
4. What is a virtual function?
5. What is a pure virtual function?

NAME OF STUDENT ENROLLMENT NUMBER Page 43


C++ & Object Oriented Programming Lab Manual

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)
begin
try
begin
if (a<0)
throw a
else
print sqrt(a)
end try
catch(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

NAME OF STUDENT ENROLLMENT NUMBER Page 44


C++ & Object Oriented Programming Lab Manual

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


element references
Pseudo code:
array

a[max]

operator[](int i)

operator []( i)
begin
if (i<0 || i>=max)
throw i

else
return 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…

NAME OF STUDENT ENROLLMENT NUMBER Page 45


C++ & Object Oriented Programming Lab Manual

VIVA QUESTION
1. Define Exception Handling
2. What are the type of Error?
3. Define try and catch.
4. What are the type of Error?

NAME OF STUDENT ENROLLMENT NUMBER Page 46


C++ & Object Oriented Programming Lab Manual

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
else
return b
end

Output:
enter two characters: a c
c
enter a,b:5 7
7
enter p,q:5.2 7.5
7.5

NAME OF STUDENT ENROLLMENT NUMBER Page 47


C++ & Object Oriented Programming Lab Manual

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
Pseudo code:

array

T *a;
int n;

void getdata()
void putdata()
void sort( )

getdata()
begin
int i
print ”enter how many no:”
input n
set a=new T[n]
for i=0 to n by 1
do
print”enter a number:”
input a[i]
end for
end
putdata()
begin
for i=0 to n by 1
do
Print a[i]
end for

NAME OF STUDENT ENROLLMENT NUMBER Page 48


C++ & Object Oriented Programming Lab Manual

end
sort( )
begin
T k;
int i,j;
for i=0 to n by 1
do
for j=0 to n by 1
do
if (a[i]>a[j])
do
set k=a[i]
set a[i]=a[j]
set a[j]=k
end if
end for
end for
end
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

NAME OF STUDENT ENROLLMENT NUMBER Page 49


C++ & Object Oriented Programming Lab Manual

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?

NAME OF STUDENT ENROLLMENT NUMBER Page 50


C++ & Object Oriented Programming Lab Manual

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
Pseudo code:

matrix

**a
m,n

matrix()
void getdata()
void show()
~matrix()
mul(matrix &x,matrix &y)

matrix()
begin
set i
print "enter m,n:"
input m,n
set a=new int*[m]
for i=0 to m by 1
set a[i]=new int[n]
end

getdata()
begin
set i,j;

NAME OF STUDENT ENROLLMENT NUMBER Page 51


C++ & Object Oriented Programming Lab Manual

for i=0 to m by 1
do
for j=0 to n by 1
do
print "enter a number:"
input a[i][j]
end for
end for
end

show()
begin
set i,j
for i=0 to m by 1
do
for j=0 to n by 1
do
print a[i][j]
end for
end for
end

~matrix()
begin
int i;
for i=0 to m by 1
delete a[i]
delete a
end

matrix mul(matrix &x,matrix &y)


begin
int i,j,k

NAME OF STUDENT ENROLLMENT NUMBER Page 52


C++ & Object Oriented Programming Lab Manual

matrix z
for i=0 to m by 1
do
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 z
end

Output:
enter m,n:2 2
Enter the matrix of size2x2
23
23
Enter the matrix of size2x2
45
45
The matrix for object p:
23
23
The matrix for object q:
45
45
The resultant matrix
20 25
20 25

NAME OF STUDENT ENROLLMENT NUMBER Page 53


C++ & Object Oriented Programming Lab Manual

2. Modify the above program as follow


a. Use operator*() for matrix multiplication instead of mul()
b. Make operator*() as friend function

Pseudo code:

matrix

**a;
m,n;

matrix()
void getdata()
void show()
~matrix()
operator*(matrix &x,matrix &y)

matrix()
begin
int i
print"enter m,n:"
input m,n
set a=new int*[m]
for i=0 to m by 1
set a[i]=new int[n]
end
getdata()
begin
int i,j
for i=0 to m by 1
do
for j=0 to n by 1
do
print"enter a number:"

NAME OF STUDENT ENROLLMENT NUMBER Page 54


C++ & Object Oriented Programming Lab Manual

input a[i][j]
end for
End for
end
show()
begin
int i,j
for i=0 to m by 1
do
for j=0 to n by 1

do
print a[i][j]
end for

end for
end
~matrix()
begin
int i;
for i=0 to m by 1
delete a[i]
delete a
end
matrix operator*(&x,&y)
begin
int i,j,k
matrix z
for i=0 tox. m by 1
do
forj=0toy.n by 1
do
set z.a[i][j]=0

NAME OF STUDENT ENROLLMENT NUMBER Page 55


C++ & Object Oriented Programming Lab Manual

for k=0 to x.m by 1


do
set z.a[i][j]=z.a[i][j]+x.a[i][k]*y.a[k][j]
end for
end for
end for
return end

Output:
enter m,n:2 2
Enter the matrix of size2x2
23
23
Enter the matrix of size2x2
45
45
The matrix for object p:
23
23
The matrix for object q:
45
45
Calling to overload *
20 25
20 25

NAME OF STUDENT ENROLLMENT NUMBER Page 56


C++ & Object Oriented Programming Lab Manual

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();
end
Output:
Enter File name: one.txt
Enter contents to store in file (enter # at end)

NAME OF STUDENT ENROLLMENT NUMBER Page 57


C++ & Object Oriented Programming Lab Manual

4.Program for read the content of a file.


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

NAME OF STUDENT ENROLLMENT NUMBER Page 58


C++ & Object Oriented Programming Lab Manual

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?

NAME OF STUDENT ENROLLMENT NUMBER Page 59

Вам также может понравиться