BASICs
ASSIGMENT-1
======================================================================================================================
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
cout<<"c++ is better than c.";
getch();
return 0;
}
/*============output=====
c++ is better than c.
*/
**************************************************************************************
#include<iostream.h>
#include<conio.h>
int main()
{
float num1,num2,sum,average;
clrscr();
cout<<"enter two numbers=";
cin>>num1;
cin>>num2;
sum=num1+num2;
average=sum/2;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average;
getch();
return 0;
}
/*---------------------
enter two numbers=78
23
sum=101
average=50.5
*/
********************************************************************************************
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata();
void display();
};
void person::getdata()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person::display()
{
cout<<"\n Your name:"<<name;
cout<<"\n Your age:"<<age;
}
int main()
{
person p;
p.getdata();
p.display();
getch();
return 0;
}
/*===========output========
Enter name:neha
Enter age:20
Your name:neha
Your age:20
*/
*****************************************************************************************************
#include<iostream.h>
#include<conio.h>
int m=10;
int main()
{
int m=20;
{
int k=m;
int m=30;
cout<<"We are in inner block\n";
cout<<"k="<<k<<"\n";
cout<<"m="<<m<<"\n";
cout<<"::m="<<::m<<"\n";
}
cout<<"\n We are in outer block\n";
cout<<"\n m="<<m<<"\n";
cout<<"::m="<<::m<<"\n";
getch();
return 0;
}
/*=========output=========
We are in inner block
k=20
m=30
::m=10
We are in outer block
m=20
::m=10
*/
********************************************************************************************************
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int data1;
char data2;
public:
void set(int i,char c)
{
data1=i;
data2=c;
}
void display()
{
cout<<"data1="<<data1;
cout<<"data2="<<data2;
}
};
int main()
{
sample *s_ptr;
try
{
s_ptr=new sample;
}
catch(bad_alloc ba)
{
cout<<"Bad Allocation occurred...the program will terminate now..";
return 1;
}
s_ptr->set(25,'A');
s_ptr->display();
delete s_ptr;
getch();
}
*****************************************************************************************************************
#include<iostream.h>
#include<conio.h>
int main()
{
int intvar=25;
float floatvar=35.87;
cout<<"intvar="<<intvar;
cout<<"\n floatvar="<<floatvar;
cout<<"\n float(intvar)="<<float(intvar);
cout<<"\n int(floatvar)="<<int(floatvar);
getch();
return 0;
}
/*=============output============
intvar=25
floatvar=35.869999
float(intvar)=25
int(floatvar)=35
*/
*****************************************************************************************************************************
[3] Create a class for Bank account with the following data members.
(1) Name of depositor
(2) Account number
(3) Type of account
(4) Balance member functions
a. To assign initial values
b. To deposit an amount in a particular account
c. To withdraw an amount after checking the balance
d. To display name and balance
Write a program to manage at least 10 customers who can deal with deposit and withdraw amount and calculate the current balance.
Ans:-
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
class bank
{
char name[20];
int acno;
char actype[20];
int bal;
public :
void opbal(void);
void deposit(void);
void withdraw(void);
void display(void);
};
void bank :: opbal(void)
{
cout<<endl<<endl;
cout<<"Enter Name :-";
cin>>name;
cout<<"Enter A/c no. :-";
cin>>acno;
cout<<"Enter A/c Type :-";
cin>>actype;
cout<<"Enter Opening Balance:-";
cin>>bal;
}
void bank :: deposit(void)
{
cout<<"Enter Deposit amount :-";
int deposit=0;
cin>>deposit;
deposit=deposit+bal;
cout<<"\nDeposit Balance = "<<deposit;
bal=deposit;
}
void bank :: withdraw(void)
{
int withdraw;
cout<<"\nBalance Amount = "<<bal;
cout<<"\nEnter Withdraw Amount :-";
cin>>withdraw;
bal=bal-withdraw;
cout<<"After Withdraw Balance is "<<bal;
}
void bank :: display(void)
{
cout<<endl<<endl<<endl;
cout<<setw(50)<<"DETAILS"<<endl;
cout<<setw(50)<<"name "<<name<<endl;
cout<<setw(50)<<"A/c. No. "<<acno<<endl;
cout<<setw(50)<<"A/c Type "<<actype<<endl;
cout<<setw(50)<<"Balance "<<bal<<endl;
}
void main()
{
clrscr();
bank o1;
int choice;
do
{
cout<<"\n\nChoice List\n\n";
cout<<"1) To assign Initial Value\n";
cout<<"2) To Deposit\n";
cout<<"3) To Withdraw\n";
cout<<"4) To Display All Details\n";
cout<<"5) EXIT\n";
cout<<"Enter your choice :-";
cin>>choice;
switch(choice)
{
case 1: o1.opbal();
break;
case 2: o1.deposit();
break;
case 3: o1.withdraw();
break;
case 4: o1.display();
break;
case 5: goto end;
}
}while(1);
end:
getch();
}
Choice List
1) To assign Initial Value
2) To Deposit
3) To Withdraw
4) To Display All Details
5) EXIT
Enter your choice :-1
Enter Name :-abc
Enter A/c no. :-20
Enter A/c Type :-saving
Enter Opening Balance:-5000
Enter your choice :-2
Enter Deposit amount :-550
Deposit Balance = 5550
Choice List
1) To assign Initial Value
2) To Deposit
3) To Withdraw
4) To Display All Details
5) EXIT
Enter your choice :-
3
Balance Amount = 5550
Enter Withdraw Amount :-550
After Withdraw Balance is 5000
Choice List
1) To assign Initial Value
2) To Deposit
3) To Withdraw
4) To Display All Details
5) EXIT
1) To assign Initial Value
2) To Deposit
3) To Withdraw
4) To Display All Details
5) EXIT
Enter your choice :-4
DETAILS
name abc
A/c. No. 20
A/c Type saving
Balance 5000
Choice List
1) To assign Initial Value
2) To Deposit
3) To Withdraw
4) To Display All Details
5) EXIT
//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
Chapter – 5 Program – 5.1 to 5.5 #include<iostream.h> #include<conio.h> class item { int no1; float cost; public: void getdata(int a , float b); void putdata() { cout<<" Number : " <<no1<<endl; cout<<"cost : "<<cost<<endl; } }; void item :: getdata(int a , float b) { no1=a; cost=b; } int main() { item x; cout<<"\n Object x" <<endl; x.getdata(100 , 299.95); x.putdata(); item y ; cout<<"Object y "<<endl; y.getdata(200,175.6); y.putdata(); getch(); return 0; } /*============output======== Object x Number : 100 cost : 299.950012 Object y Number : 200 cost : 175.600006 */ **************************************************************** 2] #include<iostream.h> #include<conio.h> class set { int m,n; public: void input(); void display(); int largest(); }; int set::largest() { if(m>=n) return(m); else return(n); } void set::input() { cout<<"Enter the value of m "<<endl; cin>>m; cout<<"Enter the value of n "<<endl; cin>>n; } void set::display() { cout<<"the largest value is="<<largest(); } int main() { set A; A.input(); A.display(); getch(); return 0; } /*==============output================ Enter the value of m 10 Enter the value of n 50 the largest value is=50 */ ************************************************************************ #include<iostream.h> #include<conio.h> const m=50; class items { int itemcode[m]; float itemprice[m]; int count; public: void cnt() { count=0; } void getitem(); void displaysum(); void remove(); void displayitems(); }; void items::getitem() { cout<<"Enter item code:"; cin>>itemcode[count]; cout<<"Enter item cost:"; cin>>itemprice[count]; count++; } void items::displaysum() { float sum=0; for(int i=0;i<count;i++) sum=sum+itemprice[i]; cout<<"\n total value:"<<sum<<"\n"; } void items::remove() { int a; cout<<"Enter item code:"; cin>>a; for(int i=0;i<count;i++) if(itemcode[i]==a) itemprice[i]=0; } void items ::displayitems() { cout<<"\n code price\n"; for(int i=0;i<count;i++) { cout<<"\n"<<itemcode[i]; cout<<" "<<itemprice[i]; } cout<<"\n"; } int main() { items order; order.cnt(); int x; do { cout<<"\n You can do the following;" <<"Enter appropriate number\n"; cout<<"\n1 :Add an item"; cout<<"\n2 :Display total value"; cout<<"\n3 : Delete an item:"; cout<<"\n4 : Display all item"; cout<<"\n5 : Quite"; cout<<"\n\n What is your option?"; cin>>x; switch(x) { case 1: order.getitem(); break; case 2: order.displaysum(); break; case 3:order.remove(); break; case 4: order.displayitems(); break; case 5: break; default : cout <<"Error in input ;try again\n"; } }while(x!=5); getch(); return 0; } /*==============output============= You can do the following;Enter appropriate number 1 :Add an item 2 :Display total value 3 : Delete an item: 4 : Display all item 5 : Quite What is your option?1 Enter item code:256 Enter item cost:230 You can do the following;Enter appropriate number 1 :Add an item 2 :Display total value 3 : Delete an item: 4 : Display all item 5 : Quite What is your option?2 total value:230 You can do the following;Enter appropriate number 1 :Add an item 2 :Display total value 3 : Delete an item: 4 : Display all item 5 : Quite What is your option?4 code price 256 230 You can do the following;Enter appropriate number 1 :Add an item 2 :Display total value 3 : Delete an item: 4 : Display all item 5 : Quite What is your option? *******************************************************/ #include<iostream.h> #include<conio.h> class item { static int count; int number; public: void getdata(int a) { number=a; count ++; } void getcount() { cout<<"count : "; cout<<count<<endl; } }; int item :: count; int main() { item a, b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); cout<<"After reading the data "<<endl; a.getcount(); b.getcount(); c.getcount(); getch(); return 0 ; } /*==============output============= count : 0 count : 0 count : 0 After reading the data count : 3 count : 3 count : 3 */ ************************************************************** #include<iostream.h> #include<conio.h> class test { int code; static int count ; public: void setcode() { code= ++count; } void showcode() { cout<<"object number : "<<code<<endl; } static void showcount() { cout<<"Count: " <<count<<endl; } }; int test :: count; int main() { test t1, t2; t1.setcode(); t2.setcode(); test :: showcount(); test t3; t3.setcode(); test :: showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); return 0; } //##############################OUTPUT################################ /* Count: 2 Count: 3 object number : 1 object number : 2 object number : 3 #################################\\ **************************************************************************-----------------------------------------------------------------------------------------------------------------------------------------------------[1]Write a program to declare a class string with member char * str and needed member functions, also write a member function to convert string object to uppercase. ans:- #include<iostream.h> #include<string.h> #include<conio.h> #include<stdio.h> class strng { char *str; public: strng() { } void setdata() { cin >>str; } void uppercase() { int i; for(i=0;i<strlen(str);i++) str[i]=str[i]-32; } void getdata() { cout<<str; } }; void main() { clrscr(); strng s; cout<< "Enter the String:"; s.setdata(); s.uppercase(); printf("\n\n%c\t%d\n\n",'a','a'); printf("\n\n%c\t%d\n\n",'A','A'); cout<< "Your String is Uppercase:"; s.getdata(); getch (); } /* Enter the String:jaydip a 97 A 65 Your String is Uppercase:JAYDIP */ ******************************************************************************************************************************************* Operator Overloading =========================== [2]WAP to multiply two matrices A and B by using class matrix. ANS:- #include<iostream.h> #include<conio.h> int main() { int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k; clrscr(); cout<<"Enter rows and columns of first matrix:"; cin>>m>>n; cout<<"Enter rows and columns of second matrix:"; cin>>p>>q; if(n==p) { cout<<"\nEnter first matrix:\n"; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>a[i][j]; cout<<"\nEnter second matrix:\n"; for(i=0;i<p;++i) for(j=0;j<q;++j) cin>>b[i][j]; cout<<"\nThe new matrix is:\n"; for(i=0;i<m;++i) { for(j=0;j<q;++j) { c[i][j]=0; for(k=0;k<n;++k) c[i][j]=c[i][j]+(a[i][k]*b[k][j]); cout<<c[i][j]<<" "; } cout<<"\n"; } } else cout<<"\nSorry!!!! Matrix multiplication can't be done"; getch(); return 0; } /************************************************** Output : Enter rows and columns of first matrix: 2 2 Enter rows and columns of second matrix: 2 2 Enter first matrix: 2 3 1 3 Enter second matrix: 2 5 3 4 The new matrix is: 13 22 11 17 ***********/ ***************************************************************************************************************************************** [3] Using unary plus operator (++) create Fibonacciseries of data member of class fibo for n numbers. ( 0, 1, 1, 2, 3, 5, 8, 13,.) ANS:- #include<iostream.h> #include<conio.h> class fibo { int n,a,b,i,c; public: void getdata(); void disp_data(); }; void fibo::getdata() { cout<<"Enter limit of the series: "; cin>>n; } void fibo::disp_data() { if(n>0) { a=-1; b=1; cout<<"\nFirst "<<n<<" terms of fibonacci series are:\n"; for(i=1;i<=n;i++) { c=a+b; cout<<c<<" "; a=b; b=c; } } else { cout<<"Limit must be greater than zero"; } } void main() { fibo F; clrscr(); F.getdata(); F.disp_data(); getch(); } /***************************************************** Output : Enter limit of the series: 6 First 6 terms of fibonacci series are: 0 1 1 2 3 5 ****************************************************/ ************************************************************************************************************************************** [4]Subtract two time objects, where class time has hour and minute. ANS:- #include<iostream.h> #include<conio.h> class time { int h,m,s; public: time() { h=0, m=0; s=0; } void getTime(); void show() { cout<< h<< ":"<< m<< ":"<< s; } time operator-(time); //overloading '+' operator }; time time::operator-(time t1) //operator function { time t; int a,b; a=s-t1.s; t.s=a%60; b=(a/60)+m-t1.m; t.m=b%60; t.h=(b/60)+h-t1.h; t.h=t.h%12; return t; } void time::getTime() { cout<<"\n Enter the hour(0-11) : "; cin>>h; cout<<"\n Enter the minute(0-59) : "; cin>>m; cout<<"\n Enter the second(0-59) : "; cin>>s; } void main() { clrscr(); time t1,t2,t3; cout<<"Enter the first time "; t1.getTime(); cout<<"\nEnter the second time "; t2.getTime(); t3=t1-t2; cout<<"\n First time : "; t1.show(); cout<<"\n Second time : "; t2.show(); cout<<"\n Subtract two time : "; t3.show(); getch(); } /*************************************** Output : Enter the first time Enter the hour(0-11) : 6 Enter the minute(0-59) : 40 Enter the second(0-59) : 30 Enter the second time Enter the hour(0-11) : 3 Enter the minute(0-59) : 20 Enter the second(0-59) : 10 First time : 6:40:30 Second time : 3:20:10 Subtract two time : 3:20:20 ****************************************/ ************************************************************************************************************************************ [5]By overloading arithmetic + operator concatenate two strings. ANS:- #include<iostream.h> #include<conio.h> #include<string.h> class string { char *str; public: string() { str = new char[30] ; *str = NULL; } string(char *s) { str = s; } string operator +(string ts) { string t; strcat(t.str, str); strcat(t.str, ts.str); return t; } void putstring() { cout << endl << str; } }; int main() { clrscr(); string s1("Institute "); string s2(" Computer\n"); s1.putstring(); s2.putstring(); cout<<"--------- concatenate -----------"; string s3; s3 = s1 + s2; s3.putstring(); getch(); return 0; } /********************************* output : Institute Computer --------- concatenate ----------- Institute Computer *****************************/ ******************************************************************************************************************************************* Inheritance =============================== [7]Create a class person having members as name and age. Derive two classes as student having member percentage and teacher having salary from class person. Write necessary member functions to initialize, read and display the data in proper format.// ANS:- #include<iostream.h> #include<conio.h> class person { char name[25]; int age; public: void getdata(); void display(); }; void person::getdata() { cout <<"Etrer the name:"<<endl; cin >> name; cout <<"Enter the age"<<endl; cin >> age; } void person::display() { cout<<"Your name is "<<name<<endl; cout<<"your age is "<<age<<endl; } class student : public person { float per; public: void get_stu(); void disp_stu(); }; void student::get_stu() { cout<<"Enter the persentage "<<endl; cin>>per; } void student::disp_stu() { cout<<"Persentage="<<per<<endl; } class teacher :public person { int salary; public: void get_teacher(); void display(); }; void teacher::get_teacher() { cout<<"Enter the salary "; cin>>salary; } void teacher::display() { cout<<"Salary "<<salary; } int main() { clrscr(); person P; P.getdata(); student S; S.get_stu(); S.disp_stu(); teacher T; T.get_teacher(); P.display(); S.disp_stu(); T.display(); getch (); return 0; } /*==========================output============== Etrer the name: abc Enter the age 21 Enter the persentage 90 Persentage=90 Enter the salary 10000 Your name is abc your age is 21 Persentage=90 Salary 10000 */ **************************************************************************************************************************************** [8]Assume a class cricketer. Derive class batsman from it. Batsman have total runs, total matches and best performance. Display batsman’s detail with calculated average runs (total runs / total matches) in proper format. ANS:- #include<iostream.h> #include<conio.h> class cricketer { char name[25]; int age; public: void getdata() { cout<<"Enter the name of Batsman ="; cin>>name; cout<<"Enter the Age of Batsman="; cin>>age; } }; class batsman:public cricketer { int run; int match; float avg; public: void get_bat() { cout<<"\n Enter the total run="; cin>>run; cout<<"\n Enter the number of total matches=" ; cin>>match; } void disp_bat() { cout<<"\n Total runs="<<run; cout<<"\n Total number of matches="<<match; } void display() { avg= run/ match; cout<<"\n Average runs:"; cout<<"\n\t Average : "<<avg; } }; int main() { clrscr(); batsman obj; obj.getdata(); obj.get_bat(); obj.display(); getch(); return 0; } /*===================output==================== Enter the name of Batsman =dhoni Enter the Age of Batsman=45 Enter the total run=78954 Enter the number of total matches=21 Average runs: Average : 638 */ *************************************************************************************************************************************** [9]Create a base classes teaching and nonteaching staff having name, designation and year. Derive a class collegestaff and calculate total salary for year given by user and display detail about college acade0.mic year and total salary paid. ANS:- #include<iostream.h> #include<conio.h> class coll { int year; public: void get_year(int a) { year=a; } void dis_year(void) { cout <<"Year is" <<year<<"\n"; } }; class teaching : public coll { public : int salary1; char des[]; public: void get_teac(int x) { salary1=x; } void dis_teac() { cout <<"Enter the teacnical salary:"<<salary1<<"\n"; // cout <<"Enter the Designation:"; // cin >> des; } }; class nonteaching { // int salary2; public: int salary2; void get_nont(int s) { salary2 =s; } void dis_nont() { cout<<"Enter the nontecnical salary:"<<salary2<<"\n"; } }; class totalsalary: public teaching ,public nonteaching { int total; public: void dispaly(void); }; void totalsalary :: dispaly(void) { total=salary1+salary2; // dis_year(); // dis_teac(); // dis_nont(); cout<<"Totalsalary is:"<<total<<"\n"; } int main() { clrscr(); totalsalary coll; coll.get_year(1994); coll.dis_year(); coll.get_teac(20005); coll.dis_teac(); coll.get_nont(10005); coll.dis_nont(); coll.dispaly(); getch (); // return 0; /* nonteaching N; C.getdata(); C.display(); T.getdata(); T.display(); N.getdata(); N.display(); getch(); return 0; */ } /* Year is1994 Enter the teacnical salary:20005 Enter the nontecnical salary:10005 Totalsalary is:30010 */ ************************************************************************************************************************************************************************************************* [10]Create class employee having name, salary and designation. Derive class commission from it. Derive netsal class where 10 % PF of salary deducted from salary. Display employee detail with calculated net salary as per commission. ANS:- #include<iostream.h> #include<conio.h> class emp { float basic,da,it,netsal; char name[10],num[10]; public: void getdata(); void net_sal(); void dispdata(); }; void emp::getdata() { cout << "\n Enter employee number:"; cin >> name; cout<< " Enter employee name:"; cin>>num; cout<<"Enter employee bacic salary in Rs:"; cin >>basic; } void emp::net_sal() { da=((0.55)*basic); float gsal=da+basic; it=((0.3) * gsal); netsal=gsal-it; } void emp::dispdata() { cout <<"\n Employee number: "<<name << "\n employee name: " <<num << "\n Employee netsalary: "<<netsal<<"Rs."; } void main() { clrscr(); emp ob[10]; int n; cout<<"\n\n********************************" <<"\n Calculation of Employee net Salary" <<"\n********************************" <<"\n Enter the number of Employee"; cin >>n; for(int i=0;i<n;i++) { ob[i].getdata(); ob[i].net_sal(); } // clrscr(); cout<<"\n--------------------" <<"\nEmployee detail::" <<"\n-------------------"; for(i=0;i<n;i++) { cout<<"\n \n Employee:"<<i+1<<"\n-----------"; ob[i].dispdata(); } getch(); } /* ******************************** Calculation of Employee net Salary ******************************** Enter the number of Employee001 Enter employee number:1 Enter employee name:jaydip Enter employee bacic salary in Rs:80000 -------------------- Employee detail:: ------------------- Employee:1 ----------- Employee number: 1 employee name: jaydip Employee netsalary: 86800Rs. */ *********************************************************************************************************************************************************************************************** Templates =============================== [13]Create function template to swap the two numbers of different datatype. ANS: #include<iostream.h> #include<conio.h> template <class T> void swap(T &x,T &y) { T temp = x; x = y; y = temp; } int main () { int m=1,n=2; float a=4.5,b=1.2; clrscr(); cout<<"Before Swap;"; cout<<"\nm="<<m<<"tn="<<n; cout<<"\na="<<a<<"tb="<<b; swap(m,n); swap(a,b); cout<<"\nAfter swap:"; cout<<"\nm="<<m<<"tn="<<n; cout<<"\na="<<a<<"tb="<<b; getch(); return 0; } /***************OUTPUT*************** Before Swap; m=1tn=2 a=4.5tb=1.2 After swap: m=2tn=1 a=1.2tb=4.5 */ ***************************************************************** [14]Implement bubble sort as a generic function. ANS:- #include<iostream.h> #include<conio.h> template<class T> void bubble(T a[],int n) { for(int i=0; i<n-1; i++) for(int j=n-1; i<j; j--) if(a[j]< a[j-1]) { swap(a[j],a[j-1]); } } template<class X> void swap(X &a,X &b) { X temp = a; a = b; b = temp; } int main() { int x[5] = {5,2,8,4,6}; float y[5] = {1.1,5.5,3.3,4.4,2.2}; clrscr(); bubble(x,5); bubble(y,5); cout<<"Sorted x-array:"; for(int i=0;i<5;i++) cout<< x[i] <<" "; cout<<"\n"; cout<<"sorted y-array:"; for(int j=0;j<5;j++) cout<<y[j]<<" "; cout<<"\n"; getch(); return 0; } /*** Sorted x-array:2 4 5 6 8 sorted y-array:1.1 2.2 3.3 4.4 5.5 */ ******************************************************************************************************************************************************************** Pointers and Polymorphism ============================================ [15]Write a program to display all the elements of an array in descending order using pointer. ANS:- #include<iostream.h> #include <conio.h> int main(void) { int a[10], i=0, j=0, n, t; clrscr(); cout <<"\n Enter the no. of elements: "; cin>>n; for (i = 0; i <n; i++) { cout<<"\n Enter the element : " ; cin>>a[i]; } for (j=0 ; j<(n-1) ; j++) { for (i=0 ; i<(n-1) ; i++) { if (a[i+1] < a[i]) { t = a[i]; a[i] = a[i + 1]; a[i + 1] = t; } } } cout<< "\n Ascending order: "; for (i=0 ; i<n ; i++) { cout<< a[i]<<"\t"; } cout<< "\n Descending order: "; for(i=n; i>0; i--) { cout<<a[i-1]<<"\t"; } getch(); return 0; } /* indicate successful completion */ /******OUTPUT************* Enter the no. of elements: 5 Enter the element : 20 Enter the element : 30 Enter the element : 60 Enter the element : 50 Enter the element : 21 Ascending order: 20 21 30 50 60 Descending order: 60 50 30 21 20 ****/ ******************************************************************************************************************************************* File Handling ================================= [19] Write a program to write to file and read from text file. ANS:- #include<iostream.h> #include<conio.h> #include<fstream.h> int main() { char data[100]; ofstream outfile; outfile.open("afile.txt"); cout<< "Write to file"<<endl; cout<< "Emter your name:"; cin.getline(data,100); outfile << data<<endl; cout << "Enter your age"; cin >> data; cin.ignore(); outfile << data<< endl; outfile.close(); ifstream infile; infile.open("afile.txt"); cout << "Reading from the file " <<endl; infile >> data; cout <<data << endl; infile >> data; cout << data<< endl; infile.close(); getch (); return 0; } /* Write to file Emter your name:jaydip Enter your age21 Reading from the file jaydip 21 */ ************************************************************************************************************************************************ [20]Write a program to count total numbers of characters. ANS:- #include<fstream.h> #include<iostream.h> #include<string.h> #include<conio.h> //#include<iomanip.h> int main() { ifstream infile; infile.open("jaydip.txt"); if(infile.fail()) { cout <<"Error "<<endl; } int numb_char=0; char letter; while(!infile.eof()) { infile.get(letter); cout <<letter; numb_char++; } cout << "the number of charecter:"<< numb_char << endl; infile.close(); getch (); return 0; } ****************************************************************************************************************************************************** [21]Write a program to count total numbers of lines. ANS:- #include<iostream.h> #include<fstream.h> #include<string.h> int main() string s,t; int count=0; cout<<"enter a text file :"; cin>>s; ifstream inFile(s); while(!inFile.eof()) { getline(inFile,t); count++; } cout<<s<<"contains "<<count<<"lines"; getch (); return 0; } ***************************************************************************************************************************************************** [22]Write a program to write and read object of class student (Rollno, name, total marks) using file handling functions. ANS:- #include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<fstream.h> class student { public: int roll; char name[15],f_name[20]; void put(); void get(); void switch_case(); }; student s; void student::put() { clrscr(); fstream file; cout<<"Enter roll no = "; cin>>roll; cout<<"Enter name = "; gets(name); cout<<"Enter father name = "; gets(f_name); file.open("stu.dat",ios::out|ios::app); // file.seekp(0,ios::beg); file.write((char *)this,sizeof(student)); file.close(); getch(); s.switch_case(); } void student::get() { int temp; clrscr(); cout<<"Enter roll no = "; cin>>temp; fstream file; file.open("stu.dat",ios::in); file.seekg(0,ios::beg); while(file.read((char *)this,sizeof(student))); { if(roll==temp) { cout<<"roll no.: "<<roll<<endl; cout<<"stu name: "<<name<<endl; cout<<"father name: "<<f_name<<endl; } } file.close(); getch(); s.switch_case(); } void student::switch_case() { int i; cout<<"Enter your choice (1-input, 2-output, 3-exit): "; cin>>i; switch(i) { case 1: s.put(); break; case 2: s.get(); break; case 3: exit(0); default: cout<<"wrong choice "; } } void main() { clrscr(); s.switch_case(); } /* roll no.: 22 stu name: jaydip father name: rao Enter your choice (1-input, 2-output, 3-exit): */ *************************************************************************************************************************************** [24]Write a program to create two string objects, concatenate and display them. ANS:- #include<iostream.h> #include<conio.h> int main() { char s1[100],s2[200]; int i,j; cout<< "Enter first string:"; cin >>s1; cout << "Enter Secound String:"; cin >>s2; for(i=0;s1[i];++i); for(j=0;s2[j];++j,++i) { s1[i] =s2[j]; } s1[i] ; cout<<"After Concationg:"<<s1; getch (); return 0; } /* Enter first string:rao Enter Secound String:jaydip After Concationg:raojaydipa */ ************************************************************************************************************************************** [25]Write a program to perform division and multiplication of two numbers, also handle ‘division by zero’ situation using exception handling functions. ANS: #include<iostream.h> #include<conio.h> int main() { int a,b; cout << "Enter value of and b\n"; cin >> a; cin >> b; int x =a-b; try { if(x!=0) { cout <<"Result(a/x) =" << a/x << "\n"; } else { throw(x); } } catch(int i) { cout << "exception caught: x= "<<x << "\n"; } cout <<"END"; getch (); return 0; } ***************************************************************************************************************************************1] Wap a program for addition Ans:-#include<iostream.h> #include<conio.h> int main() { int a,b; clrscr(); cout<<"enter the value:"; cin>>a; cin>>b; cout<<"addition="<<a+b<<endl; getch(); return(0); } /*----------------------- enter the value:5 6 addition=11 */ ==================================================== 2] Wap a program to find the address Ans:- #include<iostream.h> #include<conio.h> class person { int num; char name[20]; char address[30]; public: void getdata(); void display(); }; void person :: getdata() { cout<<"enter Your Home Number="; cin>>num; cout<<"Enter Your Name="; cin>>name; cout<<"Enter Your Address="; cin>>address; } void person ::display() { cout<<"Your Home number is="<<num<<endl; cout<<"Your Name is="<<name<<endl; cout<<"Your Address is="<<address<<endl; } int main() { person p; clrscr(); p.getdata(); p.display(); getch(); return 0; } /*=================output============== enter Your Home Number=126 Enter Your Name=neha Enter Your Address=ahemdabad Your Home number is=126 Your Name is=neha Your Address is=ahemdabad */ ************************************************************ 3]Wap to find out curcle,triangel,square. Ans:- #include<iostream.h> #include<conio.h> int area(int); int area(int,int); float area(float); float area(float,float); int main() { int s,l,b; float r,bs,ht; clrscr(); cout<<"enter the side of a square="; cin>>s; cout<<"enter length ="; cin>>l; cout<<"enter breadth of rectangle="; cin>>b; cout<<"enter redius or circle="; cin>>r; cout<<"enter base ="; cin>>bs; cout<<" enter height of triangle="; cin>>ht; cout<<"area of square is="<<area(s); cout<<"\n area of circle is="<<area(l,b); cout<<"\n area of circle is= "<<area(r); cout<<"\n area of triangle is="<<area(bs,ht); getch(); return 0; } int area (int s) { return(s*s); } int area(int l ,int b) { return (l*b); } float area(float r) { return(22/7 *r*r); } float area(float bs,float ht) { return(bs*ht/2); } /*------------------ output------------- enter the side of a square=45 enter length =12 enter breadth of rectangle=26 enter redius or circle=15 enter base =36 enter height of triangle=10 area of square is=2025 area of circle is=312 area of circle is= 675 area of triangle is=180 */ ************************************************************* 4]Wap to find number Ans:- #include<iostream.h> #include<conio.h> class num { int no1; int no2; public: void getdata(); void display(); }; void num ::getdata() { cout<<"enter the data"; cin>>no1>>no2; } void num ::display() { cout<<"no1="<<no1; cout<<"no2="<<no2; } int main() { num n; n.getdata(); n.display(); getch(); return 0; } /*-------------------- enter the data4 5 no1=4no2=5 */ ******************************************************** 5]Wap to copy constancr Ans:- #include<iostream.h> #include<conio.h> class num { int n1; int n2; int n3; public: void detdat(); // void display(); num(num &tnum) { n1=tnum.n1; n2=tnum.n2; n3=tnum.n3; } num (int x ,int y,int z) { n1=x;n2=y;n3=z; } void num :: display() { cout<<"n1 : "<<n1<<" "<<"\nn2 : "<<n2<<" "<<"\nn3 : "<<n3<<endl; } }; int main() { num N(10,20,30); clrscr(); num n1(N); num n2(N); num n3(N); N.display(); getch(); return(0); } /*==output=============== n1 : 10 n2 : 20 n3 : 30 */ ***************************************************************** 6]Wap to find prime number Ans:- #include<iostream.h> #include<conio.h> int main() { int a,i,sum=0; clrscr(); cout<<"enter the value of a="; cin>>a; for(i=2;i<a;i++) { if(a%i==0) { sum++; break; } } cout<<" "; if(sum==0 && i!=1) { cout<<"the number is prime number."; } else { cout<<"the number is not prime number."; } getch(); return(0); } /*===========output======== enter the value of a=10 the number is not prime number. enter the value of a=5 the number is prime number. */ ************************************************************ 7]// friend function to change rs and curr.\\ #include<iostream.h> #include<conio.h> class num; class curr { int rs; int ps; public: void getdata(); void display(); friend void add(curr, num); //it is not a class member function// }; class num { int no1; int no2; public: void getdata(); void display(); friend void add(curr, num); //it is not a class member function// }; void add (curr fc, num fn) { cout<<"add="<<fc.rs+fn.no1<<endl; cout<<"add="<<fc.ps+fn.no2<<endl; } void curr:: getdata() { cout<<"\n Enter value for rupess="; cin>>rs; cout<<"\n Enter value for currancy="; cin>>ps; } void curr:: display() { cout<<"\nValues are : "; cout<<rs<<" "<<ps<<endl; } void num:: getdata() { cout<<"\n Enter value for No1="; cin>>no1; cout<<"\n Enter value for No2="; cin>>no2; } void num:: display() { cout<<"\nValues are : "; cout<<no1<<" "<<no2<<endl; } void main() { num N; curr C; clrscr(); N.getdata(); N.display(); C.getdata(); C.display(); add(C,N); getch(); } /*=output= Enter value for No1=25 Enter value for No2=23 Values are : 25 23 Enter value for rupess=12 Enter value for currancy=15 Values are : 12 15 add=37 add=38 */ *************************************************** 8]Wap a function over Ans:- #include<iostream.h> #include<conio.h> int add(int,int); void add(float,float); int main() { int no1,no2; float f1,f2; clrscr(); cout<<"addition no1="; cin>>no1 ; cout<<"addition no2="; cin>>no2; cout<<"addition f1="; cin>>f1; cout<<"addition f2="; cin>>f2; cout<<"addition of integers:"<<add(no1,no2)<<endl; add(f1,f2); getch(); return 0; } int add(int a,int b) { return(a+b); } void add(float f1, float f2) { // cin>>f1; // cin>>f2; cout<<"\n addition of float:"<<f1+f2; // cout<<f1+f2; getch(); } /*=output============ addition no1=12 addition no2=26 addition f1=12.23 addition f2=15.23 addition of integers:38 addition of float:27.459999 ********************************************************************** 9]Wap to find square in inline function Ans:- #include<iostream.h> #include<conio.h> inline void square(int x) { cout<<"square="<<x*x<<endl; } void main() { int a; clrscr(); cout<<"enter value for a="; cin>>a; square(a); getch(); } /*=output========== enter value for a=8 square=64 */ ************************************************************************** 10] #include<iostream.h> #include<conio.h> class num { int no1; int no2; public: void getdata(int x, int y); void display(); }; inline void num :: getdata(int x,int y) { no1=x; no2=y; } inline void num :: display() { cout<<no1<< no2; } void main() { num N; clrscr(); N.getdata(10,20); N.display(); getch(); } output /* 1020 */ **************************************************************** 11] //wap inline and square and marge and frinde in c++.// #include<iostream.h> #include<conio.h> //inline void square(int x) //{ // cout<<"square="<<x*x<<endl; //} class num { int no1; int no2; public: void getdata(); int rtnn1() { return(no1); } int rtnn2() { return(no2); } void display(); friend void square(num); //it is not a class member function// }; void square (num fn) { cout<<"square="<<fn.no1*fn.no1<<endl; cout<<"square="<<fn.no2*fn.no2<<endl; } void num :: getdata() { cout<<"\n Enter value of no1 in data members="; cin>>no1; cout<<"\n Enter value of no2 in data members="; cin>>no2; } void num :: display() { cout<<"\nValues are : "; cout<<no1<<" "<<no2<<endl; } void main() { // int a,b; num N; clrscr(); N.getdata(); N.display(); square(N); // a=N.rtnn1(); // b=N.rtnn2(); // cout<<"\n\n------SQUARE----------\n"; //square(a); //square(b); getch(); } /*=output====== using inline and square to marge program Enter value of no1 in data members=12 Enter value of no2 in data members=19 Values are : 12 19 ------SQUARE---------- square=144 square=361 /******************************************* useing friend function Enter value of no1 in data members=5 Enter value of no2 in data members=9 Values are : 5 9 square=25 square=81 */ ******************************************************************** 12]Wap to find maximum number Ans:- #include<iostream.h> #include<conio.h> int main() { int a[5],i,max=0; clrscr(); for(i=0;i<5;i++) { cout<<"enter the value :"; cin>>a[i]; } max=a[0]; for(i=1;i<5;i++) { if(a[i]>max) max=a[i]; } cout<<"maximum="<<max; getch(); return(0); } /*--------------------------- enter the value :8 enter the value :3 enter the value :6 enter the value :9 enter the value :5 maximum=9 */ ************************************************************************** 13]Wap to find out maximum number in three numbers Ans:- #include<iostream.h> #include<conio.h> int main() { int a,b,c; clrscr(); cout<<"enter the value of a and b and c:"; cin>>a; cin>>b; cin>>c; if(a>b && b>c) cout<<"largest no="<<a<<endl; else if(b>c) cout<<"largest no="<<b<<endl; else cout<<"largest no="<<c<<endl; getch(); return(0); } /*------------------------ enter the value of a and b and c:45 56 89 largest no=89 */ *********************************************************************** 14]Wap to find out maximum number in two numbers Ans:- #include<iostream.h> #include<conio.h> int main() { int a,b; clrscr(); cout<<"enter the value of a and b:"; cin>>a; cin>>b; if(a>b) { cout<<"largest no="<<a<<endl; } else { cout<<"largest no="<<b<<endl; } getch(); return(0); } /*----------------------- enter the value of a and b:10 15 largest no=15 */ ************************************************************** 15]Wap to find out name and age Ans:- #include<iostream.h> #include<conio.h> class person { char name[20]; int age; public: void getdata(); void display(); }; void person :: getdata() { cout<<"Enter the name="; cin>>name; cout<<"Enter the age="; cin>>age; } void person :: display() { cout<<"Your name is="<<name<<endl; cout<<"Your age is="<<age; } int main() { person p; clrscr(); p.getdata(); p.display(); getch(); return 0; } /*--------------------- Enter the name=neha Enter the age=21 Your name is=neha Your age is=21 */ *************************************************************************** 16]Wap remainder Ans:- #include<iostream.h> #include<conio.h> int main() { int divisor,dividend,quotient,remainder; clrscr(); cout<<"enter dividend="; cin>>dividend; cout<<"Enter divisor="; cin>>divisor; quotient=dividend/divisor; remainder=dividend%divisor; cout<<"Quotient= "<<quotient<<endl; cout<<"Remainder="<<remainder; getch(); return(0); } /*==output============ enter dividend=17 Enter divisor=4 Quotient= 4 Remainder=1 */ ******************************************************************** 17]Wap Ans:- #include<iostream.h> #include<conio.h> int m=100; void main() { int m=50; { int m=10; cout<<"m="<<m<<endl; cout<<"m="<<::m<<endl; } cout<<"m="<<m<<endl; cout<<"m="<<::m<<endl; getch(); } /*===========output=== m=10 m=100 m=50 m=100 */ ************************************************************************* 18]Wap to find ave and sum. Ans:- #include<iostream.h> #include<conio.h> int main() { float num1,num2,sum,average; clrscr(); cout<<"enter two numbers="; cin>>num1; cin>>num2; sum=num1+num2; average=sum/2; cout<<"sum="<<sum<<endl; cout<<"average="<<average; getch(); return 0; } /*--------------------- enter two numbers=78 23 sum=101 average=50.5 */ *************************************************************************** 19]Wap to sawp. Ans:- #include<iostream.h> #include<conio.h> int main() { int a,b,temp; clrscr(); cout<<"enter the value:"; cin>>a; cin>>b; temp=a; a=b; b=temp; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; getch(); return(0); } /*-------------------- enter the value:4 5 a=5 b=4 */ ************************************************************************19] 20]Wap Ans:- #include<iostream.h> #include<conio.h> int main() { int a,b; void swap(int,int); cout<<"enter value:"; cin>>a>>b; swap(a,b); getch(); return 0; } void swap(int x,int y) { int temp; temp=x; x=y; y=temp; cout<<"supposed value are:"<<x<<"and"<<y; } /* output=== enter value:12 23 supposed value are:23and12 */ *********************************************************************** 21]Wap to find total. Ans:- #include<iostream.h> #include<conio.h> class student { int roll_no; int Sci; int Math; int Eng; int total() { return(Sci+Math+Eng); } public: void getdata(); void dispdata(); }; void student::getdata() { cout<<"enter the roll_no"; cin>>roll_no; cout<<"enter the sci number:"; cin>>Sci; cout<<" enter the math number:"; cin>>Math; cout<<"enter the eng number:"; cin>>Eng; } void student ::dispdata() { cout<<"roll_no:"<<roll_no<<endl; cout<<"sci="<<Sci<<endl; cout<<"math="<<Math<<endl; cout<<"eng="<<Eng<<endl; cout<<"total="<<total(); } int main() { student s; clrscr(); s.getdata(); s.dispdata(); getch(); return 0; } /*--------------- enter the roll_no1 enter the sci number:45 enter the math number:26 enter the eng number:32 roll_no:1 sci=45 math=26 eng=32 total=103 */ ********************************************************************** 21]Wap to find byte of datatype Ans:- #include<iostream.h> #include<conio.h> int main() { clrscr(); cout<<"size of char="<<sizeof(char)<<"byte"<<endl; cout<<"size of int ="<<sizeof(int)<<"bytes"<<endl; cout<<"size of float="<<sizeof(float)<<"bytes"<<endl; cout<<"size of double="<<sizeof(double)<<"bytes"<<endl; getch(); return(0); } /*=============output==== size of char=1byte size of int =2bytes size of float=4bytes size of double=8bytes */ ********************************************************************* 22]//wap of static function.// #include<iostream.h> #include<conio.h> class cnt { static int c; public: cnt() { } static void display() { cout<<"\n \n object number="<<++c<<endl; } }; int cnt::c; int main() { int i; clrscr(); cnt C[5]; for(i=0;i<5;i++) { C[i].display(); } getch(); return(0); } /*=========output====== object number=1 object number=2 object number=3 object number=4 object number=5 */ ************************************************************************** 23]//wap of constrector for perameter and copy// #include<iostream.h> #include<conio.h> class student { int rollno; int marks; public: student() { rollno=1; marks=0; } //this is perameter constructor.............// student(int r,int m) { rollno=r; marks=m; } //this is copy constructor..................// student(student &ts) { rollno=ts.rollno; marks=ts.marks; } void display(); }; void student :: display() { cout<<"rollno is ="<<rollno<<endl; cout<<"marks is ="<<marks; } int main() { student s(10,43); student s1(s); clrscr(); s.display(); cout<<"\n \n copied object....."<<endl; s1.display(); getch(); return(0); } /*===output=========== rollno is =10 marks is =43 rollno is =10 marks is =43 copied object..... rollno is =10 marks is =43 */
0 comments:
Post a Comment