Skip to main content

Posts

Showing posts from September, 2012

Returning a pointer

Program to illustrate a function returning a pointer ////////////////////////////////Tested By C++ and Created//////////////////////////////////////// #include<iostream.h> #include<conio.h> int *big(int &,int &); void main() {  int a,b,*c; clrscr(); cout<<"\t\tPROGRAM TO ILLUSTRATE A FUNCTION RETURNING A POINTER\n\n"; cout<<"\tENTER TWO INTEGER\t";cin>>a>>b; c=big(a,b); cout<<"\n\tTHE BIGGER VALUE IS\t"<<*c<<"\n"; getch(); } int*big(int & x, int & y) {   if(x>y) return (&x);     else    return (&y); }

Structure Pointers

Program to illustrate the use of Structure pointers ////////////////////Program Tested By C++ and created also///////////////////////////// #include <iostream.h> #include <conio.h> void main() { clrscr(); struct date {short int dd,mm,yy;}join_dt={19,12,2006}; date*dt_ptr; dt_ptr=&join_dt; cout<<"\t\t\tPROGRAM TO USE STRUCTURE POINTERS\n\n"; cout<<"\n\tPRINTING STRUCTURE ELEMENTS USING STRUCTURE VARIABLE\n\n"; cout<<"\tdd="<<join_dt.dd<<",mm="<<join_dt.mm<<",yy="<<join_dt.yy<<"\n"; cout<<"\n\tPRINTING STRUCTURE ELEMENTS USING STRUCTURE POINTERS\n\n"; cout<<"\tdd="<<dt_ptr->dd<<",mm="<<dt_ptr->mm<<",yy="<<dt_ptr->yy<<"\n"; getch(); }

Cgets

Cgets :- Its is a Delcaration type   Ex:- {char*cgets(char*str;)} Cgets reads a string of characters from the console and stores the string ( and the string length ) in the location  *str[0]. Before you call cgets,ste str[0] to the maximum length of the string to be read.

Free Store

In C++ every program is provided with a pool of unallocated memory that is may utlise during execution. This memory is called Free store memory. Struct Node { char info [15];    // Declare variable forming Info part                        Node*next       // pointer that will point to next node                     }; Node *ptr               // pointer that will point to the newly allocated memory  ptr=new node     // allocate memory to hold a node and make ptr point to it 

Transpose ofa Matrix

#include<iostream.h> #include<conio.h> #include<stdlib.h> void main() { system ("cls"); int a[3][3],b[3][3],i,j; cout<<"\n\tEnter the element of a matrix"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>a[i][j]; cout<<"\n\tGiven matrix is:"; for(i=0;i<3;i++) { cout<<"\n"; for(j=0;j<3;j++) cout<<a[i][j]<<" "; } cout<<"\n\tTranspose of a given matrix is:"; for(i=0;i<3;i++) { cout<<"\n"; for(j=0;j<3;j++) { b[i][j]=a[i][j]; cout<<b[i][j]<<" "; } } getch(); }

Transpose of matrix

#include<iostream.h> #include<conio.h> #include<stdlib.h> void main() { system ("cls"); int a[3][3],b[3][3],i,j; cout<<"\n\tEnter the element of a matrix"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>a[i][j]; cout<<"\n\tGiven matrix is:"; for(i=0;i<3;i++) { cout<<"\n"; for(j=0;j<3;j++) cout<<a[i][j]<<" "; } cout<<"\n\tTranspose of a given matrix is:"; for(i=0;i<3;i++) { cout<<"\n"; for(j=0;j<3;j++) { b[i][j]=a[i][j]; cout<<b[i][j]<<" "; } } getch(); }

SieveOfEratosthenes

To Get the prime number in the range 2.N , you can use a simple and intersting method known as Sieve Of Eratosthenes. For Exampl this Program :- #include <iostream.h> #include <conio.h> #define Max 100 void main() { clrscr(); int isprime[Max+1]; int n,i; cout<<"\tEnter N (<=100):\t"; cin>>n; for(i=2;i<=n;i++) isprime[i]=1; for(i=2;i*i<=n;i++) { if (isprime[i]==1) {for(int j=i;i*j<=n;j++) isprime[i*j]=0; } } int gap =0; int bestgap=0; int right=0; for(i=2;i<=n;i++) { if (isprime[i])gap=0; else gap++; if (gap>bestgap) { bestgap=gap; right=i; } } int left=right-bestgap+1; cout<<"\t\nThere are no prime between"<<left<<"\tand"<<right<<endl; cout<<"\t\nThat is"<<bestgap<<"\tconsecutive integers"<<endl; getch(); }

Bubble Sort

#include <iostream.h> #include <conio.h> void bubblesort(int [],int); void main() {clrscr(); int AR[50],item,n,index; cout<<"\n\t HOW MANY ELEMENT DO U WANT TO CREATE ARRAY WITH"; cin>>n; cout<<"\n\t ENTER ARRAY ELEMEMT"; for(int i=0;i<n;i++) cin>>AR[i]; bubblesort(AR,n); cout<<"\n\n THE SORTED ARRAY IS AS SHOWN BELOW"; for(i=0;i<n;i++) cout<<AR[i]<<"_"; cout<<endl; getch(); } void bubblesort(int AR[],int size) { int tmp,ctr=0; for(int i=0;i<size;i++) { for(int j=0;j<(size-1)-i;j++) {if (AR[j]<AR[j+1]) { tmp=AR[j]; AR[j] =AR[j+1]; AR[j+1]=tmp; } } cout<<"ARRAY AFTER INERATION"<<++ctr<<"is"; for(int k=0;k<size;k++) cout<<AR[k]<<" "; cout<<endl; }} SCREEN SHORT : OUTPUT BUBBLE SORT PRORAME PREVIEW 

Insertion in Array

#include<iostream.h> #include<process.h> #include<conio.h> int findpos(int[],int,int); void main() { int AR[50],item,n,index; cout<<"How many elements do u want to create array with?"; cin>>n; cout<<"enter array elements(must be sorted in Asc order)"; for(int i=0;i<n;i++) cin>>AR[i]; char ch='y'; while(ch=='y'||ch=='y') { cout<<"enter element to be inserted...."; cin>>item; if(n==50) { cout<<"overflow!!"; exit(1);

Nested class inherit

Here is An example class A { private:   class B { };   B *z;   class C : private B {   private:       B y; //      A::B y2;       C *x; //      A::C *x2;     }; };

logic of reversed list

NodePtr revList = NULL NodePtr currNode = currHead; while(currNode != NULL) {         // Set the head to the next node         currHead=currHead->next;             // Link currNode to the reversed list         currNode->next=revList;         revList=currNode;             // Move currNode to next node         currNode=currHead;         currHead=revList; }

Complex numbers library

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them: Classes complex                             Complex number class (class template) Functions Complex values: real       Return real part of complex (function template) imag     Return imaginary part of complex (function template) abs       Return absolute value of complex (function template) arg       Return phase angle of complex (function template ) norm    Return norm of complex number (function template) conj     Return complex conjugate (function template) polar    Return complex from polar components (function template ) Transcendentals overloads: cos                  Return cosine of complex (function template) cosh                Return hyperbolic cosine of complex (function template) exp                  Return exponential of complex (function template) log                   Return natural lo

Online Job For C++ Programmers

Yes You read it right online money earning techique for all the C++ programmers . C++ is a general purpose programming language that is compiled. It has both high level and low level features and was developed in Bell Labs as an enhancement to the C programming language . If you need help developing with or working with the C++ programming language, you can take the help of C++ freelancers on this site. Start connecting with them by posting your job today! http://www.freelancer.in/jobs/CPlusPlus-Programming/

Meeting The Creator Of C++

BJARNE STROUSTRUP Bjarne Stroustrup  (born December 30, 1950 in Ã…rhus, Denmark) is a Danish computer scientist, most notable for the creation and the development of the widely used C++ programming language. He is currently Professor and holder of the College of Engineering Chair in Computer Science at Texas A&M University. Stroustrup began developing C++ in 1979 (then called "C with Classes"), and, in his own words, "invented C++, wrote its early definitions, and produced its first implementation... chose and formulated the design criteria for C++, designed all its major facilities, and was responsible for the processing of extension proposals in the C++ standards committee." Stroustrup also wrote what many consider to be the standard textbook for the language, The C++ Programming Language, which is now in its third edition. The text has been revised twice to reflect the evolution of the language and the work of the C++ standards committee.

Binary Search In Array

#include<iostream.h> #include<conio.h> int Bsearch(int[],int,int); void main() { clrscr(); int AR[50],item,n,index; cout<<"Enter desired array size (max 50)"; cin>>n; cout<<"enter array element (must be sorted in Asc order)"; for (int i=0;i<n;i++) cin>>AR[i]; cout<<"enter the element to be searched for"; cin>>item; index=Bsearch(AR,n,item); if(index== -1) cout<<"sorry!!given element could not be found"; else cout<<"element found at index:"<<index<<",Position"<<index+1<<endl; } int Bsearch(int AR[],int size,int item) { int beg,last,mid; beg=0;   last=size-1; while (beg<=last) {mid=(beg+last)/2; if(item==AR[mid])return mid; else if (item>AR[mid]) beg=mid+1; else last=mid-1; } return-1; }

Arrays as parameters

At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation. In order to accept arrays as parameters the only thing that we have to do when declaring the function is to specify in its parameters the element type of the array, an identifier and a pair of void brackets []. For example, the following function: void procedure (int arg[]) accepts a parameter of type "array of int" called arg. In order to pass to this function an array declared as: int myarray [40]; it would be enough to write a call like this: procedure (myarray);

How To Take Screen Short of Your Output of Program

Ever Wonder how to take a snap of your output of C++ or C programming. Today we will tell how to do it. some simple step will take a good screen snap of your program or its out put on pc laptop. Here What you have to do 1. Make Program u want to take pic of . 2. Click Alt + Enter And you will see this 3. Now Click PrtScsysrq   button which can be found on the top line of your key board. after the F12 key or     above the number pad if u use keyboard in pc. Well its Done !!!!!! Just past it on the paint and edit.

Shallow And Deep copying

A shallow copy means that C++ copies each member of the class individually using the assignment operator. When classes are simple (eg. do not contain any dynamically allocated memory), this works very well. For example, let’s take a look at our Cents class: class Cents { private:     int m_nCents; public:     Cents(int nCents=0)     {         m_nCents = nCents;     } }; When C++ does a shallow copy of this class, it will copy m_nCents using the standard integer assignment operator. Since this is exactly what we’d be doing anyway if we wrote our own copy constructor or overloaded assignment operator, there’s really no reason to write our own version of these functions! However, when designing classes that handle dynamically allocated memory, memberwise (shallow) copying can get us in a lot of trouble! This is because the standard pointer assignment operator just copies the address of the pointer — it does not allocate any memory or copy the contents being pointed to!

What is "genericity"?

FAQ : A way to say "class templates". Don't confuse with "generality". FQA : Hmm, why do we need three ways of saying this? Apparently C++ promoters consider templates an excellent and unique feature, despite their obscurity and the availability of much better meta-programming facilities in many languages. The many different synonyms are probably needed to illuminate the killer feature from many different directions.

What's the idea behind templates?

FAQ: A template describes how to build definitions (classes or functions) which are basically the same. One application is type-safe containers; there are many, many more. FQA: Let's get a bit more specific. The FAQ's answer is applicable to C macros, Lisp macros, ML functors, functions like eval found in many interpreted languages, OS code that generates assembly instructions used to handle interrupts at run time, and just plain code generation (writing programs that print source code). The purpose of all such devices is meta-programming - writing code that works with code, creating pieces of code which are "basically the same" (after all, they are built from the same rules), and yet have some interesting differences. The question is, how do we specify these rules and these differences? The approach used in C++ templates is to use integral constants and types to represent the differences, and to use class & function definitions to represent the rules. T

Should my class declare a member function or a friend function?

FAQ: Use a member function unless you must use a friend function. For example, you may need friend for operator overloading. FQA: The FAQ says it at last! See? It was about operator overloading all the way. The FAQ's advice may be further simplified if we use the observation that C++ operator overloading is just a pain in the neck.

Do friends violate encapsulation?

FAQ : On the contrary - they enhance it. If used properly, of course. For example, you can use them to split code into two classes and have their data inaccessible for code in all other classes. Or you can use them as "a syntactic variant" of member functions. Think of the friends as part of the class interface instead of something outside the class. FQA: What encapsulation? C++ doesn't have encapsulation. It has access control (code outside of a class using private members of this class will not compile), but it has neither compile time encapsulation (stable binary interfaces) nor run time encapsulation (safe memory access). What's there to violate? The case of two classes was discussed in the previous FAQ; this argument only sounds convincing if you think C++ classes are a good way to define the important, stable interfaces in your system. As to the "syntactic variant" business, it's probably about overloaded operators. The C++ syntax make

Is Encapsulation a Security device?

FAQ: No. Encapsulation is about error prevention. Security is about preventing purposeful attacks. FQA : Depends on the kind of "encapsulation". Some managed environments rely on their support for run time encapsulation, which makes it technically impossible for code to access private parts of objects, to implement security mechanisms. C++ encapsulation evaporates at run time, and is almost non-existent even at compile time - use #define private public before including a header file and there's no more encapsulation (correction). It's hardly "encapsulation" at all, so of course it has no security applications - security is harder than encapsulation. The capital E and S in the question are very amusing. I wonder whether they are a manifestation of Deep Respect for Business Values or Software Engineering; both options are equally hilarious.

How does C++ help with the tradeoff of safety vs. usability?

FAQ : In C, stuff is either stored in structs (safety problem - no encapsulation), or it is declared static at the file implementing an interface (usability problem - there is no way to have many instances of that data). With C++ classes, you can have many instances of the data (many objects) and encapsulation (non-public members). FQA: This is wildly wrong, and the chances that the FAQ author didn't know it are extremely low. That's because you can't use FILE* from <stdio.h> or HWND from <windows.h> or in fact any widely used and/or decent C library without noticing that the FAQ's claim is wrong. When you need multiple instances and encapsulation in C, you use a forward declaration of a struct in the header file, and define it in the implementation file. That's actually better encapsulation than C++ classes - there's still no run-time encapsulation (memory can be accidentally/maliciously overwritten), but at least there's compile-time

Program To Input The Flight Details and Print It

#include<iostream.h> #include<conio.h> class flight { public: int flight; char destination; int distance; public : void flight::feeddata(void) { cout<<"enter the flight no.:"; cin>>flight; cout<<"enter the destination:"; cin>>destination; cout<<"enter the distance:"; cin>>distance; } void flight::showdata(void) { cout<<"the flight no. is:"<<flight; cout<<"the flight will go to:"<<destination; cout<<"the distance is:"<<distance; } }; void main() { clrscr(); flight plane; plane.feeddata(); plane.showdata(); getch(); }

Program to Print First n Natural Number and Their Sum

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,sum,n; cout<<"\n\tHow natural number?\t"; cin>>n; for(i=1;i<=n;++i); { cout<<"\n\t"<<i<<"\t\n"; sum=sum+i; } cout<<"\n\t"<<"The Sum of First"<<"\t"<<n<<"\tnatural number is :"<<sum<<"\n"; getch(); }; Download Now :- Sum Of Natural No.

Program to Print Fibonacci Series i.e (0 1 1 2 3 5 8 ....)

#include <iostream.h> #include <conio.h> Void main() { clrscr(); unsigned long first ,second,third,n; first=0; second=1; cout<<"How many element (>5?)\n"; cin>>n; cout<<"Fibonacci series\n"; cout<<first<< "   "<<second ; for (int i=2; i<n;++i) { third=first+second; cout<<" "<<third ; first=second; second=third; getch(); }};

What is Recursion

Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our function will never finish. This might be true is some cases, but in practice we can check to see if a certain condition is true and in that case exit (return from) our function. The case in which we end our recursion is called a base case . Additionally, just as in a loop, we must change some value and incremently advance closer to our base case. For Example :- void myFunction( int counter) { if(counter == 0)      return; else        {        cout <<counter<<endl;        myFunction(--counter);        return;        } } Every recursion should have the following characteristics. A simple base case which we have a solution for and a return value. Sometimes there are more than one base