You are on page 1of 37

Lab Manual

For
“OBJECT ORIENTED PROGRAMMING USING C++”
[BCA303P]

III Semester BCA

Ms. Sneha V K, Assistant Professor

K. L. E. Society’s Degree College


(Affiliated to Bangalore University)
Department of Computer Applications
II Stage, III Block, Nagarbhavi, Bengaluru-560072
2019
CONTENTS
Sr. No Programs List (PART-A) Page No

1. Write a C++ program to prepare a shopping list. 1-2

2. Write a C++ program to perform bank transactions. 3-6

Write a C++ program to swap numbers using friend


3. 7-8
function.

Write a C++ program to calculate area and


4. 9
circumference of circle using inline function.

Write a C++ program to perform multiplication of two


5. 10 - 11
matrices using operator overloading.

Write a C++ program to implement operations on


6. 12 - 13
Queue.

Write a C++ program to create a student report using


7. 14 - 16
inheritance technique.

Write a C++ program to find the area and volume of


8. 17 - 18
respective figures using function overloading.

Write a C++ program to show returning current object,


9. accessing member data of current object and returning 19 - 20
values of object using this pointer.

10. Write a C++ program to sort elements using template. 21 - 22


Sr. No Programs List (PART-B) Page No

Write a C++ program to print star in right angled


1. 23
triangle.

Write a C++ program to add two complex number


2. 24 - 25
using constructor overloading.
Write a C++ program to compare two distances using
3. comparison operator overloading. 26 - 27

Write a C++ program to find largest and smallest


4. number in an array using friend function. 28

Write a C++ program to convert a value from basic


5. 29
type to class type.

Write a C++ program to perform addition of two


6. 30 - 31
matrices using operator overloading

Write a C++ program to demonstrate the working of


7. 32
single inheritance.

Write a C++ program to calculate simple interest which


consist of data members principle, rate, time and
8. 33
member function getdata(), display() and calculate
using scope resolution operator.

Write a C++ program that takes two command line


9. 34
arguments and perform addition operation.

Write a C++ program to perform arithmetic operations


10. 35
on integer and float values using class templates.
Object Oriented Programming using C++ [BCA303P]

PART-A
1) Write a C ++ Program to prepare a shopping list.
Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
const size =2;
class shopping
{
private:
char name[25];
intqty;
float rate;
public:
int amount;
void getdata()
{
cout<<endl<<"\nEnter the item name:";
cin>>name;
cout<<endl<<"Enter the rate:";
cin>>rate;
cout<<endl<<"Enter the quantity:\n";
cin>>qty;
}
void printdata()
{
cout<<name;
cout<<"\t\t"<<rate;
cout<<"\t\t"<<qty;
amount=rate * qty;
cout<<"\t\t"<<amount<<endl;
}
};
void main()
{
shopping item[size];
int total=0;
clrscr();
cout<<"\n Enter your shopping details\n";
for(inti=0;i<=size;i++)
item[i].getdata();
cout<<"\n Hello your shopping list is as follows\n";
for(i=0;i<=size;i++)
{

KLE Society's Degree College[BCA] Page 1


Object Oriented Programming using C++ [BCA303P]

item[i].printdata();
total=total+item[i].amount;
}
cout<<"\n Total\t\t"<<total;
getch();
}

Input:

Output:

KLE Society's Degree College[BCA] Page 2


Object Oriented Programming using C++ [BCA303P]

2) Write a C ++ Program to perform bank transactions.


Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
class bank
{
char name[25];
intaccno;
char acctype[40];
float balance;
public:
void init();
void deposit();
void withdraw();
void disp_det();
};
void bank::deposit()
{
float dep;
cout<<"Deposit\n";
cout<<"Enter the amount to be deposited:";
cin>>dep;
balance+=dep;
}
void bank::withdraw()
{
float amt;
cout<<"Withdraw\n";
cout<<"Enter the amount to withdraw\n";
cin>>amt;
balance-=amt;
}
void bank::init()
{
cout<<"\n New Account";
cout<<"\n Enter the Name of the Depositor:";
cin>>name;
cout<<"\n Enter the account number:";
cin>>accno;
cout<<"\n Enter the account type:";
cin>>acctype;
cout<<"Enter the amountto be deposited:";
cin>>balance;

KLE Society's Degree College[BCA] Page 3


Object Oriented Programming using C++ [BCA303P]

}
void bank::disp_det()
{
cout<<"\n Account Details\n";
cout<<"\n Name of the Depositor:"<<name<<endl;
cout<<"\n Account Number:"<<accno<<endl;
cout<<"\n Account Type:"<<acctype<<endl;
cout<<"\n Balance Amount:"<<balance<<endl;
}
void main()
{
clrscr();
bank obj;
int choice;
while(choice!=0)
{
cout<<"\n 1.New Account\n 2.Deposit\n 3.Withdraw\n 4. Display Account Status\n 0
to exit\n";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 0: cout<<"Exiting.. Program\n";
exit(0);
break;
case 1:obj.init();
break;
case 2:obj.deposit();
break;
case 3:obj.withdraw();
break;
case 4:obj.disp_det();
break;
default:cout<<"Choose Correct Option";
}
}
getch();
}
Input:

Step1: Choose choice 1 and create new account by giving:


i) Name ii) Account Number iii) Account Type(Savings/Current/FD/RD)
iv)Amount to be Deposited
Step 2: After creating new account choose choice 4 to display the details / Account
status

KLE Society's Degree College[BCA] Page 4


Object Oriented Programming using C++ [BCA303P]

Step 3: In order to deposit amount, choose choice 2 and enter the amount to be
deposited.
Step 4: Choose choice 4 in order to view account details
(Amount will be added with existing amount)
Step 5: Choose choice 3 to withdraw amount and add amount to be withdrew
Step 6: Choose choice 4 to view the account status after withdrawing.
(Entered amount will be deducted from existing amount)
Step 7: Choose choice 0 to exit from the output console screen to program.

Output:
Step 1:

Step 2:

Step 3:

KLE Society's Degree College[BCA] Page 5


Object Oriented Programming using C++ [BCA303P]

Step 4:

Step 5:

Step 6:

KLE Society's Degree College[BCA] Page 6


Object Oriented Programming using C++ [BCA303P]

3) Write a C ++ Program to swap two numbers using friend function.


Program:
#include<iostream.h>
#include<conio.h>
class swapping
{
clrscr();
private: int x;int y;
public:
void setdata(inta,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"\n x="<<x<<"\t y="<<y;
}
friend void swap(swapping &);
};
void swap(swapping &s)
{
int temp;
temp=s.x;
s.x=s.y;
s.y=temp;
}
void main()
{
swapping s;
int x1,x2;
clrscr();
cout<<"\n Enter the first number:";
cin>>x1;
cout<<"\n Enter the second number:";
cin>>x2;
s.setdata(x1,x2);
cout<<"\n Before Swapping";
s.showdata();
swap(s);
cout<<"\n After Swapping";
s.showdata();
getch();
}

KLE Society's Degree College[BCA] Page 7


Object Oriented Programming using C++ [BCA303P]

Input:

Step 1:Enter two integer values

Output:

KLE Society's Degree College[BCA] Page 8


Object Oriented Programming using C++ [BCA303P]

4) Write a C ++ Program to calculate area and circumference of circle


using inline function.
Program:
#include<iostream.h>
#include<conio.h>
const float pi=3.14159;
inline float circum(float x)
{
return (2*pi*x);
}
inline float area(float x)
{
return(pi*x*x);
}
void main()
{
float r;
clrscr();
cout<<"\n Enter the radius of a circle:";
cin>>r;
cout<<"\n Circumference ="<<circum(r);
cout<<"\n\n Area="<<area(r);
getch();
}
Input:

Step 1:Enter radius value for circle

Output:

KLE Society's Degree College[BCA] Page 9


Object Oriented Programming using C++ [BCA303P]

5) Write a C ++ Program to perform multiplication of two matrices


using operator overloading.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class matrix
{
int a[10][10],m,n;
public:void inputmatrix();
void outputmatrix();
matrix operator * (matrix x);
};
void matrix::inputmatrix()
{
int i,j;
cout<<"Enter order of matrix:";
cin>>m>>n;
cout<<"Enter Matrix elements:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void matrix::outputmatrix()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
matrix matrix::operator * (matrix x)
{
matrix c;
int i,j,k;
if(n!=x.m)
{
cout<<"Multiplicatioon is not possible\n";
exit(0);
}
KLE Society's Degree College[BCA] Page 10
Object Oriented Programming using C++ [BCA303P]

else
{
c.m=m;
c.n=x.n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c.a[i][j]=0;
for(k=0;k<m;k++)
c.a[i][j]=a[i][k]*x.a[k][j];
}
}
return c;
}
int main()
{
matrix A,B,C;
clrscr();
cout<<"\n Enter the matrix order and elements "<<endl;
A.inputmatrix();
cout<<"\n Enter the matrix order and elements "<<endl;
B.inputmatrix();
C=A*B;
cout<<"Product of Matrix A and B is:"<<endl;
C.outputmatrix();
getch();
return 0;
}
Input:
Step 1: Enter order and elements of Matrix A and B respectively.

Output:

KLE Society's Degree College[BCA] Page 11


Object Oriented Programming using C++ [BCA303P]

6) Write a C ++ Program to implement operations on Queue


Program:
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main()
{
int ch;
clrscr();
cout<<"\n 1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;

KLE Society's Degree College[BCA] Page 12


Object Oriented Programming using C++ [BCA303P]

cout<<"4) Exit"<<endl;
while(ch!=4)
{
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
}
return 0;
}

Input:
Step 1: Enter Choice 1 and insert the element
Step 2: Display all the inserted elements by entering choice 3
Step 3: Enter choice 2 to delete element
Step 4: Display the existing list after deletion by entering choice 3 again
Step 5: Enter choice 4 to exit the output console screen.

Output:

KLE Society's Degree College[BCA] Page 13


Object Oriented Programming using C++ [BCA303P]

7) Write a C ++ Program to create a student report using inheritance


technique.
Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class student
{
protected:
char name[100];
int regno;
char college[30];
char city[20];
public:
void getdata()
{
cout<<"\n Enter regno:";
cin>>regno;
cout<<"\n Enter student name:";
cin>>name;
cout<<"\n Enter college name:";
cin>>college;
cout<<"\n Enter city:";
cin>>city;
}
void showdata()
{
cout<<"\n Regno:"<<regno;
cout<<"\n Name:"<<name;
cout<<"\n College :"<<college;
cout<<"\n City:"<<city;
}
};
class marks:public student
{
protected:
float s1,s2,s3;
float percen;
public:
void getmarks()
{
cout<<"\n Enter marks";
cout<<"\n Subject 1:";
cin>>s1;
cout<<"\n Subject 2:";
KLE Society's Degree College[BCA] Page 14
Object Oriented Programming using C++ [BCA303P]

cin>>s2;
cout<<"\n Subject 3:";
cin>>s3;
percen=((s1+s2+s3)/300)*100;
}
void showmarks()
{
cout<<"\n Subject1="<<s1;
cout<<"\n Subject2="<<s2;
cout<<"\n Subject3="<<s3;
cout<<"\n Percentage="<<percen;
}
};
class result:public marks
{
private:
char grade;
public:
void display()
{
showdata();
showmarks();
cout<<"\n Grade="<<getgrade();
}
char getgrade()
{
if(s1<35||s2<35||s3<35)
grade='F';
else if(percen>=75)
grade='A';
else if(percen>=60)
grade='B';
else if(percen>=50)
grade='C';
else if(percen>=35)
grade='D';
return grade;
}
};
void main()
{
clrscr();
result stud;
stud.getdata();
stud.getmarks();

KLE Society's Degree College[BCA] Page 15


Object Oriented Programming using C++ [BCA303P]

stud.display();
getch();
}

Input:
Step 1: Enter details of the student and marks of respective subjects.
Step 2: If any one subject marks is less than 35 result will be Fail

Output:

KLE Society's Degree College[BCA] Page 16


Object Oriented Programming using C++ [BCA303P]

8) Write a C ++ Program to find the area and volume of respective


figures using function overloading.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void area(float b,float h)
{
float ar,vol;
ar=0.5*b*h;
vol=ar*h;
cout<<"\n Area of triangle:"<<ar<<endl;
cout<<" Volume of triangle:"<<vol<<endl;
}
void area(float l)
{
float ar,vol;
ar=l*l;
vol=l*l*l;
cout<<"\n Area of Square:"<<ar<<endl;
cout<<" Volume of Square:"<<vol<<endl;
}
void area(float l,float b, float w)
{
float ar,vol;
ar=l*b;
vol=l*b*w;
cout<<"\n Area of rectangle:"<<ar<<endl;
cout<<"Volume of rectangle:"<<vol<<endl;
}
void main()
{
float b,h,r,l,w;
int ch;
clrscr();
while(1)
{
cout<<"-------------------------------------------\n"<<endl;
cout<<"1> Triangle "<<endl;
cout<<"2> Square"<<endl;
cout<<"3> Rectangle"<<endl;
cout<<"4> Exit"<<endl;
cout<<"-------------------------------------------\n"<<endl;
cout<<"Enter your choice:";
cin>>ch;
KLE Society's Degree College[BCA] Page 17
Object Oriented Programming using C++ [BCA303P]

switch(ch)
{
case 1:cout<<"\n Enter base and height of triangle:";
cin>>b>>h;
area(b,h);
break;
case 2:cout<<"\n Enter the side of square:";
cin>>l;
area(l);
break;
case 3:cout<<"\n Enter the length,breadth and width of rectangle:";
cin>>l>>b>>w;
break;
case 4: exit(0);
default : cout<<"Choose right option";
}
getch();
}
}

Input:
Step 1: Enter respective measurement for the selected shape.
Step 2: Area and volume of the selected shape will be displayed.

Output:

KLE Society's Degree College[BCA] Page 18


Object Oriented Programming using C++ [BCA303P]

9) Write a C ++ Program to show returning of current object, accessing


member data of current object and returning values of object using this
pointer:
Program:
#include<iostream.h>
#include<conio.h>
class myclass
{
int data1;
int data2;
public:myclass(int data1, int data2)
{
this->data1=data1;
this->data2=data2;
}
myclass &assign()
{
this->data1=300;
this->data2=400;
return *this;
}
void print()
{
cout<<"Data1="<<data1<<endl;
cout<<"Data2="<<data2<<endl;
}
}; Output:
void main()
{
clrscr();
myclass obj1(100,200);
cout<<"Object1 data"<<endl;
obj1.print();
myclass obj2 = obj1.assign();
cout<<"Object2 data"<<endl;
obj2.print();
getch();
}

KLE Society's Degree College[BCA] Page 19


Object Oriented Programming using C++ [BCA303P]

10) Write a C ++ Program to sort elements using templates.


Program:
include<iostream.h>
#include<conio.h>
template<class T>
void bubble(T a[],int n)
{
for(int i=0;i<n;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template<class X>
void swap(X &a,X &b)
{
X temp=a;
a=b;
b=temp;
}
void main()
{
int x[5]={10,30,40,20,50};
float y[5]={1.1,3.3,4.4,2.2,5.5};
char a[5]={'e','i','o','a','u'};
clrscr();
bubble(x,5);
bubble(y,5);
bubble(a,5);
cout<<"\n Sorted Integer Array:\t";
for(int i=0;i<5;i++)
cout<<x[i]<<"\t";
cout<<endl;
cout<<"\n Sorted Float Array:\t";
for(int j=0;j<5;j++)
cout<<y[j]<<"\t";
cout<<endl;
cout<<"\n Sorted Charecter Array:\t";
for(int k=0;k<5;k++)
cout<<a[k]<<"\t";
cout<<endl;
getch();
}
void main()

KLE Society's Degree College[BCA] Page 20


Object Oriented Programming using C++ [BCA303P]

{
int x[5]={10,30,40,20,50};
float y[5]={1.1,3.3,4.4,2.2,5.5};
char a[5]={'e','i','o','a','u'};
clrscr();
bubble(x,5);
bubble(y,5);
bubble(a,5);
cout<<"\n Sorted Integer Array:\t";
for(int i=0;i<5;i++)
cout<<x[i]<<"\t";
cout<<endl;
cout<<"\n Sorted Float Array:\t";
for(int j=0;j<5;j++)
cout<<y[j]<<"\t";
cout<<endl;
cout<<"\n Sorted Charecter Array:\t";
for(int k=0;k<5;k++)
cout<<a[k]<<"\t";
cout<<endl;
getch(); Output:
}

KLE Society's Degree College[BCA] Page 21


Object Oriented Programming using C++ [BCA303P]

PART-B

1) Write a C ++ Program to print “*” in right angled triangle form.


Program:
#include<iostream.h>
#include<conio.h>
void print_triangle();
void main()
{
clrscr();
print_triangle();
}
void print_triangle()
{
int n;
cout<<"Enter a value for n:";
cin>>n;
for(int i=0;i<=n;i++)
{
for(int j=0;j<i;j++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}

Input:
Step 1: Enter number of stars to be displayed in right angled triangle form.

Output:

KLE Society's Degree College[BCA] Page 22


Object Oriented Programming using C++ [BCA303P]

2) Write a C ++ Program to add two complex number using constructor


overloading.
Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public:
complex()
{
}
complex(int r)
{
real=r;
imag=r;
}
complex(int r, int i)
{
real=r;
imag=i;
}
complex(complex &c)
{
real=c.real;
imag=c.imag;
}
void print()
{
cout<<"\n The sum of two complex numbers is:"<<real<<"+"<<imag<<"i";
}
friend complex sum(complex,complex);
};
complex sum(complex obj1, complex obj2)
{
complex obj3;
obj3.real=obj1.real+obj2.real;
obj3.imag=obj1.imag+obj2.imag;
return obj3;
}
void main()
{
clrscr();
int a,b,c;

KLE Society's Degree College[BCA] Page 23


Object Oriented Programming using C++ [BCA303P]

complex c1;
cout<<"\n For equal values";
cout<<"\n Enter the equal value for real and imaginary part of number 1:\n\t";
cin>>a;
complex c2(a);
cout<<"\n For different values";
cout<<"\n Enter the real and imaginary part of number 2:\n \t ";
cin>>b>>c;
complex c3(b,c);
complex c4=sum(c2,c3);
c4.print();
getch();
}

Input:
Step 1: Enter a value for number 1
Step 2: Enter two values for number 2

Output:

KLE Society's Degree College[BCA] Page 24


Object Oriented Programming using C++ [BCA303P]

3) Write a C ++ program to compare two distances using comparison


operator overloading.
Program:
#include<iostream.h>
#include<conio.h>
class distance
{
private:int feet; float inches;
public:distance()
{
feet=0;inches=0.0;
}
distance(int ft,float inch)
{
feet=ft;
inches=inch;
}
void getdist()
{
cout<<"\n Enter feet:";
cin>>feet;
cout<<"\n Enter inches:";
cin>>inches;
}
void showdist()
{
cout<<feet<<"\'-"<<inches<<"\''";
}
int distance::operator<(distance);
};
int distance :: operator<(distance d2)
{
float f1=feet+inches/12;
float f2=d2.feet+d2.inches/12;
return(f1<f2)?1:0;
}
void main()
{
clrscr();
distance dist1;
dist1.getdist();
distance dist2(6,2.5);
cout<<"\n Distance 1:\t";
dist1.showdist();

KLE Society's Degree College[BCA] Page 25


Object Oriented Programming using C++ [BCA303P]

cout<<"\n Distance 2:\t";


dist2.showdist();
if(dist1<dist2)
cout<<"\t Distance 1 is less than Distance 2";
else
cout<<"\t Distance 2 is less than Distance 1";
getch();
}
Input:

Step 1: Enter a values for feet and inch respectively.

Output:

KLE Society's Degree College[BCA] Page 26


Object Oriented Programming using C++ [BCA303P]

4) Write a C ++ program to find largest and smallest number in an


array using friend function.
Program:
#include<iostream.h>
#include<conio.h>
class number
{
public:friend void finding(int n,int a[10])
};
void finding(int n,int a[10])
{
int small,large;
small=a[0];large=a[0];
for(int i=1;i<n;i++)
{
if(small>a[i])
small=a[i];
if(large<=a[i])
large=a[i];
}
cout<<"\n The largest element is:"<<large;
cout<<"\n The smallest element is:"<<small;
}
void main()
{
int n,a[10];
clrscr(); Input:
cout<<"\n Enter value for N:";
cin>>n; Step 1: Enter a value for array and its
cout<<"\n Enter array elements:\n"; elements.
for(int i=0;i<n;i++)
cin>>a[i]; Output:
finding(n,a);
getch();
}

KLE Society's Degree College[BCA] Page 27


Object Oriented Programming using C++ [BCA303P]

5) Write a C ++ program to convert a value to basic type to value type


Program:
#include<iostream.h>
#include<conio.h>
class time
{
int hrs,min;
public:
time(int);
void display();
};
time :: time(int t)
{
cout<<"\n Basic Type to Class type conversion\n";
hrs=t/60;
min=t%60;
}
void time :: display()
{
cout<< hrs<<" Hours"<<endl;
cout<< min<<" Minutes"<<endl;
}
void main()
{
clrscr();
int duration;
cout<<"\n Enter duration in minutes:";
cin>>duration;
time t1=duration;
t1.display();
getch();
}
Input:

Step 1: Enter a value in minutes

Output:

KLE Society's Degree College[BCA] Page 28


Object Oriented Programming using C++ [BCA303P]

6) Write a C ++ Program to perform addition of two matrices


using operator overloading.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class matrix
{
int a[10][10],m,n;
public:void inputmatrix();
void outputmatrix();
matrix operator + (matrix x);
};
void matrix::inputmatrix()
{
int i,j;
cout<<"Enter order of matrix:";
cin>>m>>n;
cout<<"Enter Matrix elements:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void matrix::outputmatrix()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
matrix matrix::operator + (matrix x)
{
matrix c;
int i,j,k;
if(n!=x.m)
{
cout<<"Addition is not possible\n";
exit(0);
}
KLE Society's Degree College[BCA] Page 29
Object Oriented Programming using C++ [BCA303P]

else
{
c.m=m;
c.n=x.n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c.a[i][j]=a[i][j]+x.a[i][j];
}
}
return c;
}
int main()
{
matrix A,B,C;
clrscr();
cout<<"\n Enter the matrix order and elements "<<endl;
A.inputmatrix();
cout<<"\n Enter the matrix order and elements "<<endl;
B.inputmatrix();
C=A+B;
cout<<"Sum of Matrix A and B is:"<<endl;
C.outputmatrix();
getch();
return 0;
}

Input:
Step 1: Enter order and elements of Matrix A and B respectively.

Output:

KLE Society's Degree College[BCA] Page 30


Object Oriented Programming using C++ [BCA303P]

7) Write a C ++ Program to demonstrate the working of single


inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class adddata
{
protected:
int num1,num2;
public:
void accept()
{
cout<<"\n Enter the first number:";
cin>>num1;
cout<<"\n Enter the second mumber:";
cin>>num2;
}
};
class addition:public adddata
{
int sum;
public:
void add()
{
sum=num1+num2;
}
void display()
{
cout<<"\n Addition of two numbers:"<<sum;
}
}; Input:
int main()
{ Step 1: Enter two values for addition
clrscr();
addition a; Output:
a.accept();
a.add();
a.display();
getch();
return 0;
}

KLE Society's Degree College[BCA] Page 31


Object Oriented Programming using C++ [BCA303P]

8) Write a C ++ Program to calculate simple interest which consist of


data members, principle, rate, time and member function getdata(),
display() and calculate using scope resolution operator.
Program:
#include<iostream.h>
#include<conio.h>
class simple_interest
{
float principle,rate,time;
public:
void getdata();
void display();
void calculate();
};
void simple_interest::getdata()
{
cout<<"Enter principle:"<<endl;
cin>>principle;
cout<<"Enter rate:"<<endl;
cin>>rate;
cout<<"Enter time:"<<endl;
cin>>time;
}
void simple_interest::display()
{
cout<<"The principle is:"<<principle<<endl;
cout<<"The rate is:"<<rate<<endl;
cout<<"The time is:"<<time<<endl;
Input:
}
void simple_interest::calculate()
Step 1: Enter values for P , T and R
{
float SI;
Output:
SI=(principle*time*rate)/100;
cout<<"The SI:"<<SI<<endl;
}
void main()
{
clrscr();
simple_interest s1;
s1.getdata();
s1.display();
s1.calculate();
getch();
}

KLE Society's Degree College[BCA] Page 32


Object Oriented Programming using C++ [BCA303P]

9) Write a C ++ Program that takes two command line arguments and


perform addition operation.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main(int argc ,char *argv[])
{
int a,b,sum;
if(argc!=3)
{
cout<<"ERROR";
exit (0);
}
a=atoi(argv[1]);
b=atoi(argv[2]);
sum=a+b;
cout<<"\n Addition of two numbers is:\n"<<sum;
return 0;
}

Input:
Step 1: Compile using Alt + F9 and run using Ctrl+F9.
Step 2: Go to file menu (press Alt+F)
Step 3: Select DOS Shell
Step 4: C:\TC\Source> program_filename.exe 10 5
Step 5: Press enter to view result

Output:

KLE Society's Degree College[BCA] Page 33


Object Oriented Programming using C++ [BCA303P]

10) Write a C ++ Program arithmetic operators on integer and float


values using class template.
Program:
#include<iostream.h>
#include<conio.h>
template <class t1, class t2>
void sum(t1 a, t2 b)
{
cout<<"Sum:"<<a+b<<endl;
cout<<"Difference:"<<a-b<<endl;
cout<<"Product:"<<a*b<<endl;
cout<<"Quotient:"<<a/b<<endl;
}
int main()
{
clrscr();
int a,b;
float x,y;
cout<<"\nEnter two integer values:";
cin>>a>>y;
cout<<"\n Enter two float values:";
cin>>x>>y;
sum(a,b);
sum(x,y);
getch();
return 0;
}
Input:

Step 1: Enter two integer values


Step 2: Enter two float values.

Output:

KLE Society's Degree College[BCA] Page 34

You might also like