C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 10th, 2004, 06:50 AM
hotrock hotrock is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 hotrock User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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…

Reply With Quote
  #2  
Old February 10th, 2004, 07:22 AM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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...

Reply With Quote
  #3  
Old February 10th, 2004, 07:35 AM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Although you do get undefined input behavior when the input expects a float and gets an int...


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()

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Templates-C++


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap