Академический Документы
Профессиональный Документы
Культура Документы
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.
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
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 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:-"
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
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
A/c. No 17717
A/c Type Saving
Balance 5000
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
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.
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
&)
value1=200
value2=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:
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
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
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.
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
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 ==
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
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?
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"
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)
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)
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
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
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.
Experiment 6:
1. C++ Program to calculate the area and perimeter of rectangles using concept of
inheritance.
Pseudocode:
Area Perimeter
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
Pseudocode: student
rno;
nm[30];
get_n
put_n
test sports
part1 score
part2
result
int total
float avg
STUDENT CLASS
get_n ( a, *n)
begin
set rno = a
strcpy(nm,n)
end
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 ( )
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
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?
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
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
shape
x,y
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
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
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?
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
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…
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?
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
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
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
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?
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;
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 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
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:"
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
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
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?