Friday, September 11, 2009

C++ Programs

Employee details and to output the data
Fibonacci Numbers Sum Of Digits
Constructor - Default Constructor
Parameterized Constructor
Constructor Overloading
Copy Constructor
Function Overloading - Program
Outside Declaration Of The Member Function Inside Declaration Of The Member Function Overloading Unary Operator
Overloading Unary Operator Using + Operator
Merge Sort Program - Algorithm File Mode Program
File Concept Program
Exception Handling Program
Function Templates Program
Virtual Function Program
Friend Function Program Multilevel Inheritance
Multiple Inheritance Program Single Inheritance Program Static Member Function
Creation Of Class And Function
Static Class Member
Destructor Program
Constructor Program

Book Entity

Write a program using structure to define the elements of a book entity and to display the same.

/* Code for defining a structure and to display using structure */


#include"iostream.h"
#include"string.h"
struct book

{
int bookno;
char bookname[30];
char author[25];
float price;
};
void main()
{
book mybook;
mybook.bookno = 100;
strcpy(mybook.bookname,"Introduction to C++");
strcpy(mybook.author,"Surya");
mybook.price = 150.00;
cout << "The book number is " << mybook.bookno;
cout << "The book name is " << mybook.bookname;
cout << "The author of the book is " << mybook.author;
cout << "The price of the book is " << mybook.price;
}

Friday, May 29, 2009

Fibonacci numbers

#include "iostream.h"
#include "conio.h"

// sequence is 0, 1, 1, 2, 3, 5, 8, 13, ...

int fib (int i) {
int pred, result, temp;

pred = 1;
result = 0;

while (i > 0) {
temp = pred + result;
result = pred;
pred = temp;
i = i-1;
}
return(result);
}

void main () {
int n;
cout << "Enter a natural number: ";
cin >> n;
while (n < 0) {
cout << "Please re-enter: ";
cin >> n;
}
cout << "fib(" << n << ") = " << fib(n) << endl;

}

Friday, May 15, 2009

Sum of Digits

To display the Sum of Digits

header files
void main()
{int n;
int n1, sum=0;
cout<<"Enter the Number of N: ";
cin>> n;
while(n>0)
{
n1=n%10;
sum=sum+n1;
n=n/10;
}
cout<< sum;getch();
}
Output will be
Enter the Number of N: 258
15

Friday, May 8, 2009

CONSTRUCTOR - Default Constructor

header files

class num
{
int a,b;
public:
num() // default constructor
{
a=100;
b=200;
}
void display()

};

void num::display()
{
cout<<>
cout<<>
}
void main()
{
num obj;
obj.display();
}

Output will be

100
200

Parameterized Constructor

header files

class num
{
int a,b;
public:
num(int x, int y) // Parameterized constructor
{
a=x;
b=y;
}
void display()

};

void num::display()
{
cout<<>
cout<<>
}
void main()
{
num obj(1000,5000);
obj.display();
}

Output will be

1000
5000

Constructor Overloading

header files

class num
{
int a,b;
float c,d;
char s[5], s1[10];
public:
num(int x, int y) // constructor overloading
{
a=x;
b=y;
}
num(char str[10], char str1[15]) // constructor overloading
{
strcpy(s,s1);
strcpy(s1,str1);
}
num(float p) // constructor overloading
{
c=p;
d=555;

}
void display()

};

void num::display()
{
cout<<>
cout<<>
cout<<>
}
void main()
{
num obj(1000,5000);
num obj("Kumar","Dharsinisamy");
num obj(150.60);
obj.display();
}

Output will be

1000
5000
Kumar
Dharsinisamy
100.50
555

Copy Constructor

class copy
{
int a,b,c;
public:
copy(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
copy(copy & ob) // copy constructor declaration
{
a=ob.a;
b=ob.b;
c=ob.c;
}
void display()
{
cout << a << endl << c << endl;
}
};

void main()
{
copy ob1(5,10,20);
copy pb2(ob1); // object as an argument
clrscr();
ob1.display();
ob2.display();
getch();
}

output will be
5
10
20
5
10
20

Friday, May 1, 2009

Function Overloading - Program

header files

include

iostrem.h
conio.h
string.h

class swap
{
int a,b,c;
float x,y,z;
char p[10];
char ch,ch1;
public:
swap()
{
a=20;
b=40
}
swap(int x, int y)
{
a=x;
b=y;
}
swap(float a, float b, int aa)
{
x=a;
y=b;
c=aa;
}
swap(char s[10])
{
strcpy(p,s);
}
void display1()
{
cout<< a <<"\n" << b <<"\n";
}
void display1()
{
cout<< x <<"\n" << y <<"\n"<< c <<"\n";
}
void display1()
{
cout<< p;
}
};
void main()
{
clrscr();
swap obi();
swap obj1(6,15);
swap obj2(2.4,5.6,6);
swap obj3("god");
obj.display1();
obj1.display2();
obj2.display3();
obj3.display1();

}

Outside declaration of the member function

Outside declaration of the member function

header files
class addition // class declaration
{
int a,b,c; // data member declaration
public:
void get() // function declaration
void put()

};
void addition::get()
{
cin>> a >> b;
c=a+b;
}
void addition::get()
{
cin>> a >> b;
c=a+b;
}

void main()
{
clrscr();
addition obj;
obj.get(); // object creation
obj.put(); // object calling
getch();

}

inside declaration of the member function

inside declaration of the member function

header files
class addition // class declaration
{
int a,b,c; // data member declaration
public:
void get()
{
cin>> a >> b;
c=a+b;
}
void put()
{
cout<<"Result is C=" << c;
}
};
void main()
{
clrscr();
addition obj;
obj.get(); // object creation
obj.put(); // object calling
getch();

}

Tuesday, April 28, 2009

Overloading Unary Operator

header files

class minus
{
int a,b,c;
public:
void get();
{
a=10;
b=-20;
z=30;
}
void operator-()
{
a=-a;
b=-b;
c=-c;
}

void display()
{
cout<<"A=" << a <<"\n";
cout<<"B=" << b <<"\n";
cout<<"C=" << c <<"\n";
}
};

void main()
{
clrscr();
minus obj;
obj.get();
obj.display();
-obj;
obj.display();
getch();
}

Output will be

A=-10
B=20
Z=-30

Overloading Unary Operator Using + Operator

Operator Overloading


header files
class time
{
int hrs, min;
public:
void get(int a,int b)
{
hrs=a;
min=b;
}
time operator +(time ob);
void disp();
void display();
};
time time::operator +(time ob)
{
time t;
t.hrs=hrs+ob.hrs;
t.min=min+ob.min;
return(t);
}
void time::disp()
{
cout<<"\n" <<"Hours=" << hrs <<" " <<"Minutes=" << min << endl;
}
void display()
{
int n1, m;
m=min/60%;
n1=m/60;
hrs=hrs+n1;
cout<<"\n" << hrs <<"hrs" << m <<"mins" << endl;
}
void main()
{
clrscr();
time obj1, obj2, obj3,;
obj1.get(5,0);
obj2.et(7,45);
obj3=obj1+obj2;
obj1.disp();
obj2.disp();
obj3.display();
getch();
}

Output will be

12 hrs 45 mins

Saturday, April 25, 2009

MERGE SORT PROGRAM - ALGORITHM

#include<>
#include<>
class msort
{
int n,a[20];
public:
void msplit(int a[20],int first,int last);
void merge(int a[20],int f1,int l1,int f2,int l2);
};
void msort::msplit(int a[20],int first,int last)
{
int mid;
if(first {
mid=(first+last)/2;
msplit(a,first,mid);
msplit(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void msort::merge(int a[20],int f1,int l1,int f2,int l2)
{
int i,j,k=0,temp[20];
i=f1;
j=f2;
while((i<=l1)&&(j<=l2))
{
if(a[i] {
temp[k]=a[i];
i++;
}
else
{
temp[k]=a[j];
j++;
}
k++;
}
while(i<=l1)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=l2)
{
temp[k]=a[j];
j++;
k++;
}
i=f1;
j=0;
while((i< =l2)&&(j< =k))
{
a[i]=temp[j];;
i++;
j++;
}
}
{
int i,a[20],n;
clrscr();
cout<<"\n\n\t\tMERGE SORT\n";
cout<<"\t\t---------------------\n";
msort x;
cout<<"\nEnter the n value:";
cin>>n;
cout<<"\nEnter the array values:\n";
for(i=0;i cin>>a[i];
x.msplit(a,0,n-1);
cout<<"\nThe Sorted list is:\n";
for(i=0;i cout<<" "< getch();
}

OUTPUT WILL BE

Enter the n value: 5
Enter the array values:
5 9 2 7 1

The Sorted list is:
1 2 5 7 9

Friday, April 24, 2009

File Mode Program

File Mode Program
#include<>
#include<>
void main()
{
char name[25],des[50];
int empcode;
clrscr();
ofstream f;
f.open(vvp.txt",ios::app);
cout<<"Enter Employee Name"; cin>> name;
f<<>> des;
f<<>> empcode;
f<<>> name;
fo>> des;
fo>> empcode;
cout>> name >> endl;
cout>> des >> endl;
cout>> empcode >> endl;
fo.close();
getch();
}

Sunday, April 12, 2009

FILE CONCEPT PROGRAM

#include
#include
#include
#include
#include
#include
#include

void main()
{
clrscr();
ofstream outfile;
ifstream infile;
char ch,uch;
char fname1[10],fname2[10];
cout<<"\nEnter a file name:";
cin>>fname1;
cout<<"\nNew file name:";
cin>> fname2;
infile.open(fname1);
if(infile.fail())
{
cout<<"\n No such a file exists";
exit(0);
}
outfile.open(fname2);
if(outfile.fail())
{
cout<<"\n Unable to create a file";
exit(0);
}
while(!infile.eof())
{
ch=(char)infile.get();
uch=toupper(ch);

outfile.put(uch);
}
infile.close();
outfile.close();
getch();
}
Enter a file name :File1.cpp

New file name : File2.cpp

Input File: File1.cpp

#include
#include

void main()
{
clrscr();
cout<<"\n\nHello World";
getch();
}

Output will be:

Output File: File2.cpp

#INCLUDE
#INCLUDE

VOID MAIN()
{
CLRSCR();
COUT<<"\N\NHELLO WORLD";
GETCH();
}

EXCEPTION HANDLING PROGRAM

#include
#include
void main()
{
int a,b;
clrscr();

cout<<"\nEnter the value a and b:";
cin>>a;
cin>>b;
int x=a-b;
try
{
if(x!=0)
cout<<"\nResult:"<< a/x;
else
throw (x);
}
catch(int i)
{
cout<<"\nException Caught"<< x;
}
getch();
}

Output will be:

Enter the value a and b: 10 10

Exception Caught 0

EXCEPTION HANDLING

Enter the value a and b: 10 5

Result: 2

FUNCTION TEMPLATES PROGRAM

#include
#include
templatevoid swaparg(x &a,x &b)
{
x temp;
temp=a;
a=b;
b=temp;
}
void main()
{
int i=10,j=20;
double x=10.1,y=23.3;
char a='y',b='z';
clrscr();
cout<<"\nOriginal i and j: "<< i<<" ,"<< j;
cout<<"\nOriginal x and y: "<< x<<" ,"<< y;
cout<<"\nOriginal a and b: "<< a<<" ,"<< b;
swaparg(i,j);
swaparg(x,y);
swaparg(a,b);
cout<<"\n\nSwapped i and j: "<< i<<", "<< j;
cout<<"\nSwapped x and y: "<< x<<" ,"<< y;
cout<<"\nSwapped a and b: "<< a<<", "<< b;
getch();
}


Output will be:


Original i and j : 10, 20
Original x and y: 10.1, 23.3
Original a and b: y, z

Swapped i and j : 20, 10
Swapped x and y: 23.3, 10.1
Swapped a and b: z, y

VIRTUAL FUNCTION PROGRAM

#include
#include

class base
{
public:
virtual void vfunc()
{
cout<<"\nThis is base vfunc()";
}
};

class derived1:public base
{
public:
void vfunc()
{
cout<<"\nThis is derived1 vfunc()";
}
};

class derived2:public base
{
public:
void vfunc()
{
cout<<"\nThis is derived2 vfunc()";
}
};

void main()
{
clrscr();
base *p,b;
derived1 d1;
derived2 d2;
p=&b;
p-> vfunc();
p=&d1;
p-> vfunc();
p=&d2;
p-> vfunc();
getch();
}



Output will be:

This is base vfunc()
This is derived1 vfunc()
This is derived2 vfunc()

FRIEND FUNCTION PROGRAM

#include
#include
class data
{
int a,b;
public:
void get_value()
{
a=30;
b=55;
}
friend float mean(data s);
};
float mean(data s)
{
return float(s.a+s.b)/2;
}
void main()
{
clrscr();
data x;
x.get_value();
cout<<"\n Mean Value="<< mean(x);
getch();
}


Output will be:

Mean Value=42.5

MULTILEVEL INHERITANCE

#include
#include
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”;
cin>> rno;
cout<<”Enter the student Name :”;
cin>> name;
cout<<”Enter the Student Address:”;
cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<< rno;<<”\n”;
cout<<”Name : “<< name<<”\n”;
cout<<"Address :”<< address<<”\n”;
}
class mark:public student
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”;
cin>>mark1>>mark2>>mark3>>mark4>>mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< mark1<< endl;
cout<< mark1<< endl;
cout<< mark2<< endl;
cout<< mark3<< endl;
cout<< mark4<< endl;
cout<< mark5<< endl;
cout<<“Total =“<< total<< endl;
cout<<“Average =“<< avg<< endl;
}
class result:public mark

{
public:
void check();
};
void result::check()
{
if(avg>=90)
{
cout<”Distinction”;
}
else if(avg<=80 && avg>=60)
{
cout<<”I Class”;
}
else if(avg<60 && avg>=40)
{
cout<<”II Class”;
}
else
cout<<”Fail”;
}
void main()
{
clrscr();
result obj1;
obj1.get();
obj1.getmark();
obj1.diaplay();
obj1.displaymark();
obj1.check();
getch();
}

Output will be

Enter the Student Roll Number: 101
Enter the student Name: Lak
Enter the Student Address: Chennai
Enter the five subject Marks one by one
80
75
80
90
100
Total = 425
Average = 85
I Class

MULTIPLE INHERITANCE PROGRAM

#include <>
#include <>
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”; cin>> rno;
cout<<”Enter the student Name :”; cin>> name;
cout<<”Enter the Student Address:”; cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<< style="FONT-WEIGHT: bold">class mark
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”; cin>>mark1>>mark2>>mark3>>mark4>>mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< total ="“<<" average ="“<<" style="FONT-WEIGHT: bold">class result:public student, public mark
{
public:
void check();
};
void result::check()
{
if(avg>=90)
{
cout<”Distinction”; } else if(avg<=80 && avg>=60)
{
cout<<”I Class”; } else if(avg<60>=40)
{
cout<<”II Class”; } else cout<<”Fail”; } void main() { clrscr(); result obj1; obj1.get(); obj1.getmark(); obj1.diaplay(); obj1.displaymark(); obj1.check(); getch(); } Output will be

Enter the Student Roll Number: 101
Enter the student Name: Lak
Enter the Student Address: Chennai
Enter the five subject Marks one by one
80
75
80
90
100

Total = 425
Average = 85
I Class

SINGLE INHERITANCE PROGRAM

#include<>
#include<>
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”; cin>> rno;
cout<<”Enter the student Name :”; cin>> name;
cout<<”Enter the Student Address:”; cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<< style="FONT-WEIGHT: bold">class mark:public student
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”; cin>>mark1>>mark2>>mark3>>mark4>>mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< total ="“<<" average ="“<<" style="FONT-WEIGHT: bold">Output will be

Enter the Student Roll Number: 101
Enter the student Name: Lak
Enter the Student Address: Chennai
Enter the five subject Marks one by one
80
75
80
90
100

80
75
80
90
100
Total = 425
Average = 85

STATIC MEMBER FUNCTION

#include
#include
class test
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"Object Number:"<< code<<"\n";
}
static void showcount(void)
{
cout<<"\ncount:"<< count;
}
};
int test :: count;

void main()
{
clrscr();
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}

Output will be

Count: 3

Object Number: 1
Object Number: 2
Object Number: 3

CREATION OF CLASS AND FUNCTION

#include
#include
class item
{
float cost;
int number;
public:
void getdata(int,float);
void putdata()
{
cout<<"\nNumber="<< number<<"\n";
cout<<"Cost="<< cost<<"\n";
}
};
void item::getdata(int a,float b)
{
number=a;
cost=b;
}
void main()
{
clrscr();
item a;
a.getdata(100,20.31);
a.putdata();
item b;
b.getdata(180,25.56);
b.putdata();
getch();
}


Output will be:

Number=100
Cost=20.309999

Number=180
Cost=25.559999

STATIC CLASS MEMBER

#include
#include
class item
{
static int count;
int number;
public:
void getdata(int);
void getcount()
{
cout<<"Count="<< count<<"\n";
}
};
void item::getdata(int a)
{
number=a;
count++;
}
int item::count;
void main()
{
clrscr();
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"\n After Reading Data\n\n";
a.getcount();
b.getcount();
c.getcount();
getch();
}


Output will be:

Count=0
Count=0
Count=0

After Reading Data

Count=3
Count=3
Count=3

DESTRUCTOR PROGRAM

DESTRUCTOR


#include
#include
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\nNo Of Objects Created "<< count;
}
~alpha()
{
cout<<"\nNo Of Objects Destroyed"<< count;
count--;
}
};
void main()
{
clrscr();
cout<<"\nEnter Main";
alpha a1,a2,a3,a4;
{
cout<<"\nEnter Block1";
alpha a5;
}
{
cout<<"\nEnter Block2";
alpha a6;
}
cout<<"\nRe-Enter main";
getch();
}





Output will be


Enter Main
No Of Objects Created 1
No Of Objects Created 2
No Of Objects Created 3
No Of Objects Created 4

Enter Block1
No Of Objects Created 5
No Of Objects Destroyed5

Enter Block2
No Of Objects Created 5
No Of Objects Destroyed5

Re-Enter main

No Of Objects Destroyed4
No Of Objects Destroyed3
No Of Objects Destroyed2
No Of Objects Destroyed1

CONSTRUCTOR PROGRAM

CONSTRUCTOR

#include
#include
class integer
{
int m,n;
public:
integer(int,int0);
void display()
{
cout<<"m="<< m<<"\n";
cout<<"n="<< n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x;
n=y;
}
void main()
{
clrscr();
integer int1(0,100);
integer int2=integer(25,75);
cout<<"\nobject1\n";
int1.display();
cout<<"\nobject2\n";
int2.display();
getch();
}



Output will be


object1
m=0
n=100

object2
m=25
n=75