mercredi 30 mars 2016

Calling a class member function from another class member function in C++

Here is a code in which I called a class member function using another class member function. The basic purpose is to accept 3 numbers from the user and arrange them in the ascending order. While I haven't come up with a way to code the ascending order part, I decided to do comparison of each successive number in the sequence first and then do the swapping if a number occurs greater than the other. I thought it would be cool to call a class member function from another class member function, but unfortunately, that part ain't working out. What should I do?

/*Whats wrong with the swap function here?*/    
// i think there is some logical fallacy in this code. Please point out?
#include<iostream>
using namespace std;

class Trial
{
private:
int roll[10];
int i,m,n,temp;

public:

void intake()
{
std::cout<<"\n Enter any number sequence "<<"\n";
for(i=1;i<=3;i++)
{
 std::cin>>roll[i];
}
}

void ascend()
{
for(i=1;i<=3;i++)
 {
  if(roll[i]>roll[i+1])
   {
    swap(roll[i],roll[i+1]);// Thought it would be cool to call another
    // member function from a member function
   }
 }              // Take two counters i and j
}

void swap(int m, int n)  // Write simple swap function here.
{
//int m,n,temp; // Think this member function ain't working
temp=m;
m=n;
n=temp;
}

};

int main()
{
Trial ob1;
ob1.intake();
ob1.ascend();
return 0;
}

Aucun commentaire:

Enregistrer un commentaire