Bca C++ Pratical
Bca C++ Pratical
1 Create a Class
3 Inline Function
6 Multilevel Inheritance
7 Copy Constructure
8 Destructor
9 Friend Function
10 Function Overloading
12 Exception Handling
13 Rethrow an Exception
14 Class Template
15 Function Template
CREATE A CLASS
#include<iostream.h>
#include<conio.h>
class student
{
int a,b,c;
public:
void add()
{
cout<<"Enter two Number=";
cin>>a>>b;
c=a+b;
cout<<"addition is="<<c;
}
};
void main()
{
student s1,s2,s3;
s1.add();
getch();
}
SCOPE RESOLUTION OPERATOR
#include<iostream.h>
#include<conio.h>
class operate
{
public:
void hello();
};
void operate::hello()
{
cout<<"It is the Member Function of a Operator Class";
}
void main()
{
operate s;
s.hello();
getch();
}
INLINE FUNCTION
#include<conio.h>
#include<iostream.h>
inline square(int a)
{
return(a*a);
}
void main()
{
clrscr();
cout<<square(6);
getch();
}
STATIC DATA MEMBER
#include<conio.h>
#include<iostream.h>
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount(void)
{
cout<<"\ncount=";
cout<<count<<"\n";
}
};
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<<"\nAfter reading data";
a.getcount();
b.getcount();
c.getcount();
getch();
}
STATIC MEMBER FUNCTION
#include<conio.h>
#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<"\n Object Number is="<<code;
}
static void showcount()
{
cout<<"\nCount "<<count;
}
};
int test::count;
void main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
MULTILEVEL INHERITANCE
#include<conio.h>
#include<iostream.h>
class student
{
protected:
int rollno;
public:
void getno(int);
void putno();
};
void student::getno(int a)
{
rollno=a;
}
void student::putno()
{
cout<<"\nRoll no. is"<<rollno;
}
class test:public student
{
protected:
int sub1,sub2;
public:
void getmarks(int,int);
void putmarks();
};
void test::getmarks(int x,int y)
{
sub1=x;
sub2=y;
}
void test::putmarks()
{
cout<<"\nMarks in sub 1= "<<sub1;
cout<<"\nMarks in sub 2= "<<sub2;
}
class result:public test
{
int total;
public:
void display();
};
void result::display()
{
total=sub1+sub2;
putno();
putmarks();
cout<<"\nTotal marks is= "<<total;
}
void main()
{
result r1;
clrscr();
r1.getno(101);
r1.getmarks(45,50);
r1.display();
getch();
};
COPY CONSTRUCTURE
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code()
{
}
code(int a)
{
id=a;
}
code(code&x)
{
id=x.id;
}
void display()
{
cout<<id;
}};
void main()
{
clrscr();
code a(100);
code b(a);
code c=a;
code d;
d=a;
cout<<"\n ID of A";
a.display();
cout<<"\n ID of B";
b.display();
cout<<"\n ID of C";
c.display();
cout<<"\n ID of D";
d.display();
getch();
}
Destructor
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\nNumber of Object Created"<<count;
}
~alpha()
{
cout<<"\nNumber of object Destroyed"<<count;
count--;
}};
void main()
{
clrscr();
alpha a1,a2,a3,a4;
{
cout<<"\nEnter Block 1";
alpha a5;
{
cout<<"\nEnter block 2";
alpha a6;
}
getch();
}}
Friend Function
#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample S)
};
float mean(sample s)
{
return(s.a+s.b)/2.0;
}
void main()
{
clrscr();
sample x;
x.setvalue();
cout<<"mean value"<<mean(x);
getch();
}
Function Overloading
#include<iostream.h>
#include<conio.h>
int volume(int s)
{
return(s*s*s);
}
float volume(float r,int h)
{
return(3.14*r*r*h);
}
int volume(int l,int b,int h)
{
return(l*b*h);
}
void main()
{clrscr();
cout<<"\n"<<volume(10);
cout<<"\n"<<volume(2,5,8);
cout<<"\n"<<volume(10,7,5);
getch();
}
Unary Operator Overloading
#include<iostream.h>
#include<conio.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display()
{
cout<<x<<"\n";
cout<<y<<"\n";
cout<<z<<"\n";
}
void space::operator_()
{
x=-x;
y=-y;
z=-z;
}
};
void main()
{
clrscr();
space s;
s.getdata(10,-50,20);
cout<<"\n Before Operator Overloading\n";
s.display();
getch();
}
Exception Handling
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"Enter the value of A&B";
cin>>a>>b;
int x=a-b;
try
{ if(x!=0)
{ cout<<"result is "<<a/x;
}
else
{ throw(x);
}}
catch(int i)
{
cout<<"Exception caught"<<x;
}
getch();
}
Rethrow an Exception
#include<iostream.h>
#include<conio.h>
void divide(double x,double y)
{
try
{ if(y==0.0)
throw y;
else
cout<<"division ="x/y;
}
catch(double)
{ cout<<"caught an exception";
throw;
}}
void main()
{ try
{
divide(105,2.0)
divide(20.0,0.0)
}
catch(double)
{ cout<<"caught & double exception";
}
getch();}
Class Template
#include<iostream.h>
#include<conio.h>
template<class t1,class t2>
class test
{
t1 a;
t2 b;
public:
test(t1 x,t2 y)
{
a=x;
b=y;
}
void show ()
{
cout<<a<<b<<"\n";
}};
void main()
{clrscr();
test<float,int>test1(1.23,123);
test<int,char>test2(100,'c');
test1.show();
test2.show();
getch();
}
Function Template
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t&x,t&y)
{
t temp;
temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"\nvalue of m & n after swapping"<<n<<n;
swap(m,n);
cout<<"\nvalue of m & n after swapping"<<m<<n;
cout<<"\nvalue of a & b after swapping"<<a<<b;
cout<<"\nvalue of a & b after swapping"<<a<<b;
}
void main()
{
clrscr();
fun(100,110,15.2,18.9);
getch();
}
SIGNATURE