Você está na página 1de 19

//class employee for reading and displaying the employee information getinfo.

//display info method will be where getinfo will be private.


Practical 1A
#include<iostream.h>
#include<conio.h>
class employee
{
int id;
char name[31];
float salary;
void getinfo()
{
cout<<"Enter the name::\n";
cin>>name;
cout<<"Enter the id::\n";
cin>>id;
cout<<"\n Enter the salary:";
cin>>salary;
}
public:
void display info()
{
getinfo();
cout<<"\n The name is ::\n"<<name;
cout<<"\n The id is ::\n"<<id;
cout<<"\n The salary is:"<<salary;
}};
void main()
{
clrscr();
employee E1;
E1.display info();
getch();
}

Practical 1B
//class sudent containing getdata,display data as two methods which will used for
reading and displaying student info.

#include<iostream.h>
#include<conio.h>
class employee
{
int r;
char n[31];
void getdata()
{
cout<<"Enter the name::\n";
cin>>n;
cout<<"Enter the roll no::\n";
cin>>r;
}
public:
void display ()
{
get data();
cout<<"\n The name is ::\n"<<n;
cout<<"\n The roll no ::\n"<<r;
}};
void main()
{
clrscr();
student s1;
s1.display();
getch();
}

Practical 1C
//factorial reverse polindrome armstrong

#include<iostream.h>
#include<conio.h>
class demo
{
int i,n,num,rev,d,c,sum,fact;
void read no()
{
cout<<"\n Enter the number:";
cin>>num;
}
public:
void factorial()
{
read no();
fact=1;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<"\n Factorial of the number is :"<<fact;
}
void reverse no()
{
read no();
n=num;
rev=0;
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;}
cout<<"\n Reverse of the number is:"<<rev;
}
void polindrom()
{
if(num==rev)
{cout<<"\n The number is polindrom";
}
else
{cout<<"\n The number is not polindrome";
}}
void armstrong()
{
n=num;
sum=0;
while(n>a)
{
d=n%10;
c=d*d*d;
sum=sun+c;
n=n/10;
}
if(num==sum)
{
cout<<"\n The number is an armstrong";
}else
{
cout<<"\n The number is not an aromstrong";
}}};
void main()
{
clrscr();
demo d;
d.factorial();
d.reverse no();
d.polindrom();
d.armstrong();
getch();
}
Practical 1D
//Demonstrate function defination outside the class excessing class member in
function defination.

#include<iostream.h>
#include<conio.h>
class karan
{
public:
int a,b,c;
getdata();
};
karan::getdata()
{
cout<<"\n Enter the number:";
cin>>a>>b;
c=a+b;
cout<<"\n Addition of number is:";
}
void main()
{
clrscr();
karan s;
s.getdata();
getch();
}
Practical no 3A public
#include<iostream.h>
#include<conio.h>
class teacher
{
int n;
public:
int m;
void setdata()
{
cout<<"\n Enter the value of n=";
cin>>n;
cout<<"\n Enter the value of m=";
cin>>m;
}
int getdata()
{
return n;
}
void display()
{
cout<<"Display the N="<<n;
}};
class student:public teacher
{
int a;
public:
void addition()
{
a=m+getdata();
}void show()
{
cout<<"\n Display the N="<<getdata();
cout<<"\n Display the M="<<m;
cout<<"\n show the addition A="<<a;
}};
void main()
{
clrscr();
student s1;
s1.setdata();
s1.addition();
s1.display();
s1.show();
getch();
}

Practical 2A
// friend function for addition of complex numbers using single class
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c,d;
public:
void getdata();
void display();
friend void addition(complex);
};
void complex::getdata()
{
cout<<"\n Enter the value of a,b,c and d:";
cin>>a>>b>>c>>d;
}
void complex::display()
{
cout<<"\n First comex number is:"<<a<<"+"<<b<<"i";
cout<<"\ Second complex number is:"<<c<<"+"<<d<<"i";
}
void addition (complex c1)
{
int add=0,Add=0;
add=c1.a+c1.c;
Add=c1.b+c1.d;
cout<<"\n Addition of two complex number is:"<<add<<"+"<<Add"i";
}
void main()
{
clrscr();
complex c1;
c1.getdata();
c1.display();
addition(c1);
getch();
}

Practical 2B

//friend function for add two different distance and display sum using two class
#include<iostream.h>
#include<conio.h>
class karan
class manish
{
int a;
public:
void getdata()
{
cout<<"\n Enter the value of a:";
cin>>a;
}
friend void add(manish,karan);
};
class karan
{
int b;
public:
void readdata()
{
cout<<"\n Enter the value of b:";
cin>>b;
}
friend void add(manish,karan);
};
void add(manish A,karan K)
{
int add;
add=A.a+K.b;
cout<<"\n First distance is:"<<A.a<<"km";
cout<<"\n Second distance is:"<<K.b<<"km";
cout<<"\n Addition of two distance is:"<<add<<"km";
}
void main()
{
clrscr();
manish A;
A.getdata();
karan K;
K.readdata();
add(A,K);
getch();
}

Practical 2C
Friend function adding two matrix from diferent class

#include<iostream.h>
#include<conio.h>
class matrix 1
class matrix 2
{
int i,j,q[20][20];
public:
void getdata()
{
cout<<"\n Enter the value for 2*2 matrix A:";
for(j=1;j<=2;j++)
{
for(j=1;j<=2;j++)
{
cin>>a[i][j];
}}}
void display
{
cout<<"\n matrix A:\n";
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}}
friend void addition(matrix 2,matrix 1);
};
class matrix 1
{
int i,j,b[20][20];
public:
void readdata()
{
cout<<"\n Enter the value for 2*2 matrix B:";
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cin>>b[i][j];
}}}
void show()
{
cout<<"\n Matrix B:\n";
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n";
}}
friend void addition (matrix 2,matrix 1);
};
void addition(matrix 2 m1,matrix 2 m2)
{
int add[20][20],i,j;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
add[i][j]=m1.a[i][j]+m2.b[i][j]
}}
cout<<"\n Addition of two matrix is :\n";
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cout<<"\t"<<add[i][j];
}
cout<<"\n";
}}
void main()
{
clrscr();
matrix 2 m1;
matrix 1 m2;
m1.getdata();
m1.display();
m2.readdata();
m2.show();
addition(m1,m2);
getch();
}

Praactical 3A
//single level inheritance using public class
#include<iostream.h>
#include<conio.h>
class teacher
{
int n;
public:
int m;
void setdata()
{
cout<<"\n Enter the value of n=";
cin>>n;
cout<<"\n Enter the value of m=";
cin>>m;
}
int getdata()
{
return n;
}
void display()
{
cout<<"Display the n="<<n;
}};
class student:public teacher
{
int a;
public:
void addition()
{
a=m+getdata();
}
void show()
{
cout<<"\n Display tbe N="<<getdata();
cout<<"\n Display the M="<<m;
cout<<"\n Show the addition A="<<a;
}};
void main()
{
clrscr();
student s1;
s1.setdata();
s1.addition();
s1.display();
s1.show();
getch();
}

Practical 3B
//Design a class for multiple inheritance
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void set (int x)
{
a=x;
}};
class B
{
protected:
int b;
public:
void set a(int y)
{
b=y;
}};
class C: public A,public B
{
public:
void show()
{
cout<<"\nA="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<a+b;
}};
void main()
{
clrscr();
C n;
n.set(10);
n.set a(20);
n.show();
getch();
}

Practical 3C
//Implement the hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class number
{
public:
int num;
void set(int x)
{
num=x;
}};
class square:public number
{
public:
square()
{
set(10);
cout<<"\n square="<<num*num;
}};
class cube:public number
{
public:
cube()
{
set(20);
cout<<"\n cube="<<num*num*num;
}};
void main()
{
clrscr();
square s;
cube c;
getch();
}

Practical no 4A
//class complex for add two complex numbers and also show the use of constructor
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c,d,add,Add;
public:
complex(int p,int q,int r,int s);
void display();
void getdata();
};
complex::complex(int p,int q,int r,int s)
{a=p;
b=q;
c=r;
d=s;}
void complex::getdata()
{
add=a+c;
Add=b+d;
}
void complex::display()
{
cout<<"\n first complex number is:"<<a<<"+"<<b<<"i";
cout<<"\nSecond complex number is:"<<c<<"+"<<d<<"i";
cout<<"\n addition of two complex is:"<<add<<"+"<<Add<<"i";
}
void main()
{
clrscr();
complex p(1,2,3,4);
p.getdata();
p.display();
getch();
}

Practical 4B
//area() volume()
#include<iostream.h>
#include<conio.h>
class geometry
{
public:
void area(int a)
{
cout<<"\n area of circle="<<3.14*a*a;
}
void area(int a, int b)
{
cout<<"\n area of rectangle="<<a*b;
}
void volume(int c)
{
cout<<"\n volume of cube="<<c*c*c;
}};
void main()
{
clrscr();
geometry g;
g.area(4);
g.area(4,5);
g.volume(6);
getch();
}

Practical 4C
//class static demo to show the implementation of static variable & static function
#include<iostream.h>
#include<conio.h>
class staticdemo
{
int id;
static int c;
public:
void getdata()
{
cout<<"\n Enter the id:";
cin>>id;
c=c+1;
}
void display()
{
cout<<"\n id="<<id;
cout<<"\n result="<<c;
}
static void show_count()
{
cout<<"\n total number of employee:"<<c;
}};
int staticdemo::c;
void main()
{
clrscr();
staticdemo s1;
s1.getdata();
s1.display();
staticdemo::show_count();
getch();
}

Practical 5A
// overload the operator unary(-)
#include<iostream.h>
#include<conio.h>
class A
{
int a,b;
public:
A(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\n Substraction="<<a-b;
}
void operator-()
{
a=-a;
b=-b;
}};
void main()
{
clrscr();
A a(5,3);
a.display();
-a;
a.display();
getch();
}

Practical 5B
//overload the operator(+) for adding the timing of two clocks and pass object as
argument.
#include<iostream.h>
#include<conio.h>
class time
{
public:
int h,m,s;
void gettime()
{
cout<<"Enter the hour(0-11)=";
cin>>h;
cout<<"\nEnter the minute(0-59)=";
cin>>m;
cout<<"\n Enter the seconds (0-59)=";
cin>>s;
}
void display();
time operator +(time t)
{
time z;
int a=s+t.s;
z.s=a%60;
int b=(a/60)+m+t.m;
z.m=b%60;
z.h=(b/60)+h+t.h;
return z;
}
};
void time::display()
{
cout<<"\n sum of both time="<<h<<":"<<m<<":"<<s;
}
void main()
{
clrscr();
time t1,t2,t3;
t1.gettime();
cout<<"\n";
t2.gettime();
t3=t1+t3;
t3.display();
getch();
}

Practical 5C
//overload the + for two stings for eg."py" + "tbon"= python
#include<iostream.h>
#include<conio.h>
class string
{
char str[20];
public:
void input()
{
cout<<"Enter tbe string=";
cin>>str;
}
void out()
{
cout<<"\n Complete string is="<<str;
}
string operator+(string s1)
{
string s;
str py(s.string.str);
str thon(s.str,st.str);
return s;
}};
void main()
{
clrscr();
string s1,s2,s3;
s1.input();
s2.input();
s3=s1+s2;
s3.output();
getch();
}

Practical 6B
//show tbe use of virtual function
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void draw()
{
cout<<"Base class";
}};
class circle:public shape
{
public:
void draw()
{
cout<<"\n Circle";
}};
class squares: public shape
{
public:
void draw()
{
cout<<"Square";
}};
void main()
{
shape *ptr;
circle c;
square s;
ptr=&c
ptr=>draw();
ptr=&s
ptr=>draw();
getch();
}

Practical 6C
//abstact class

#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void draw()=0;
};
class circle:public shape
{
public:
void draw()
{
cout<<"circle";
}};
class square:public shape
{
public:
void draw()
{
cout<<"square";
}};
void main()
{
shape *ptr
circle C;
square S;
ptr=&c;
ptr=> draw();
ptr =&s;
ptr=>draw();
getch();
Practical 7A
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
string str1("hello"" ");
string str2("world");
string str3=str 1+str 2;
cout<<"concatination is:"<<str3;
cout<<"\nThe lengtb of str1 is:"<<str1.length();
cout<<"\n the length of str2 is:"<<str2.length();
return 0;
}

Practical 8A
// show the implementation of exception handling
#include<ipstream.h>
using namespace std;
int main()
{
int roll no;
cout<<"\n enter your roll no:";
cin>>roll no;
try
{
if(roll no>0 && roll no<69)
{
throw(roll no);
}
else
{
cout<<"\n invalid roll no:";
}}
catch (int a)
{
cout<<"\n valid roll no.";
}return 0;
}

Practical 8B
//show the implementation of multiple catch in exception handling

#include<iostream.h>
using namespace std;
int main()
{
int a;
vout<<"\n enter the number:";
cin>>a;
try
{
throw(a);
}
catch(int (A)
{
cout<<"}n it is number"<<a;
}
catch(char y)
{
cout<<"\n it is charcter"<<a;
}
return 0;
}

Practical 9A
//open a file in read mode and display the total number of words and line

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class a
{
int wc,lc;
char c;
public:
void set()
{
wc=1;
lc=1;
ifstream fread("p1.cpp");
while(fread)
{
fread.get(a);
if(c!=' '||a!='\n')
{
x++;}
if(a=='\n')
{
y++;
}}
fread.close();
}
void show((
{
cout<<"\n total number of words="<<x;
cout<<"\n total number of lines="<<y;
}};
void main()
{
clrscr();
demo d;
d.take();
d.show();
getch();
}

Practical 9B
// handle multiple file and file operation

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class karan
{
public:
char l[60];
void get()
{
fstream p1("prac1c.cpp", ios::out);
p1<<"\n the first line of prac 1c.cpp files:";
p1<<"\n tbe second line of prac1c.cpp files:";
p1.close();
}
void display()
{
fstream p2("prac1c.cpp",ios::in);
while(p2)
{
p2.getline(l,50);
cout<<l;
}
p2.close();
}
void getdata()
{
fstream.p1("prac1c.cpp",ios::out);
p1<<"\n the first line of prac1c.cpp file;";
p1<<"\n the second line of prac1c.cpp file;";
p1.close();
}
void showing()
{
fsyream p2("prac2c.cpp",ios::in);
while(p2)
{
p2.getline(l,50);
cout<<l;
}
p2.close();
}};
void main()
{
clrscr();
{
clrscr();
karan A;
A.get();
A.display();
A.getdata();
A.showing();
getch();
}
Practical 10A
//implementation of template class for swap function

#include<iostream.h>
#include<conio.h>
template<class T>
T swap(T &n1,T &n2)
{
T temp;
temp=n1;
n1=n2;
n2=temp;
}
int main()
{
clrscr();
int i1=1,i2=2;
float f1=1.1,f2=2.1;
cout<<"before passing data to function template:\n";
cout<<"i1="<<i1<<"\ni2="<<i2;
cout<<"\nf1="<<f1<<"\nf2="<<f2;
swap(i1,i2);
swap(f1,f2);
cout<<"\n\n after passing data to function template:";
cout<<"\ni1="<<i1<<"\ni2="<<i2;
cout<<"\nf1="<<f1<<"\nf2="<<f2;
getch();
}

Practical 10B
// template class for ascendind sorting any ten integers and descending

#include<iostream.h>
#include<conio.h>
template<class T>
class sort
{
public:
T s()
{
a[30],i,j;
cout<<"\n enter no";
for(i=0;i<10;i++)
{
CIN>>a[i];
}
cout<<"\nascending order\n";
for(i=0;i<10;i++)
(
for(j=0;j<10;j++)
{
if(a[i]<a[j])
{
T m;
m=a[i];
a[i]=a[j];
a[j]=m;
}}}
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n Descending order\n";
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[i]>a[j])
{
T m;
m=a[i];
a[i]=a[j];
a[j]=m;
}}}
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}};
void main()
{
clrscr();
sort<float>s1;
s1.s();
getch();
}

Você também pode gostar