0% found this document useful (0 votes)
91 views59 pages

Lab Manual

This document contains a lab manual for an Object Oriented Programming Using C++ course. The manual includes 20 programs divided into two parts (Part A and Part B) that students are expected to complete. Each program listing includes the program name, components used, aim of the program, code for the program, and an output section. This collection of programs is intended to teach students various object oriented programming concepts through hands-on programming exercises.

Uploaded by

Asus s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views59 pages

Lab Manual

This document contains a lab manual for an Object Oriented Programming Using C++ course. The manual includes 20 programs divided into two parts (Part A and Part B) that students are expected to complete. Each program listing includes the program name, components used, aim of the program, code for the program, and an output section. This collection of programs is intended to teach students various object oriented programming concepts through hands-on programming exercises.

Uploaded by

Asus s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

KRUPANIDHI DEGREE COLLEGE

KRUPANIDHI DEGREE COLLEGE


No.12/1,Chikkabellandur,Carmelaram Post,Varthur Hobli,Bangalore-560035

DEPARTMENT OF BCA
OBJECT ORIENTED PROGRAMMING
USING C++
LAB MANUAL
(BCA303T)
FOR THIRD SEMESTER

NAME……………………………………………

REG NO…………………………………………

SL PART - A PROGRAM LIST PAGE DOE SIGNATURE

Object Oriented Programming Using C++Page 1


KRUPANIDHI DEGREE COLLEGE

NO
NO.
.

1. Write a program to prepare a shopping lists. 1

2. Write a program to perform bank transactions. 4

Write a program to swap numbers using friend


3. 7
function.

Write a program to calculate area and


4. 9
circumference of circle using inline function

Write a program to perform multiplication of two


5. 11
matrices using operator overloading.

6. Write a program to implement operation on queue. 15

Write a program to create a student report using


7. 20
inheritance technique.

Write a Program to find the area and volume of


8. 24
respective figures using function overloading.

Write a program to show returning current object,


9. accessing member data of current object and 27
returning values of object using this pointer

10. Write a program to sort elements using template. 29

Object Oriented Programming Using C++Page 2


KRUPANIDHI DEGREE COLLEGE

SL PAGE DOE SIGNATURE


PART - B PROGRAM LIST
NO. NO.
Write a program to illustrate class with member
11. functions defined outside the class. 31

Write a program to check whether a given year


12. 33
is leap year using inline function

Write a program to find the roots of a quadratic


13. 35
equation using constructors

Write a program to add two distances given in


14. 38
feet and inches.
Write a program to find maximum of two
15. numbers using friend function. 41

Write a program to illustrate overloading of


16. binary operator +. 43

Write a program to demonstrate constructor


17. with default arguments. 46

18. Write a program to find the length of a string. 48

Write a program to perform addition of two


19. matrices using operator overloading. 50

Write a program to concatenate two string using


20. + operator. 53

Object Oriented Programming Using C++Page 3


KRUPANIDHI DEGREE COLLEGE

PART
–A
PRO
GRA
MS

Object Oriented Programming Using C++Page 4


KRUPANIDHI DEGREE COLLEGE

1) Write a program to prepare a shopping lists.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of class and object.

Procedure:

#include<iostream.h>

#include<conio.h>

class item

private:

char name[25];

float rate;

int qty;

float amount;

public:

void getdata()

cout<<”\n enter item name:”;

cin>> name;

cout<<”enter item quantity”;

cin>> qty;

cout<<”enter item rate:”;

cin>> rate;

cout<<”___________________________”<<endl;

void printdata()

Object Oriented Programming Using C++Page 5


KRUPANIDHI DEGREE COLLEGE

cout<<setw(7)<<name;

cout<<setw(8)<<qty;

cout<<setw(8)<<rate;

amount=rate*qty;

cout<<setw(8)<<amount<<endl;

};

void main()

clrscr();

item shop[10];

int n;

cout<<”\n No of items in the shoping list\n\n”;

cin>>n;

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

shop[i].getdata();

cout<<”\n details of shopping list\n\n”;

cout<<”name qty rate amount”<<endl;

cout<<”__________________________”<<endl;

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

shop[i].printdata();

cout<<”____________________________”<<endl;

getch();

Object Oriented Programming Using C++Page 6


KRUPANIDHI DEGREE COLLEGE

Output:

2) Write a program to perform bank transactions.

Components Used: Computer, TURBO C++

Object Oriented Programming Using C++Page 7


KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of class, object and data members.

Procedure:

#include<iostream.h>

#include<conio.h>

class current;

class saving

char name[15];

int accno;

float balance;

public:

void getdata()

cout<<”\n saving’s account details:\n”;

cout<<”\n name”;

cin>>name;

cout<<”account number:”;

cin>>accno;

cout<<”balance:”;

cin>>balance;

};

class current

private:

Object Oriented Programming Using C++Page 8


KRUPANIDHI DEGREE COLLEGE

char name[15];

int accno;

float balance;

public:

void getdata()

cout<<”\n current account details:\n;

cout<<”\name:”;

cin>>name;

cout<<”account number:”;

cin>>accno;

cout<<”balance:”;

cin>>balance;

friend float totbalance(saving,current);

};

float totbalance(saving sav, cuurent cur)

cout<<endl<<”*********************************”<<endl;

cout<<”\n saving’s account balance : rs.”<<setw(6)<<sav.balance<<endl;

cout<<”\n current account balance: rs.”<<setw(6)<<cur.balance<<endl;

void main()

saving s;

Object Oriented Programming Using C++Page 9


KRUPANIDHI DEGREE COLLEGE

current c;

clrscr();

s.getdata();

c.getdata();

cout<<”\n total balance: rs”<<setw(6)<<totbalance(s,c)<<endl;

cout<<endl<<”***************************************************<<”endl;

getch();

Output:

3) Write a program to swap numbers using friend function.

Components Used: Computer, TURBO C++

Object Oriented Programming Using C++Page 10


KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of friend function.

Procedure:

#include<iostream.h>

#include<conio.h>

class sample

private:

int x;

int y;

public:

void setdata(int a, int b)

x=a;

y=b;

void showdata()

cout<<”\nx=”<<x<<”y=”<<y;

friend void swap(sample &);passing object reference

};

void swap(sample& s)

int temp;b

temp=s.x;

Object Oriented Programming Using C++Page 11


KRUPANIDHI DEGREE COLLEGE

s.x=s.y; s.y=temp;

void main()

sample s;

int x1, x2;

clrscr();

cout<<”\n input 2 numbers:”;

cin>>x1>>x2;

s.setdata(x1,x2);

cout<<”\nbefore swapping”;

s.showdata();

swap(s);

cout<<”\n after swapping”;

s.showdata();

Output:

4) Write a program to find area and circumference of a circle using inline function.

Object Oriented Programming Using C++Page 12


KRUPANIDHI DEGREE COLLEGE

Components Used: Computer, TURBO C++

Aim: To introduce the concept of friend function.

Procedure:

#include<iostream.h>

#include<conio.h>

const pi=3.14;

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<<”\narea:”<<area(r);

getch();

Output:

Object Oriented Programming Using C++Page 13


KRUPANIDHI DEGREE COLLEGE

5) Write a program to perform multiplication of two matrices using operator


overloading.

Components Used: Computer, TURBO C++


Object Oriented Programming Using C++Page 14
KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of operator overloading.

Procedure:

#include<iostream.h>

#include<conio.h>

class matrix

private:

int mat[10][10];

int rc;

public:

matrix() {};

void size(int n)

rc=n;

void matinput();

void matprint();

matrix operator *(matrix);

};

void matrix:: matinput()

cout<<”\n type”<< rc*rc <<”elements:\n”;

for(int i=0;i<rc;i++)

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

cin>>mat[i][j];

Object Oriented Programming Using C++Page 15


KRUPANIDHI DEGREE COLLEGE

void matrix::matprint()

for(int i=0;i<rc;i++)

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

cout<<mat[i][j]<<setw(6);

cout<<”\n”;

matrix matrix :: operator *(matrix m)

matrix matprod;

matprod.size(rc);

for(int i=0; i<rc; i++)

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

matprod.mat[i][j]=0;

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

matprod.mat[i][j]=matprod.mat[i][j]+mat[i][k]*m.mat[k][j];

return matprod;

void main()

Object Oriented Programming Using C++Page 16


KRUPANIDHI DEGREE COLLEGE

clrscr();

matrix a,b,c;

int rc;

cout<<”\ntype the order of matrix:”;

cin>>rc;

a.size(rc);

b.size(rc);

c.size(rc);

cout<<”\nmatrix a:”;

a.matinput();

cout<<”\nmatrix b:”;

b.matinput();

cout<<”\nmatrix a:”;

a.matprint();

cout<<”\nmatrix b:”;

b.matprint();

c=a*b;

cout<<”\n product of matrices:\n”;

c.matprint();

getch();

Output:

Object Oriented Programming Using C++Page 17


KRUPANIDHI DEGREE COLLEGE

6) Write a program to implement operation on queue.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of queue.

Object Oriented Programming Using C++Page 18


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

const int max= 5;

class queue

private:

int qu[max];

int front,rear;

public:

queue()

front=-1;

rear=-1;

void addq();

void delq();

void show();

};

void queue :: addq()

int item;

if(rear==max-1)

Object Oriented Programming Using C++Page 19


KRUPANIDHI DEGREE COLLEGE

cout<<endl<<”queue is full”;

return;

rear++;

cout<<endl<<”enter item to be inserted”;

cin>>item;

qu[rear]=item;

if(front==-1)

front=0;

void queue :: delq()

int item;

if(front==-1)

cout<<endl<<”queue is empty”;

return;

item=qu[front];

cout<<”\n item deleted:”<<item;

if(front==rear)

front=rear=-1;

else

front++;

return;

Object Oriented Programming Using C++Page 20


KRUPANIDHI DEGREE COLLEGE

void queue::show()

if(front==-1)

cout<<”\n queue is empty:\n”;

else

cout<<endl<<”queue elements:\n”;

for(int i=front;i<=rear;i++)

cout<<setw(4)<<qu[i];

void main()

int choice;

queue q;

clrscr();

do

cout<<”\n-----------------queue operations--------------------“;

cout<<”\n1. insert”;

cout<<”\n2.delete”;

cout<<”\n3.display”;

cout<<”\n4. exit”;

cout<<”enter your choice:”;

Object Oriented Programming Using C++Page 21


KRUPANIDHI DEGREE COLLEGE

cin>>choice;

switch(choice)

case 1:q.addq();

break;

case 2: q.delq();

break;

case 3: q.show();

break;

case 4: exit(0);

default:cout<<”\nwrong choice!!”;

while(choice!=4);

getch();

Output:

Object Oriented Programming Using C++Page 22


KRUPANIDHI DEGREE COLLEGE

7) Write a program to create a student report using inheritance technique.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of inheritance.

Object Oriented Programming Using C++Page 23


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

class student

protected:

char name[20];

int reg;

public:

void getdata()

cout<<”type register no:”;

cin>>reg;

cout<<”type name:”;

cin>>name;

void showdata()

cout<<setprecision(2);

cout.setf(ios::left,ios::adjusted);

cout<<”\nroll no:”<<setw(3)<<reg;

cout<<”\nname:”<<setw(10)<<name;

};

class marks:public student

Object Oriented Programming Using C++Page 24


KRUPANIDHI DEGREE COLLEGE

protected:

int m1,m2,m3;

float perc;

public:

void getmarks()

cout<<”\n marks in c++, maths, dbms:”;

cin>>m1>>m2>>m3;

perc=(m1+m2+m3)/300.0*100;

void showmarks()

cout<<”\nc++:”<<setw(5)<<m1;

cout<<”\nmath:”<<setw(5)<<m2;

cout<<”\ndbms:”<<setw(5)<<m3;

cout<<”\npercent:”<<setw(8)<<perc;

};

class result:public marks

private:

char grade;

public:

void display()

Object Oriented Programming Using C++Page 25


KRUPANIDHI DEGREE COLLEGE

showdata();

showmarks();

cout<<”\ngrade:”<<setw(10)<<getgrade()<<endl;

char getgrade()

if(m1<40||m2<40||m3<40)

grade=’f’;

else if(perc>75)

grade=’a’;

else if(perc>60)

grade=’b’;

else if(perc>50)

grade=’c’;

else if(perc>40)

grade=’d’;

else

grade=’f’;

return grade;

};

void main()

clrscr();

Object Oriented Programming Using C++Page 26


KRUPANIDHI DEGREE COLLEGE

result std;

cout<<”grades:a->75% and above”<<endl;

cout<<” b->60-74”<<endl;

cout<<” c->50-59”<<endl;

cout<<” d->40-49”<<endl;

cout<<” f->below 40”<<endl;

std.getdata();

std.getmarks();

std.display();

getch();

Output:

8) Write a Program to find the area and volume of respective figures using
function overloading.

Components Used: Computer, TURBO C++

Object Oriented Programming Using C++Page 27


KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of function overloading.

Procedure:

#include<iostream.h>

#include<conio.h>

Const double pi=3.14;

class shape

public:

void measure(double r)

cout<<”\n area of a circle is:”<<pi*r*r;

cout<<”\n volumes of a sphere is:”<<(4.0/3)*pi*r*r*r<<endl;

void measure(long l, int b, int h)

cout<<”\n area of a rectangle is:”<<l*b;

cout<<”\n volume of a rectangle box is:”<<l*b*h<<endl;

void measure(float f)

cout<<”\narea of a square is:”<<f*f;

cout<<”\nvolumes of a cube is:”<<f*f*f<<endl;

};

void main()

Object Oriented Programming Using C++Page 28


KRUPANIDHI DEGREE COLLEGE

int b,h;

long l;

double r;

float f;

shape obj;

clrscr();

cout<<”\n\t calculation of area and volume\n”;

cout<<”\nenter radius of the circle”;

cin>>r;

obj.measure( r );

cout<<”\n enter the length, breadth and height of a rectagle box:”;

cin>>l>>b>>h;

obj.measure(l,b,h);

cout<<”\n enter a side of a square:”;

cin>>f;

obj.measure(f);

getch();

Output:

Object Oriented Programming Using C++Page 29


KRUPANIDHI DEGREE COLLEGE

9) Write a program to show returning current object, accessing member data of


current object and returning values of object using this pointer

Components Used: Computer, TURBO C++

Object Oriented Programming Using C++Page 30


KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of pointers.

Procedure:

#include<iostream.h>

#include<conio.h>

class rectangle

private:

int length;

int width;

public:

void setlength(int len)

this->length=len;

int getlength()

return this->length;

void setwidth(int w)

width=w;

void getwidth()

return width;

Object Oriented Programming Using C++Page 31


KRUPANIDHI DEGREE COLLEGE

};

void main()

rectangle rect;

clrscr();

rect.setlength(20);

rect.setwidth(10);

cout<<”rectangle is”<<rect.getlength()<<”feet long”;

cout<<”and”<<rect.getwidth()<<”feet wide”<<endl;

getch();

Output:

10) Write a program to sort elements using template.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of template.

Object Oriented Programming Using C++Page 32


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

Template<class S>

void bubble(s a[], int n)

int i,j;

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

for(j=0;j<n-i-1;j++)

if(a[j]>a[j+1])

swap(a[j],a[j+1]);

Template<class T>

void swap(t &x, t &y)

t temp;

temp=x;

x=y;

y=temp;

void main()

int i,j,a[5]={25,2,56,,4,34};

Object Oriented Programming Using C++Page 33


KRUPANIDHI DEGREE COLLEGE

float p[5]={25.25,3.45,4.2,20.5,5.8};

clrscr();

cout.precision(2);

cout<<”original integer array a:”;

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

cout<<a[i]<< ‘ ‘;

cout<<”\n original float array b:”;

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

cout<<p[i]<<’ ‘ ;

bubble(a,5);

bubble(p,5);

cout<<”\n\nsorted integer array a:”;

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

cout<<a[i]<<’ ‘ ;

cout<<”\n\nsorted float array a:”;

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

cout<<p[i]<<’ ‘ ;

getch();

Output:

Object Oriented Programming Using C++Page 34


KRUPANIDHI DEGREE COLLEGE

PART
–B
PRO
GRA
MS

11) Write a program to illustrate class with member functions defined outside the
class.

Components Used: Computer, TURBO C++

Object Oriented Programming Using C++Page 35


KRUPANIDHI DEGREE COLLEGE

Aim: To introduce the concept of scope resolution operator.

Procedure:

#include<iostream.h>

#include<conio.h>

class employee

private:

char name[20];

int basic,,netpay;

public:

void gaetdata();

void showdata();

};

void employee::getdata()

cout<<”\n enter name of employee:”;

cin.getline(name,20);

cout<<”\n enter basic pay:”;

cin>>basic;

void employee::showdata()

cout<<”\n name of employee:”<<name;

cout<<”\n basic pay:”<<basic;

netpay= basic+(basic*20/100)+(basic*10/100)-(basic*10/100);

Object Oriented Programming Using C++Page 36


KRUPANIDHI DEGREE COLLEGE

cout<<”Net Salary=”<<netpay;

void main()

employee emp;

clrscr();

emp.getdata();

emp.showdata();

Output:

12) Write a program to check whether a given year is leap year

Components Used: Computer, TURBO C++

Aim: To introduce the concept of control statements.

Object Oriented Programming Using C++Page 37


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

class leap

private:

int year;

public:

int checkyear();

void getdata();

void showdata();

};

int leap::checkyear()

int r4=year%4;

int r100=year%100;

int r400=year%400;

if((r4==0 && r100 !=0) || (r400==0))

return 1;

else

return 0;

void leap::getdata()

cout<<”\n enter a 4 digit year please:”;

Object Oriented Programming Using C++Page 38


KRUPANIDHI DEGREE COLLEGE

cin>>year;

void leap::showdata()

if(checkyear())

cout<<”\n”<<year<<”is a leap year”;

else

cout<<”\n”<<year<<”is not a leap year”;

void main()

leap yr;

clrscr();

yr.getdata();

yr.showdata();

Output:

13) Write a program to find the roots of a quadratic equation using constructors

Components Used: Computer, TURBO C++

Aim: To introduce the concept of constructors.

Procedure:

Object Oriented Programming Using C++Page 39


KRUPANIDHI DEGREE COLLEGE

#include<iostream.h>

#include<conio.h>

#include<math.h>

class quad

private:

float a,b,c;

float x1,x2;

public:

quad()

a=0;b=0;c=0;

void getdata()

cout<<”\n enter the values of a,b, and c:”;

cin>>a>>b>>c;

float disc()

return(b*b-4*a*c);

void root_equal()

cout<<”\n roots are equal”;

Object Oriented Programming Using C++Page 40


KRUPANIDHI DEGREE COLLEGE

cout<<”\n x1=x2=”<<-b/(2*a)<<endl;

void root_real()

cout<<”\n roots are real and unequal\n”;

x1=(-b+sqrt(disc()))/(2*a);

x2=(-b+sqrt(disc()))/(2*a);

cout<<”\nx1=”<<x1<<endl;

cout<<”\nx2=”<<x2<<endl;

void root_imag()

float rpart, ipart;

rpart=-b/(2*a);

ipart=sqrt(abs(disc()))/(2*a);

cout<<”\n roots are imaginary\n”;

cout<<”\nx1=”<<rpart<<”+i”<<ipart<<endl;

cout<<”\nx2=”<<rpart<<”-i”<<ipart<<endl;

};

void main()

quad r1;

clrscr();

r1.getdata();

Object Oriented Programming Using C++Page 41


KRUPANIDHI DEGREE COLLEGE

if(r1.disc()==0)

r1.root_equal();

elseif(r1.disc()>0)

r1.root_real();

else

r1.root_imag();

getch();

Output:

14) Write a program to add two distances given in feet and inches.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of constructors and scope resolution operator.

Procedure:

Object Oriented Programming Using C++Page 42


KRUPANIDHI DEGREE COLLEGE

#include<iostream.h>

#include<conio.h>

#include<math.h>

class dist

private:

int feet;

float inches;

public:

disc()

feet=0; inches=0.0;

disc(int ft, float in)

feet=ft; inches=in;

void getdist()

cout<<”\n type value of feet:”;

cin>>feet;

cout<<”\n type value of inches:”;

cin>>inches;

void showdist()

Object Oriented Programming Using C++Page 43


KRUPANIDHI DEGREE COLLEGE

cout<<feet<<”’-“<<inches<<”\””;

void sumdist(dist d1, dist d2);

};

void dist::sumdist(dist d1, dist d2)

inches=(d1.inches+d2.inches)%12;

feet=d1.feet+d2.feet+(d1.inches+d2.inches)/12;

void main()

clrscr();

dist d1,d3;

d1.getdist();

dist d2(12,9);

d3.sumdist(d1,d2);

cout<<”\n d1=”;

d1.showdist();

cout<<”\n d2=”;

d2.showdist();

cout<<”\n d3=”;

d3.showdist();

getch();

Object Oriented Programming Using C++Page 44


KRUPANIDHI DEGREE COLLEGE

Output:

15) Write a program to find maximum of two numbers using friend function.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of friend function.

Procedure:

Object Oriented Programming Using C++Page 45


KRUPANIDHI DEGREE COLLEGE

#include<iostream.h>

#include<conio.h>

class max

private:

int x;

int y;

public:

void getdata()

cout<<endl<<”enter a number”;

cin>>x;

cout<<endl<<”enter an another number”;

cin>>y;

void showdata()

cout<<endl<<”x is:”<<x;

cout<<endl<<”y is:”<<y;

friend int large( max m);

};

int large(max m)

if(m.x>m.y)

Object Oriented Programming Using C++Page 46


KRUPANIDHI DEGREE COLLEGE

return m.x;

else

return m.y;

void main()

max m;

int big;

clrscr();

m.getdata();

m.showdata();

cout<<endl<<”largest is:”<<large(m);

getch();

Output:

16) Write a program to illustrate overloading of binary operator +.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of binary + operator.

Procedure:

Object Oriented Programming Using C++Page 47


KRUPANIDHI DEGREE COLLEGE

#include<iostream.h>

#include<conio.h>

class clock

private:

int hr;

int min;

public:

void gettime(int hh,int mm)

hr=hh;

min=mm;

void showtime()

cout<<hr<<”hours and”<<min<<”minutes”<<endl;

clock operator +(clock);

};

clock clock::operator+(clock t)

clock temp;

temp.min=min+t.min;

temp.hr=hr+t.hr;

if(temp.min>=60)

Object Oriented Programming Using C++Page 48


KRUPANIDHI DEGREE COLLEGE

temp.min=temp.min-60;

return temp;

void main()

clrscr();

clock t1,t2,t3;

t1.gettime(1,25);

t2.gettime(3,30);

t3=t1+t2;

cout<<endl<<”\n time 1:”;

t1.showtime();

cout<<endl<<”\n time 2:”;

t2.showtime

cout<<endl<<”\n total duration:”;

t3.showtime();

getch();

Output:

Object Oriented Programming Using C++Page 49


KRUPANIDHI DEGREE COLLEGE

17) Write a program to demonstrate constructor with default arguments.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of constructor with default arguments.

Object Oriented Programming Using C++Page 50


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

class date

private:

int day,month,year;

public:

date() {}

date(int d, int m, int y=2015)

day=d; month=m; year=y;

void getdate();

void showdate();

};

void date::getdate()

cout<<”\n enter the date in dd mm yyyy format:”;

cin>>day>>month>>year;

void date::showdate()

cout<<day<<”/”<<month<<”/”<<year<<endl;

Object Oriented Programming Using C++Page 51


KRUPANIDHI DEGREE COLLEGE

void main()

clrscr();

class date d1,d2(20,5), d3(6,6,1970);

d1.getdate();

cout<<”\n date1:”;

d1.showdate();

cout<<”\n date2:”;

d2.showdate();

cout<<”\n date3:”;

d3.showdate();

getch();

Output:

18) Write a program to find the length of a string.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of string function.

Object Oriented Programming Using C++Page 52


KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

private:

char name[50];

int length;

public:

void getdata()

cin>>name;

void show()

cout<<name<<endl;

int strlength(string s)

length=strlen(s.name);

};

void main()

Object Oriented Programming Using C++Page 53


KRUPANIDHI DEGREE COLLEGE

clrscr();

string s1;

cout<<”enter string:”;

s1.getdata();

cout<<”given string:”;

s1.show();

cout<<”\n length of a string:”<<s1.strlength(s1);

getch();

Output:

19) Write a program to perform addition of two matrices using operator


overloading.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of function.


Object Oriented Programming Using C++Page 54
KRUPANIDHI DEGREE COLLEGE

Procedure:

#include<iostream.h>

#include<conio.h>

class matrix

private:

int mat[10][10];

int rc;

public:

matrix() {};

void size(int n)

rc=n;

void matinput();

void matprint();

matrix operator +(matrix);

};

void matrix::matinput()

cout<<”\n type”<<rc*rc<<”elements”;

for(int i=0;i<rc;i++)

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

cin>>mat[i][j];

Object Oriented Programming Using C++Page 55


KRUPANIDHI DEGREE COLLEGE

void matrix::matprint()

for(int i=0;i<rc;i++)

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

cout<<mat[i][j]<<setw(6);

cout<<”\n”;

matrix matrix::operator +(matrix m)

matrix matsum;

matsum.size(rc);

for(int i=0;i<rc;i++)

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

matsum.mat[i][j]=mat[i][j]+m.mat[i][j];

return matsum;

void main()

clrscr();

matrix a,b,c;

int rc;

cout<<”\n type the order of matrix”;

cin>>rc;

Object Oriented Programming Using C++Page 56


KRUPANIDHI DEGREE COLLEGE

a.size(rc);

b.size(rc);

c.size(rc);

cout<<”\n matrix a:”;

a.matinput();

cout<<”\n matrix b:”;

b.matinput();

cout<<”\n matrix c:”;

c.matinput();

cout<<”\n matrix a:”;

b.matprint();

cout<<”\n matrix b:”;

a.matprint();

cout<<”\n matrix c:”;

c.matprint();

getch();

Output:

20) Write a program to concatenate two string using + operator.

Components Used: Computer, TURBO C++

Aim: To introduce the concept of + operator.

Procedure:

Object Oriented Programming Using C++Page 57


KRUPANIDHI DEGREE COLLEGE

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

private:

char str[80];

public:

void show()

cout<<str;

void getdata()

cin>>str;

friend string operator +(string, string);

};

string operator +(string s1, string s2)

string temp;

strcpy(temp.str, s1.str);

strcat(temp.str,s2.str);

return(temp);

Object Oriented Programming Using C++Page 58


KRUPANIDHI DEGREE COLLEGE

void main()

clrscr();

string s1,s2,s3;

cout<<endl<<”enter first string:”;

s1.getdata();

cout<<endl<<”enter second string:”;

s2.getdata();

cout<<endl<<”string 1: “;

s1.show();

cout<<endl<<”string 2: “;

s2.show();

s3=s1+s2;

cout<<endl<<”concatenated string:”;

s3.show();

getch();

Output:

Object Oriented Programming Using C++Page 59

You might also like