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
Tuesday, April 28, 2009
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
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
#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
x.msplit(a,0,n-1);
cout<<"\nThe Sorted list is:\n";
for(i=0;i
}
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();
}
#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();
}
#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
#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
#include
template
{
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()
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
Subscribe to:
Posts (Atom)