WELCOME

Welcome to KoDes4U

Pages

Wednesday, March 9, 2011

templates(C++ program)

/*Write a C++ program using function template to read two matrices of
different data types such as integers and floating point values and perform
simple arithmetic operations on these matrices separately and display.
Coded for KoDes4.blogspot.com*/

#include

#include

#include

template

class CMatrix
{
int Row, Col;
T a[10][10];
public:
void Input();
void AddMat(CMatrix
mat1, CMatrix mat2);
int check(CMatrix
mat2);
void SubMat(CMatrix
mat1, CMatrix mat2);
void Display();
};
template

void CMatrix
::Input()
{
cout<<"\nEnter the order for the matrix "; cin>>Row>>Col;
cout<<"\n Enter the elements:"; for(int i=0; i
>a[i][j];
}
}
}
template

void CMatrix
::Display()
{
for(int i=0; i

void CMatrix
::AddMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

void CMatrix
::SubMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

int CMatrix
::check(CMatrix mat2)
{
int m,n,p,q;
m = Row;
n = Col;
p = mat2.Row;
q = mat2.Col;
if(m==p && n==q)
return 1;
else
return 0;
}
void main()
{
int ch,op,k,r;
clrscr();
do
{
cout<<"\n"; cout<<"\n Matrix operations using templates"; cout<<"\n1.Integer\n2.float\n3.exit"; cout<<"\n Choice..."; cin>>ch;
switch(ch)
{
case 1:
CMatrix
m1, m2, res;
cout<<"\n\n Integer Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>op;
switch(op)
{
case 1:
m1.Input();
m2.Input();
k=m1.check(m2);
if(k==1)
{
res.AddMat(m1, m2);
cout<<"\n Addition of marix::"; res.Display(); } else cout<<"\n ROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: m1.Input(); m2.Input(); k=m1.check(m2); if(k==1) { res.SubMat(m1, m2); cout<<"\n The subtraction of the two matrices is"; res.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } break; case 2: CMatrix
fm1, fm2, fres;
cout<<"\n\n Float Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>r;
switch(r)
{
case 1:
fm1.Input();
fm2.Input();
k=fm1.check(fm2);
if(k==1)
{
fres.AddMat(fm1, fm2);
cout<<"\nThe addition of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: fm1.Input(); fm2.Input(); k=fm1.check(fm2); if(k==1) { fres.SubMat(fm1, fm2); cout<<"\nThe subtraction of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } } } while(ch!=3); getch(); }

Inheritence(C++ program)

/*Design a base class with name , date of birth, blood group another base
class consisting of the data members such as height and weight. Design one
more base class conisting of the insurance policy number and contact address.
The derived class contains the data members telephone numbers and driving
lisence number. Coded for KoDes4U.blogspot.com*/


#include
#include

#include

class base1
{
public:
char name[50],date_of_birth[10],blood_group[3];
};
class base2
{
public:
int no;
char height[5],weight[5];
};
class base3
{
public:
char in_pol_no[10],cont_add[15];
};
class d:public base1,public base2,public base3
{
char ph_no[10],drvng_lic_no[10];
public:
void get_data()
{
cout<<"\nEnter number : "; cin>>no;
cout<<"\nEnter name : "; cin>>name;
cout<<"\nEnter date of birth : "; cin>>date_of_birth;
cout<<"\nEnter blood group : "; cin>>blood_group;
cout<<"\nenter height(in cms) : "; cin>>height;
cout<<"\nEnter weight(in kgs) : "; cin>>weight;
cout<<"\nEnter insurance policy no : "; cin>>in_pol_no;
cout<<"\nEnter contact address : "; cin>>cont_add;
cout<<"\nEnter phone number(please include STD code) : "; cin>>ph_no;
cout<<"\nEnter driving licence number : "; cin>>drvng_lic_no;
}
void disp_info()
{
cout<<"\nSerial no : "<
>ch;
switch(ch)
{
case 1:
cout<<"\nEnter no.of records : "; cin>>x;
for(i=0;i
>j;
for(i=0;i
>j;
for(i=0;i
obj[i].search(obj[i].no,j);
cout<<"\nResult : ";
obj[j-1].disp_info();
break;
}
}while(ch!=7);
file.close();
}

Static member functions, friend class, this pointer, inline code and dynamic memory allocation(C++ program)

/*Develop an object oriented program in C++ to create a database of the
personal information system containing the following information:
Name, Date of Birth, Blood group, Height, Weight, Insurance policy number,
Contact address, driving licence no. etc. Construct the database with
suitable member functions for initializing and destroying the data viz
constructor,default constructor, copy constructor ,destructor, static member
functions, friend clas, this pointer,inline code and dynamic memory allocation
operators-new and delete. coded for KoDes4U.blogspot.com*/


#include
#include
#include
class database
{
char name[50],date_of_birth[11],cont_add[50],tel_no[10];
public:
database() //constructor
{
strcpy(name,"blank_name");
strcpy(date_of_birth,"00/00/00");
strcpy(cont_add,"-----");
strcpy(tel_no,"999");
}
~database() //destructor
{
}
void get_info()
{
cout<<"Enter the name:"; cin>>name;
cout<<"\nEnter the Date of Birth:"; cin>>date_of_birth;
cout<<"\nEnter the contact address:"; cin>>cont_add;
cout<<"\nEnter the telephone no(please mention your STD code also):"; cin>>tel_no;
}
friend class extra;
friend void show(database a,extra b);
};
class extra
{
int height,weight;
char blood_group[3],in_pol_no[10],drvng_lic_no[10];
public:
extra()
{
height=180;
weight=60;
strcpy(blood_group,"---");
strcpy(in_pol_no,"---");
strcpy(drvng_lic_no,"----");
}
void get_extra_info()
{
cout<<"Enter the height(in cms):"; cin>>height;
cout<<"\nEnter the weight(in kg):"; cin>>weight;
cout<<"\nEnter the blood group:"; cin>>blood_group;
cout<<"\nEnter the insurance policy number:"; cin>>in_pol_no;
cout<<"\nEnter the driving license number(may be alphanumeric:"; cin>>drvng_lic_no;
}
~extra()
{
}
friend void show(database a,extra b);
};
void main()
{
int cnt,num;
clrscr();
cout<<"Enter the no of entries:"; cin>>num;
database *d[10];
extra *e[10];
for(cnt=0;cntget_info();
e[cnt]=new extra;
e[cnt]->get_extra_info();
}
for(cnt=0;cnt cout<<"\n"< }