Professional Documents
Culture Documents
C++ Practical File
C++ Practical File
C++ Practical File
Page 2
OVERLOAD(+) OPERATOR.
19 CREATE A CLASS ITEM, HAVING TWO DATA MEMBERS X & Y,
OVERLOAD -(UNARY OPERATOR) TO CHANGE THE SIGN OF
X AND Y
20 CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM THIS
CLASS NAMELY, PROGRAMMER, ANALYST & PROJECT
LEADER. TAKE ATTRIBUTES AND OPERATIONS ON YOUR
OWN. WAP TO IMPLEMENT THIS WITH ARRAY OF
POINTERS.
21 CREATE TWO CLASSES NAMELY EMPLOYEE AND
QUALIFICATION. USING MULTIPLE INHERITANCE DERIVE
TWO CLASSES SCIENTIST AND MANAGER. TAKE SUITABLE
ATTRIBUTES & OPERATIONS. WAP TO IMPLEMENT THIS
CLASS HIERARCHY
22 WAP TO READ DATA FROM KEYWORD & WRITE IT TO THE
FILE. AFTER WRITING IS COMPLETED, THE FILE IS CLOSED.
THE PROGRAM AGAIN OPENS THE SAME FILE, READS
Page 3
1.WAP TO FIND THE GREATEST OF 3 NUMBERS.
CODING:
#include<iostream.h>
Void main() {
Int<<enter 3 numbers:;
Cin>>x>>y>>z;
Cout<<endl;
OUTPUT:
Enter the number:4 7 2
7 is largest
Page 4
2. WAP TO CALCULATE FACTORIAL OF A NUMBER.
CODING:
#include<iostream.h>
Void main() {
Unsigned long int num;
Cout<<Enter number:;
Cin>>num;
Cout<<endl;
Factorial(num);
OUTPUT:
Enter number:3
The factorial is 6
Page 5
3.WAP TO PRINT FIBONACCI SERIES OF n NUMBERS,WHERE N IS
GIVEN BY THE PROGRAMMER.
CODING:
#include<iostream.h>
While(count!=0) {
Cout<<fib<< ;
i=j;
j=fib;
fib=i+j;
count--;
}
}
Void main() {
Int n;
Cout<<enter the number of instances: ;
Cin>>n;
Cout<<endl;
Fibonacci(n);
}
OUTPUT:
Enter number of instances :8
1 1 1 2 3 5 8 13
Page 6
4.WAP TO CHECK WHETHER A NUMBER IS PRIME OR NOT
CODING:
#include<iostream.h>
For(i=0; i<num/2;i++) {
If(num%2==0) {
Count++;
}
}
If(count>1) {
Cout<<endl<<the number is not prime<<endl;
}
Else {
cout <<endl<<the number is prime<<endl;
}
}
Void main() {
Int num;
Prime(num);
}
OUTPUT:
Enter number: 42
The number is not prime
Page 7
5. WAP TO COUNT CHARACTERS IN A STRING.
CODING:
#include<iostream.h>
#include<string.h>
Void main() {
Char line[100];
Int count,I,space=0;
For(i=0;i<count;i== {
If(line[i]== ) {
Space++;
Count++;
}
}
Cout<<endl<<the numbers of characters is=<<count<<endl;
Cout<<endl<<The numbers of words is=<<space<<endl;
OUTPUT:
Please enter a sentence:my name is kartik
The number of characters is = 24
The number of words is = 5
Page 8
6.WAP TO READ A SET OF NUMBERS IN AN ARRAY AND TO FIND
THE LARGEST OF THEM.
CODING:
#include<iostream.h>
Void main() {
Int set[20],I,large=0,limit;
For(i=1;i<limit;i++ {
Cout<<enter position <<i<<:;
Cin>>set[i];
If(set[i]>large) {
Large=set[i];
}
}
Cout<<endl<<the largest value is<<large<<endl;
}
OUTPUT:
Enter no of entries:6
Enter position 1: 23
Enter position 2:12
Enter position 3:54
Enter position 4:76
Enter position 5:36
Enter position 6:69
The largest value is 76
Page 9
7.WAP TO IMPLEMENT BUBBLE SORT USING ARRAYS.
CODING:
#include<iostream.h>
Bubble_sort(matrix);
Cout<<the sorted list is: <<endl;
For(i=o;i<10;i++) {
Cout<<matrix[i]<< ;
}
}
While(k<10) {
For(j=0.j<10.j++) {
If(array[j]>array[j+1];
Temp=array[j+1];
Array[j]=array[j+1];
Array[j+1]=temp;
}
}
K++;
}
}
OUTPUT:
enter the element of an array: 5 7 3 8 12 6 9 2 4 15
the sorted list is: 2 3 4 5 6 7 8 9 12 15
Page
10
8.WAP TO READ A SET OF NUMBERS FROM KEYBOARD AND TO
FIND SUM OF ALL ELEMENTS OF THE GIVEN ARRAY USING A
FUNCTION
CODING:
#include<iostream.h>
For(i=1;i<=limit;i++) {
Cout<<enter position <<i<< : ;
Cin>>set[i];
}
Add(set,limit);
OUTPUT :
Enter number of entries:5
Enter position 1: 22
Page
11
Enter position 2: 56
Enter position 3: 12
Enter position 4: 33
Enter position 5: 9
Void main()
{int x,y;
Page
12
Cout<<please enter elements to be swapped;
Cin>>x>>y;
Swap(x,y);
{a=a+b;
b= a-b;
a=a-b;
OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16
Void main()
{int x,y;
Page
13
Cout<<please enter elements to be swapped;
Cin>>x>>y;
Swap(&x,&y);
Void swap(int*a.int*b)
Int temp;
Temp=*a;
*a=*b;
*b=temp;
OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16
#include<conio.h>
Int swap(int&n,int&b,int&c)
Page
14
{
Int s;
S=a+b+c;
Return s;
Void main()
Int a,b,c,s;
Clrscr();
Cin>>a>>b>>c;
S=swap(a,b,c);
Getch();
OUTPUT:
Enter the no 5 7 9
The sum is 21
#include<conio.h>
Page
15
Void main()
Int n,a[20],i.*p=NULL,t=0;
Clrscr();
For(i=0;i<n;i++)
Cin>>a[i];
p=&a[0];
do
{
cout<<endl<<*p;
p++;
t++;
While(t!=n);
getch();}
OUTPUT:
enter the no of elements 5
enter the element 9 7 3 10 4
9
Page
16
7
3
10
4
Page
17
Float calc(float r,float cons);
Void main()
Int length,height;
Float radius;
{return(cons*r*r);
{return(l*l);
{return (l*h*cons);
Page
18
OUTPUT:
enter radius of circle:5
the area of circle is:78.5
CODING:
#include<iostream.h>
Class student_info
Page
19
{
Private:
Char name[50],sex;
Int roll;
Public:
Void input()
Cout<<endl<<enter name:;
Cin>>day;
Cout<<endl<<enter sex:;
Cin>>sex;
Cin>>roll;
Class date
Public:
Int day,month,year;
Void insertdate()
Cin>>day;
Cin>>month;
Cin>>year;
Page
20
}
}d;
Void
Display()
Cout<<endl<<name:<<name;
Cout<<endl<<sex:<<sex;
Cout<<endl<<roll:<<roll;
Cout<<d.day<</<<d.month<</<<d.year<<endl;
};
Void main()
Student_info s1;
Cout<<endl<<Enter details---<<endl;
S1.input();
S1.d.insertdate();
Cout<<endl<<---INFORMATION---<<endl;
S1.display();
OUTPUT
---Enter Details---
Page
21
Enter Name: karan
Enter Sex: M
---INFORMATION---
Name: karan
Sex:M
Roll:1234
CODING :
#include<iostream.h>
Page
22
Public:
Fibonacci():limit(0)
{}
Fibonacci(int li):limit(li)
Int fibo=1,1=0,j=0;
Cout<<0<<;
While(limit!=0)
Cout<<fibo<<;
I=j;
J=fibo;
Fibo=i+j;
Limit--;
};
Void main()
Int n;
Cin>>n;
Fibonacci f(n);
Cout<<endl;
OUTPUT:
Page
23
0 1 1 2 3 5 8 13 21
CODING:
#include<iostream.h>
#include<conio.h>
Class cmplx
Page
24
Int real,imagin;
Cin>>real;
Cin>>imagin;
};
Cout<<RESULT:;
Cout<<[<<c1.real<<+ i<<c1.imagin;
Cout<<]+[<<c2.real<<+ i<<c2.imagin;
Cout<<]=<<c1.real+c2.real<<+ i<<c1.imagin+c2.imagin;\
Void main()
Complx op1,op2;
Op1.get();
Op2.get();
Sum(op1,op2); }
OUTPUT :
Page
25
INPUT OPERAND 2----
RESULT : [5+4i]+[7+2i]=12+6i
CODE :
#include<iostream.h>
#include<string>
Class string
Page
26
Char a[10],b[10];
Public:
Voud getdata()
Gets(a);
Gets(b);
Void operator==(string)
If(strcmp(a,b)==0)
Cout<<\nStrings equal\n;
Else
};
Int main()
String x;
x.getdata();
==x;
System(pause)
Return 0;
Page
27
OUTPUT :
Cleverfield
CODING :
#include<iostream.h>
#include<string>
Class mystring
Page
28
Char a[10,b[10];
Public:
Void getdata()
Gets(a);
Gets(b);
};
Int main()
Mystring x;
x.getdata();
+x;
System(pause);
Return 0;
OUTPUT :
KARAN SAGGU
Page
29
19.CREATE A CLASS ITEM, HAVING TWO DATA MEMBERS X & Y, OVERLOAD
-(UNARY OPERATOR) TO CHANGE THE SIGN OF X AND Y.
CODING:
#include<iostream.h>
Class item
Page
30
Int x,y;
Public:
Void getdata()
Cin>>x;
Cin>>y;
X= -x;
Y= -y;
Void display()
Cout<<\nx=<<x<<\ny=<<y;
};
Int main()
Item x;
x.getdata();
-x;
x.display();
cout<<endl;
Page
31
system(pause);
return 0;
OUTPUT :
X= -15
Y= 30
20. CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM THIS CLASS NAMELY,
PROGRAMMER, ANALYST & PROJECT LEADER. TAKE ATTRIBUTES AND
OPERATIONS ON YOUR OWN. WAP TO IMPLEMENT THIS WITH ARRAY OF
POINTERS.
CODING :
#include<iostream.h>
#include<conio.h>
#include<string.h>
Page
32
Class employee
Private:
Char name[20];
Int salary;
Public :
Strepy(name,nam);
Salary=sal;
Char* getName(void)
Return name;
Int getSal(void)
Return salary;
};
Private:
Char skill[10];
Public:
Page
33
{
putData(sal,name);
strepy(skill,skil);
Void display(void)
Cout<<\n\nProgrammer : \n;
Cout<<\nName : <<getName();
Cout<<\nSalary : <<getSal();
Cout<<\nSkill : <<skill;
};
Private:
Char type[10];
Public:
putData(sal,name);
strepy(type,typ);
Void display(void)
Page
34
Cout<<\nName :<<getName();
Cout<<\nsalary :<<getSal();
Cout<<\nType :<<type;
};
Private:
Char pName[10];
Public:
putData(sal,name);
strepy(pName,pNam);
Void display(void)
Clrscr();
Analyst al(ameeca,8600,post);
Programmer p1(ABC,12000,C++);
Pr1.display();
A1.display();
P1.display();
Getch();
Page
35
OUTPUT :
Project Leader :
Name : Akshey
Salary : 10000
Analyst :
Name : Ameeca
Salary : 8600
Type : Post
Programmer :
Name : ABC
Salary : 12000
Skill : C++
CODING :
#include<iostream.h>
#include<conio.h.
#include<stdio.h>
Class employee
Page
36
{
Char empname[10];
Int empid;
Public:
Void getemp()
Gets(empname);
Cout<<Enter Employee Id :;
Cin>>empid;
Void display()
Cout<<endl<<Name :<<empname;
Cout<<endl<<Id :<<empid;
};
Class qualification
Int exp;
Public:
Void getqual()
Cin>>exp;
Page
37
}
Void dispqual()
Cout<<endl<<Experiece=<<exp<<years;
};
Int projid;
Public:
Void getproject()
Cout<<Enter Project Id :;
Cin>>projid;
Void dispproj()
Cout<<endl<<PROJECT ID : <<projid;
};
Int groupid;
Public:
Void getgroup()
Cout<<Enter Group Id :;
Page
38
Cin>>groupid;
Void dispgroup()
Cout<<endl<<Group ID : <<groupid;
};
Void main()
Clrscr();
Scientist s;
Manager m;
Cout<<FOR SCIENTIST::::<<endl;
s.getemp();
s.getqual();
s.getproject();
s.display();
s.dispqual();
s.dispproj();
cout<<endl<<endl<<endl<<FOR MANAGER::::<<endl;
m.getemp();
m.getqual();
m.getgroup();
m.display();
Page
39
m.dispqual();
m.dispgroup();
getch();
OUTPUT :
FOR SCIENTIST :-
Name : Akash
Page
40
Id: 13012
Experience=6 years
PROJECT ID : 443
FOR MAMAGER::::
Name : Sameera
Id : 1445
Experience= 16 years
Group Id : 5002
22.WAP TO READ DATA FROM FROM KEYWORD & WRITE IT TO THE FILE. AFTER
WRITING IS COMPLETED, THE FILE IS CLOSED. THE PROGRAM AGAIN OPENS THE
SAME FILE, READS.
CODING:
#include<iostream.h>
#include<fstream.h>
Void main(void)
Char string[255];
Int ch;
Page
41
Cout<<\nMENU\n)Write To File\n2)Read From File\nEnter Choice : ;
Cin>>ch;
Switch(ch)
Case 1:
Cin>>string;
Ofstream fout;
Fout.open(myfile.txt);
Fout<<string;
Fout<<flush;
Fout.close();
Break;
Case 2:
Ifstream fin;
Fin.open(myfile.txt)
Fin>>string;
Fin.close();
Break;
Default:
Cout<<INVALID CHOICE;
Page
42
OUTPUT :
MENU
1)Write To File
Enter Choice: 1
MEMU
1)Write To File
Enter Choice: 2
Page
43