Академический Документы
Профессиональный Документы
Культура Документы
Guru Nanak Khalsa College For Women, Gujharkhan Campus, Model Town,Ludhiana
INDEX
S.NO
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.
CONTENTS
Wap to print a string. Wap to reading an object from file. Wap to find average of two numbers. Wap to swap to numbers. Wap to calculate simple interest. Wap to print fibonacci series. Wap to find the greatest among three. Wap to add,multiply,subtract or divide using switch statement. Wap to calculate area of circle using symbolic constants. Wap to calculate grow salary of an employee and HRA, PF, DA and other funds. Wap to calculate number is even or odd using if else statement. Wap to Area of triangle, circle & rectangle using switch statement Wap to calculate sum of five digit number Wap to reverse a number and check if it is in palindrome or not. Wap to check whether the character is in uppercase and lowercase. Wap to arrange three numbers in ascending order. Wap to whether character is uppercase or lowercase Wap to show dynamic initializing of variables. Wap to default arguments.
Functions
Wap to show use of scope resolution operator. Wap to show the use manipulators. Wap to show inline function.
23. 24.
Wap to add 2 times using function arguments. Wap to illustrate return by reference.
Classes
25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. Wap to show concept of classes. Wap to display complex numbers using class. Wap to display date, month, year using class. Wap to static member function. Wap to array of objects. Wap to illustrate objects and classes within classes. Wap to show another program using classes. Wap to use of class.
Operator overloading
Wap to operator overloading using friend function. Wap to operator overloading using binary operator. Wap to use unary operator. Wap to relational operator overloading. Wap to function overloading.
Inheritance
44. 45. Wap to single inheritance using public mode. Wap to single inheritance using private mode.
Wap to hybrid inheritance. Wap to multilevel inheritance. Wap to multiple inheritance. Wap to heirarichal inheritance. Wap to create a grade card using inheritance. Wap to create an employees salary structure using inheritance.
Pointers
52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. Wap to show the use of this pointer. Wap to show how to refer to pointers address by using pointers. Wap to show the manipulation of pointers. Wap to implement array of pointers. Wap to show array of pointers to objects. Wap to perform arithmetic operations on pointers. Wap to ilustrate how pinters work to access to derived class. Wap to show how to access the array contents using pointers. Wap to show how to access strings using pointers and arrays. Wap to show how to declare and define function pointers. Wap to show how to use pointers to objects. Wap to illustrate the use of virual functions using pointers.
Wap to specify field size with width(). Wap to show precision setting with precision(). Wap to show padding with fill().
Output:
C++ is better than C:
Output:
enter two numbers:23 34
Output:
enter value of a and b: 45 34 value in swapping
Output:
enter a,b,c: 34 23 67 D:-13142 INTERSET:-131
Output:
enter n: 45 45th number is-23075
Output:
enter the value of the number: 45 34 23 a is greatest
Output:
enter 2 numbers: 45 35
Output:
enter radius:6 radius:6 area of circle:113.040001
Program: To calculate grow salary of employee and HRA, PF, DA and other funds??
#include<iostream.h> #include<conio.h> class employee { int empno; char ename[30]; float basic,hra,pf,da,of,sal; public: void get() { cout<<" enter employee no"; cout<<" enter employee name"; cout<<"enter basic salary"; cin>>empno>>ename>>basic; } void show() { cout<<endl<<"employee no"<<empno; cout<<endl<<"employee name"<<ename; cout<<endl<<"basic salary"<<basic; cout<<endl<<"hra"<<hra; cout<<"da"<<da<<"\n"<<"pf"<<pf<<"\n"<<"other fund"<<of<<"\n" <<"gross salary"<<sal<<"\n";
} }; void calculate() { if(basic>0 && basic<10000) { hra=0.02*basic; da=0.03*basic; pf=0.0; of=0.0; } else { if(basic>30000 && basic<70000) { hra=0.1*basic; da=0.07*basic; pf=0.1*basic; of=0.02*basic; } else { hra=0.15*basic; da=0.1*basic; pf=0.2*bsal; of=0.05*basic; sal=basic+hra+da-of-pf;
Output:
enter number: 56 number:56 number 56 is even
Output:
1.area of triangle 2.area of circle 3.area of rectangle 45 enter value:34 34 area of triangle is:5.78 enter value:2 area of circle is:12.56 enter value:2 area of rectangle is:6042
Output:
enter 5-digit number: 12345 5-digit number:12345 sum of 5-digit number is:15
Output:
enter a number: 345 reversed number is:543 number is not palindrome
Output:
enter ch: B character :B character B is in upper case
Output:
enter values of three numbers: 10 20 30
int x,y; cout<<" enter value of x and y:\n"; cin>>x>>y; integer n1;
Output:
enter value of x and y: 10 20 A:0 B:0 A:10 :20
Output:
****************************************
final value=10056.786133
Output:
we are in inner block k=20
m=30 ::m=10
Output:
the value of a is :10 the value of a is :5
Output:
121.227898 1.257128
{ clrscr(); time T1,T2,T3; T1.get(45,89); T2.get(4,67); T3.sum(T1,T2); T1.display(); T2.display(); T3.display(); getch(); }
Output:
45:894:6751:36
Output:
value of y=10 value of x=25
Output:
object c object d a:100 b:299 a:200 b:300
n1.show(); getch(); }
Output:
COMPLEX NUMBER:10+20 COMPLEX NUMBER:40+70
Output:
DATE:17-9-1990 DATE:6-7-1984
test t1,t2; t1.set(); t2.set(); test:: showcount(); test t3; t3.set(); test ::showcount(); t1.show(); t2.show(); t3.show(); getch(); }
Output:
count:2 count:3
babita manager[size]; for(int i=0;i<size;i++) { cout<<"\n details of manager"<<i+1<<"\n"; manager[i].get(); } cout<<"\n"; for(i=0;i<size;i++) { cout<<"\n manager"<<i+1<<"\n;"; manager[i].display(); } getch(); }
Output:
details of manager1 enter name: baby enter age:30
{ for(i=0;i<n;i++) { sum=sum+price[i]; } cout<<" sum="<<sum; } } }; void main() { clrscr(); item t; t.get(); t.display(); getch(); }
Output:
a:100 b:200
Output:
enter name:babita enter age:23
vector operator*(int a,vector b) { vector c; for(int i=0;i<size;i++) c.v[i]=a*b.v[i]; return c; } vector operator *(vector b,int a) { vector c; for(int i=0;i<size;i++) c.v[i]=b.v[i]*a; return c; } istream & operator>>(istream &din,vector &b) { for(int i=0;i<size;i++) din>>b.v[i]; return(din); } ostream & operator<< (ostream &dout,vector &b) { dout<<"("<<b.v[0]; for(int i=1;i<size;i++) dout<<","<<b.v[i]; dout<<")"; return(dout);
} int x[size]={2,4,6}; void main() { clrscr(); vector m; vector n=x; cout<<"enter elements of vector m"<<"\n"; cin>>m; cout<<"\n"; cout<<"m="<<m<<"\n"; vector p,q; p=2*m; q=n*2; cout<<"\n"; cout<<"p="<<p<<"\n"; cout<<"q="<<q<<"\n"; getch(); }
Output:
enter elements of vector m 5 6 7
m=(5,6,7)
p=(10,12,14) q=(4,8,12)
} }; void main() { clrscr(); complex c1,c2,c3; c1=complex(2.5,3.5); c2=complex(1.6,2.7); c3=c1+c2; cout<<"c1="; c1.show(); cout<<"c2="; c2.show(); cout<<"c3="; c3.show(); getch(); }
Output:
c1=2.5+j3.5 c2=1.6+j2.7 c3=4.1+j6.2
}; void main() { clrscr(); space s; s.get(10 ,-20,30); cout<<"s:"; s.show(); -s; cout<<"s:"; s.show(); getch(); }
Output:
s:10 -20 30 s:-10 20 -30
} else { return(0); } }; void main() { clrscr(); data d1; data d2(4,6); data d3(32,25); if(d1>d2) { cout<<" object 1 is greater"; d1.display(); } else { cout<<" object 2 is greater"; d2.display(); }
if(d1>d3) { cout<<" obj1 is greater"; d1.dispaly(); } else { cout<<" obj3 is greater"; d3.display(); getch(); } }
Output:
1000 157.2595 112500
{ cout<<"y="<<y<<"\n"; } }; class c:public a,public b { int m,n; public: c(int e,int f,int g):a(e),b(f) { m=f; n=g; } void show_c() { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n";} }; void main() { clrscr(); c c1(5,45,56); cout<<"\n"; c1.show_a(); c1.show_b(); c1.show_c(); getch();
Output:
x=5 y=45 m=45 n=56
Output:
object1 m=45 n=100
void show_b() { cout<<"p="<<p<<"\n"; cout<<"q="<<q<<"\n"; } }; class c: public b,public a { int u,v; public: c(int j,int k,int l):a(j*2),b(l,l),u(j),v(k) { cout<<"\n c constructed"; } void show_c() { cout<<"u="<<u<<"\n"; cout<<"v="<<v<<"\n"; } }; void main() { clrscr(); c c1(4,6,9); cout<<"\n\n display member values"<<"\n\n"; c1.show_a(); c1.show_b();
c1.show_c(); getch(); }
Output:
b constructed a constructed c constructed
}; void string:: join (string &a,string &b) { length =a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); }; void main() { clrscr(); char *first="babita"; string name1(first), name2("choudhary"), name3("kaur"),s1,s2; s1.join(name1,name2); s2.join(s1,name3); name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); getch(); }
Output:
Jasmeen Gulati kaur Jasmeen Gulati Jasmeen Gulati Kaur
} }; void main() { clrscr(); code A(100); code B(A); code C=A; code D; D=A; cout<<"\n id of A:"; A.display_id(); cout<<"\n id of B:"; B.display_id(); cout<<"\n id of C:"; C.display_id(); cout<<"\n id of D:"; D.display_id(); getch(); }
Output:
id of A:100 id of B:100 id of C:100 id of D:100
} }; void main() { clrscr(); area a1; a1.display(); area a2(100,200); a2.display(); area a3(a1); a3.display(); getch(); }
Output:
area of rectangle:200 area of rectangle:20000 area of rectangle:200
{ return(a); } void B::show_a() { cout<<"\n a="<<a<<"\n"; } void D::mul() { c=b*get_a(); } void D::display() { cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n"; cout<<"c="<<c<<"\n"; } void main() { clrscr(); D d; d.get_ab(); d.mul(); d.show_a(); d.display(); d.b=20; d.mul();
d.display(); getch(); }
Output:
a=56 a=56 b=89 c=4984 a=56 b=20 c=1120
return(a); } void D::mul() { get_ab(); c=b*get_a(); } void D:: display() { cout<<"A:"<<get_a()<<"\n"; cout<<"B:"<<b<<"\n"; cout<<"\n C:"<<c<<"\n"; } void main() { clrscr(); D s1; s1.mul(); s1.display(); getch(); }
Output:
A:8 B:10 C:80
} }; class D { protected: int activities; float marks; public: void get_d() { cout<<"enter student activities ="; cin>>activities; cout<<" enter marks="; cin>>marks; } }; class C:public B,public D { float total; public: void sum() { total=sub1+sub2+activities; } void show() { cout<<"sum is "<<total;
} }; void main() { clrscr(); C obj; obj.get_a(); obj.get_b(); obj.get_d(); obj.sum(); obj.show(); getch(); }
Output:
enter rollno=1001
enter subject1:english
enter subject2:
} void show_b() { cout<<"Y="<<y<<"\n"; } }; class C:public B { int z; public: void get_c(int k) { z=k; } void display() { cout<<"Z="<<z<<"\n"; } }; void main() { clrscr(); C c1; c1.get_b(121); c1.get_c(131); c1.show_b(); c1.display();
getch(); }
Output:
Y=121 Z=131
} void display_b() { cout<<"\n B="<<b; } }; class C:public A, public B { int c; public: void sum() { c=a+b; } void show() { cout<<"\n C="<<c; } }; void main() { clrscr(); C c1; c1.get_a(100); c1.display(); c1.get_b(200); c1.display_b();
Output:
A=100 B=200 C=300
void show() { cout<<" square of no: A:"<<l; } }; class cube: public base { int l; public: void cub() { l=a*a*a; } void show_c() { cout<<"\n cube of no"<<a; } }; void main() { clrscr(); square s1; cube c1; s1.display(); s1.squ(); s1.show(); c1.get_a(47);
Output:
base A=0 square of no: A:0 base A=47 cube of no =47
float m1,m2,m3,tm,per; public: void getdata() { cout<<" enter marks of 3 subjects"; cin>>m1>>m2>>m3; } void calculate() { tm=m1+m2+m3; per=tm/3; } void display() { calculate(); cout<<"total marks"<<tm; cout<<" percentage"<<per; } }; void main() { result r; r.getdata(); r.display(); getch();
Output:
enter marks of 3 subjects 45 67 89
class result: public employee { input(); float hra,da,pf,of,gross salary; public: void getdata() { cout<<" enter<<hra<<da<<pf<<of<<gross salary"; cin>>hra>>da>>pf>>of>>gross salary; } void sum() { gross salary=hra+da-pf-of; } void display() { sum(); cout<<" basic salary"<<gsal; } }; void main() { clrscr(); result r; r.getdata(); r.display(); r.sum(); getch(); }
}; void main() { clrscr(); person p1("john",37.50), p2("amhed",29.0), p3("hebber",40.25); person p=p1.greater(p3); cout<<"elder person is:\n"; p.display(); p=p1.greater(p2); cout<<"elder person is:\n"; p.display(); getch(); }
Output:
elder person is: name:hebber age:40.25 elder person is: name:john age:37.5
Output:
The address of a : 0x8fa2fff4 The address of ptr1: 0x8fa2fff4 After incrementing the address values: The address of a: 0x8fa2fff8 The address of ptr1: 0x8fa2fff6
Output:
The value of a : 10
The value of a : 5
Output:
enter your favorite leisure pursuit:books
void printname(void) { cout<<name<<"\n"; } }; void main() { city *cptr[10]; int n=1; int option; do { cptr[n]=new city; cptr[n]->getname(); n++; cout<<"do oyu want to enter one more name?\n"; cout<<"(enter 1 for yes 0 for no):"; cin>>option; } while(option); cout<<"\n\n"; for(int i=1;i<=n;i++) { cptr[i]->printname(); } getch(); }
Output:
enter your favorite leisure pursuit:books your favorite pursuit is available here enter city name:hyderabad
do oyu want to enter one more name? (enter 1 for yes 0 for no):1 enter city name:ludhiana do oyu want to enter one more name? (enter 1 for yes 0 for no):1 enter city name:jalandhar do oyu want to enter one more name? (enter 1 for yes 0 for no):0
Output:
The array values are: 56 75 22 18 90
Value of ptr: 56 Value of ptr: 75 Value of ptr: 56 Value of ptr: 22 Value of ptr: 75 Value of ptr: 90
cout<<"bptr points to base object \n"; bptr->show(); dc derived; bptr=&derived; bptr->b=200; cout<<"bptr now points to derived object \n"; bptr->show(); dc *dptr; dptr=&derived; dptr->d=300; cout<<"dptr is derived type pointer \n"; dptr->show(); cout<<"using((dc *)bptr)\n"; ((dc *)bptr)->d=400; ((dc *)bptr)->show(); getch(); }
Output:
bptr points to base object b=100 bptr now points to derived object b=200 dptr is derived type pointer b=200 d=300
Output:
Enter the count 5 Enter the numbers one y one 2 4 7 5 8
Output:
TTTTeeeesssstttt The string reversed: tseT
Output:
1+2=3 3-2=1
const int size=2; void main() { item *p=new item[size]; item *d=p; int x,i;
cout<<"\nInput code and price for item\n" <<i+1; cin>>x>>y; p->getdata(x,y); p++; } for(i=0;i<size;i++) { cout<<"\nItem:"<<i+1<<"\n"; d->show(); d++; } getch(); }
Output:
Input code and price for item1 24 56
Item:2
code: 25 Price: 75
}; void main() { clrscr(); base b; derived d; base *bptr; cout<<"\nbptr points to base\n"; bptr=&b; bptr->display(); bptr->show(); cout<<"\n\n bptr points to derived\n"; bptr=&d;
Output:
bptr points to base
Output:
input text object oriented programming object oriented programming number of characters=27
Output:
enter city name:delhi city name:delhi enter city name:city name now:
return (0); }
Output:
P Pr Pro Prog Progr Progra Program Programm Programmi Programmin Programming Programmin Programmi Programm Program Progra Progr Prog Pro Pr P C++Programming
Output:
items cost total value 10 8 12 15 75 100 60 99 750 800 720 1485
grand total=3755
getch(); }
Output:
precision set to 3 digits
sqrt(10)=3.16228
sqrt(10)=3.162278(default setting)
Output:
<<<<1<<<<<<<<<1 <<<<2<<<<<<<0.5 <<<<3<<<<<0.333 >>>>4>>>>>>0.25 >>>>5>>>>>>>0.2 >>>>6>>>>>0.167
padding changed
#########12.346
Program: To illustrate how class objects can be written to and read from the disk files??
#include<iostream.h> #include<conio.h> #include<fstream.h> #include<iomanip.h> class INVENTORY { char name[10]; int code; float cost; public: void readdata(void); void writedata(void); }; void INVENTORY::readdata(void) { cout<<"enter name:"; cin>>name; cout<<"enter code:"; cin>>code; cout<<"enter cost:"; cin>>cost; } void INVENTORY::writedata(void)
{ cout<<setiosflags(ios::left) <<setw(10)<<name <<setiosflags(ios::right) <<setw(10)<<code <<setprecision(2) <<setw(10)<<cost <<endl; } void main() { clrscr(); INVENTORY item[3]; fstream file; file.open("stock.dat",ios::in|ios::out); cout<<"enter details for three items\n"; for(int i=0;i<3;i++) { item[i].readdata(); file.write((char *) & item[i],sizeof(item[i])); } file.seekg(0); cout<<"\noutput\n\n"; for(i=0;i<3;i++) { file.read((char *) & item[i],sizeof(item[i])); item[i].writedata();
} file.close(); getch(); }
Output:
enter details for three items enter name:C++ enter code:101 enter cost:175 enter name:Fortran enter code:102 enter cost:150 enter name:Java enter code:225 enter cost:225
output
} infile.close(); getch(); }
Output:
175.50 153.00 167.25 160.70
cerr<<"unable to create file\n"; exit(1); } while(!infile.eof()) { ch=(char)infile.get(); uch=toupper(ch); outfile.put(uch); }; infile.close(); outfile.close(); }
Output:
input file this is a test program output file THIS IS A TEST PROGRAM