Posts

Nested Member In cpp

Nested Member In cpp PROGRAM: #include<iostream> using namespace std; class student{ char name[10]; int roll_no; void print(){ cout<<"\n\n Entered Name : "<<name<<"\n\n Entered roll no : "<<roll_no; } public: void setdata(){ cout<<"\nEnter name : "; cin>>name; cout<<"\nEnter roll no : "; cin>>roll_no; print(); //!nesting of member function } }; int main(){ student s; s.setdata(); return 0; }

How to overload function in cpp

Overload Function In cpp PROGRAM: #include<iostream> using namespace std; void sub(float i,float j){ cout<<"\nsub : "<<(i-j); } void sub(int i,int j){ cout<<"\nsub : "<<(i-j); } void add(int i,int j){ cout<<"\nadd :"<<(i+j); } void add(float i,float j){ cout<<"\nadd :"<<(i+j); } int main(){ int a,b,ad; float x,y; cout<<"integer value are : "; cin>>a>>b; cout<<"float value are : "; cin>>x>>y; add(a,b); add(x,y);//!arguments have different data types sub(a,b); sub(x,y);//!!arguments have different data types return 0; }

Impemnt Producer Consumer Problem In cpp or c

Impemnt Producer Consumer Problem In cpp or c PROGRAM: #include<stdio.h> #include<stdlib.h> #include<pthread.h> # define BufferSize 3 int mutex=1,full=0,empty=3,x=0; void *producer(); void *consumer(); int BufferIndex=0; char *BUFFER; pthread_cond_t Buffer_Not_Full=PTHREAD_COND_INITIALIZER; pthread_cond_t Buffer_Not_Empty=PTHREAD_COND_INITIALIZER; pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER; int wait(int); int Signal(int); int main() { int n; pthread_t ptid,ctid; BUFFER=(char *) malloc(sizeof(char) * BufferSize); printf("\n1.Producer\n2.Consumer\n3.Exit"); while(1) { printf("\nEnter your choice:"); scanf("%d",&n); switch(n) { case 1: if((mutex==1)&&(empty!=0)){ //producer(); pthread_create(&ptid,NULL,producer,NULL); pthread_join(ptid,NULL);} else printf("Buffer is full!!"); break; case 2: if((mutex==1)&...

how to return obj in cpp

How To Return Object  PROGRAM: #include<iostream> using namespace std; class Complex{ int x,y; public: void setdata(int a,int b){ x=a; y=b; } void showdata(){ cout<<"\nx = "<<x<<" y = "<<y; } friend Complex addition(Complex,Complex); }; Complex addition(Complex a,Complex b){ Complex temp; temp.x=a.x+b.x; temp.y=a.y+b.y; return temp; } int main(){ Complex c1,c2,c3; c1.setdata(3,2); c2.setdata(2,3); cout<<"\n c1:"; c1.showdata(); cout<<"\n c2:"; c2.showdata(); c3=addition(c1,c2); cout<<"\n c3:"; c3.showdata(); return 0; }

Static function and static variable in cpp

Static variable and Static method in cpp PROGRAM: #include<iostream&gt using namespace std; class test{ int code; static int Count; public: void set_code(void){code=++Count;} void show_code(void){ cout<<"Object number : "<<code<<endl; } static void show_Count(void){ cout<<"Count : "<<Count<<endl; } }; int test:: Count; int main(){ test t1,t2; //!when 1st obj. created static Count=0 t1.set_code();//!t1=1,count=1 test::show_Count();//!because Static function t2.set_code(); //!t2=2,count=2 test::show_Count();//!count=2 test t3;//!count=2 t3.set_code();//!t3=3,count=3 test::show_Count();//!count=3 t1.show_code(); t2.show_code(); t3.show_code(); return 0; }

Thread Programming in CPP

Simple Thread Programming in CPP PROGRAM: #include&ltiostream&gt #include&ltpthread.h&gt using namespace std; void *Thread() { cout&lt&lt"HELLO"; } int main(){ pthread_t t_id; pthread_create(&t_id,0,&Thread,0); pthread_join(t_id,NULL); return 0; }

Write a program for bank which has class bank,deposit,withdraw in cp

#include<iostream> using namespace std; static long int id; class bank{ protected: char name[10]; double money=2000; int acc_no; public: void setdetails(){ cout<<"\n Enter name :"; cin>>name; cout<<"\nyour acc_no(PLEASE NOTE DOWN) : "<<id; acc_no=id; id++; cout<<"\n you have to deposite '2000rs' for opening account in pruthvi bank\n THANK YOU"; cout<<"\nPlease keep balance above 2000rs"; } }; class deposite:virtual public bank{ public: void set_deposit(){ float temp; cout<<"\n Enter deposite value : "; cin>>temp; money=money+temp; cout<<"\n Now money : "<<money<<endl; } }; class withdrawl:virtual public bank{ public: void set_with(){ float temp; cout<<"\n Enter withdrawl value : "; ...