Posts

Showing posts with the label Cpp

Static member function

 //static function  #include<iostream.h> class member{ private: static int total; int id; public: member() {total+=1; id=total; cout<<"creating id "<<id<<endl; } ~member() { // total-=1; id=total; total--; cout<<"destroying id "<<id<<endl; } void show() { cout<<"total is"<<total;} }; int member::total=0; int main(int argc, char *argv[]) { member a; member b; member c; member d; // a.show(); cout<<"**********"<<endl; }

Pointer to object

 //pointer to object #include<iostream.h> using namespace std; class dist{ private: int ft,in; public: dist():ft(0),in(0) {} dist(int f,int i):ft(f),in(i) {} void show() { cout<<ft<<" ' "<<in<<" '' "<<endl; } }; int main(int argc, char *argv[]) { dist d1, d2(7,8); d2.show(); dist* d; d=new dist(7,9); d->show(); (*d).show(); delete d; d->show(); dist&  d3=*(new dist); d3.show(); //d3 is reference variable //new dist create new return 0; }

Pointer to object array

 #include<iostream.h> using namespace std; class name{ private: char nm[20]; public: void get() {cout<<"Enter name:"; cin>>nm; } void show() {cout<<nm<<endl; } }; int main(int argc, char *argv[]) { name* p[100]; char c; int n=0; do{ p[n]->get(); cout<<"mire name:"; cin>>c; }while(c=='y'); do{ p[n]->show(); }while(n>=0); return 0; }

Character pointer array

  //array of char pointer #include<iostream.h> int main(int argc, char *argv[]) { char* p[]={"sun","mon","tue","wed"}; for(int i=0;i<4;i++) cout<<p[i]<<endl; }

Custom array length by pointer (new, delete)

 //custom array length //by new,delete #include<iostream.h> #include<cstring> using namespace std; int main(int argc, char *argv[]) { char st[]="hello"; int l=strlen(st); char* p; int ll; cout<<"len"; cin>>ll; p=new char[ll]; strcpy(p,st); cout<<p; delete []p; return 0; }

Copy character pointer type string to character array

//copy character pointer to character array #include<iostream.h> using namespace std; int main(int argc, char *argv[]) { void copy(char*, char*); char* st="Hello world"; char st1[100]; copy(st1,st); cout<<st1; return 0; } void copy(char* s1, char* s2) { while(*s2) *s1++=*s2++; *s1='\0'; }

Inductance caculator

 #include<iostream.h> #include<math.h> #include<conio.h> using namespace std; unsigned short int bit; double c,l,fre,rad,Tmax,Tmin=0.5e-6; unsigned short int res; double ind(double); int main(int argc, char *argv[]) { cout<<"INDUCTANCE METER CACULATOR\n"; cout<<"**************************\n"; cout<<"8MHZ Fosc is used \nTcyc= 0.5e-6 sec\n "; cout<<"Select timer mode.\n"; cout<<"for 8 bit mode enter 8\nfor 16 bit mode enter 16\nbit:"; cin>>bit; res=pow(2,bit)-1; Tmax=res*Tmin; cout<<"\nResolution="<<res; std::cout<<std::scientific; cout<<"\nTmin="<<Tmin<<" s"; cout<<"\nTmax="<<Tmax<<" s"; cout<<"\nEnter ref cap(farad):"; cin>>c; cout<<"\n\nYou Entered"<<endl; //cout<<"L="<<l<<endl; cout<<"C="<...

Conversion operator

  //operator()  //conversion operator #include<iostream.h> class dist{ private: int f; int i; float mtf; public: dist():f(0),i(0),mtf(3.28) {} dist(int ft,int in):f(ft),i(in),mtf(3.28) {} void get() { cout<<"Enter ft:"; cin>>f; cout<<"Enter in"; cin>>i; } void show() { cout<<f<<" ' "<<i<<" ``"<<endl; } operator float() const { float fraf=i/12; fraf+=static_cast<float>(f); return (fraf/mtf); } }; int main(int argc, char *argv[]) { dist d1(3,12); d1.show(); cout<<static_cast<float>(d1); }

Stack pop push

 //stack //push //pop #include<iostream.h> class stack { private: enum{max=100}; int st[max]; int cnt; int var; public: stack() { cnt=0; } void push(int x) { st[cnt++]=x; } int pop() { return st[--cnt]; } }; int main(int argc, char *argv[]) { stack s; s.push(6); s.push(67); cout<<s.pop()<<endl; cout<<s.pop()<<endl; }

static

 //static store last value #include<iostream.h> float avg(int ); int main(int argc, char *argv[]) { int n; float res; for(;;){ cout<<"Enter a num;"; cin>>n; res=avg(n); cout<<res<<endl; } return  0 } float avg(int x) { static int c=0; static float total=0; c++; total+=x; return(total/c); }

Formatted, aligned output

  //set left right //set precision #include<iostream.h> #include<iomanip> #include<cmath> int main(int argc, char *argv[]) { cout<<setw(3)<<"x"<<setw(10)<<"x^2"<<setw(15)<<"sqrt x"<<endl; for(int i=0; i<10;i++) { cout<<resetiosflags(ios::left)<<setw(3)<<setprecision(1)<<i<<setiosflags(ios::right)<<setw(10)<<setiosflags(ios::fixed)<<i*i<<setiosflags(ios::right)<<setw(15)<<setprecision(2)<<sqrt(i)<<endl; } return 0; }

Right left justify

  //align right left #include<iostream.h> #include<iomanip> int main(int argc, char *argv[]) { cout<<setiosflags(ios::left)<<setw(10)<<"HELLO"; cout<<setiosflags(ios::right)<<setw(20)<<"world"; return 0; }

Setwidth, setw,formatted output

//formatted output //setf //setw #include<iostream.h> #include<iomanip> double i[3]={1234.9766,0.854335,8652146.9}; char *k[3]={"still","alive","guys"}; int main(int argc, char *argv[]) { cout.setf(ios::fixed); cout<<endl; for(int c=0;c<3;c++) { cout<<setw(10)<<k[c]; cout<<setw(20)<<i[c]<<endl; } return 0; }

String empty,length

  //string empty //string length #include<iostream.h> #include<string> void test(const string & st) { if(st.empty())cout<<"Empty"<<endl; else cout<<"length="<<st.length()<<endl; } int main(int argc, char *argv[]) { string t; test(t); t="alaska fox"; test(t); return 0; }

Search word and text

  //text search #include<iostream.h> #include<string.h> #include<iomanip> int main(int argc, char *argv[]) { string s1("hello"); string s2(" world"); s1+=s2; int n; n=s1.find('l'); if(n!=-1) cout<<setw(25)<<"first text position="<<n<<endl; n=s1.rfind('l'); if(n!=-1) cout<<setw(25)<<"last text position="<<n<<endl; n=s1.find("llo"); if(n!=-1) cout<<setw(25)<<"first word position="<<n; return 0; }

Aligned text print

  //setw #include<iostream.h> #include<iomanip>//setw int main(int argc, char *argv[]) { //right justified aligned cout<<setw(10)<<"Hello"<<setw(5)<<"all"<<endl; cout<<setw(10)<<"hi"<<setw(5)<<"yo"<<endl; return 0; }

String

Class template/ partial template specialization

 //class template //partial template specialization //use the same class nane for various template #include<iostream.h> template<class t1, class t2> class area { t1 a; t2 b; public: area(t1 o1,t2 o2) { a=o1; b=o2; } void display() { cout<<a<<endl; cout<<b<<endl; } }; template < class t> class area<t,char> { t ob1; t ob2; public: area(t oo1,char c) { ob1=oo1; ob2=int(c); } void display() { cout<<ob1<<endl; cout<<ob2<<endl; } }; int main(int argc, char *argv[]) { area<int,int> a1(5,7); a1.display(); area<int,float>a2(6,7.8); a2.display(); area<int,char> a3(6,'t'); a3.display(); return 0; }

Reference reduces complex notation

  // reference variable //reduce complex notation #include<iostream.h> struct date { int day,month,year; }; struct player { int no; char*name; date dt; }; player pl[]={ {1,"Aung Aung",{2,12,1987}}, {2,"zaw zaw",{4,5,1990}}, {0} }; //notice 0 that end player int main(int argc, char *argv[]) { player *p=pl; while(p->no!=0) { player& pr=*p; date& d=pr.dt; cout<<pr.no<<"\t"<<pr.name<<"\t"<<d.day<<"/"<<d.month<<"/"<<d.year<<endl; p++; } return 0; }

Creating reference/alias variable

  //reference variable #include<iostream.h> int main(int argc, char *argv[]) { int a=123; int& b=a; b++; a+8; cout<<"a="<<a; cout<<"\nb="<<b; return 0; }