| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Templates-C++
Class Template
Q) Write a C++ object program to sort the contents of an array storing data of any type (eg. Int, float and double) using class templates. The class should include following member functions. i) A member function to input the values for the array. ii) A member function to output the contents of the array. iii) A member function that sorts the array contents. Ans) #include<iostream.h> #include<stdio.h> #include<conio.h> template <class T>class bsort{ private: T *p; int size; public: bsort(){ cout<<"Enter how many numbers you want to sort"; cin>>size; p=new T[size]; } void input(){ for(int i=0;i<size;i++){ cout<<"Enter the next element\n"; cin>>p[i]; } } void display(){ for(int i=0;i<size;i++) cout<<"\n"<<p[i]; } void sort(){ int ex=1; T temp; while(ex!=0){ ex=0; for(int i=0;i<size-1;i++){ for(int j=i+1;j<size;j++) if(p[i]>p[j]) { temp=p[i]; p[i]=p[j]; p[j]=temp; ex++; } } } } }; void main() { bsort<int>b1; b1.input(); b1.sort(); b1.display(); bsort<float>b2; b2.input(); b2.sort(); b2.display(); bsort<double>b3; b3.input(); b3.sort(); b3.display(); getch(); } I'm getting 9 warnings saying (Functions containing for(loop) are not expanded inline)..Could you tell me Why this 9 warnings are shown…and please help me to rectify my problem… |
|
#2
|
|||
|
|||
|
Your compiler is set to expand functions in-line when it can, it is merely warning you that it cannot expand these particular functions. When I compile this on VC++6.0, I generate no warnings in debug mode, but that is set to disable in-line function expansion, so technically you have no problem.
It works, right? Although you do get undefined input behavior when the input expects a float and gets an int... I assume this was a homework problem, right? I wonder why they are not teaching the new standard for headers... <iostream> instead of <iostream.h> for example... |
|
#3
|
|||
|
|||
|
Quote:
Oh no, I see, it is waiting for the getch() before doing the last display. If you do this: #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; it displays the 3rd sort before waiting for the getch() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Templates-C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|