10 Replies - 299 Views - Last Post: 04 December 2012 - 04:29 AM Rate Topic: ***-- 2 Votes

#1 Juice24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 13-November 12

Calling a Function in a class?

Posted 03 December 2012 - 05:35 PM

Would anybody be able to explain to me how I would call my functions in my class from my main, cause currently my only function that is displaying to the screen is my main. I know how to call a function normally but classes are new to me.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


class menuItemType 
{
public:
     void getData(menuItemType item[], ofstream& outData);
     void showMenu(menuItemType item[], ofstream& outData);
     void printCheck (double& total, double& tax, double& totalF, ofstream& outData);

private:
    string itemName;
    double itemCost;


};


int main()
{

ofstream outData;
outData.open("OutMenu.txt");

cout<< "                Welcome !:)/>           " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;
				

outData << "             Welcome! :)/>        " << endl;
outData << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
outData << "  *To select an Item from the list below,  " << endl;
outData<< "just indicate the Item number for the item \n\n" << endl;


    menuItemType menu [8];
   /* getData(menu, outData); 
    showMenu(menu, outData);*/
	

    system("pause");

    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a Function in a class?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Calling a Function in a class?

Posted 03 December 2012 - 06:06 PM

Quote

Would anybody be able to explain to me how I would call my functions in my class
Ok, before that, here are two very important questions:

1. Why do you have a class?

2. Why do you have functions within the class?

This post has been edited by Oler1s: 03 December 2012 - 06:07 PM

Was This Post Helpful? 3
  • +
  • -

#3 Juice24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 13-November 12

Re: Calling a Function in a class?

Posted 03 December 2012 - 06:35 PM

I had this program using a struct but now I have to write it using a class and it must contain atleast one Constructor and one Destructor.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


class menuItemType 
{
public:
    string itemName;
    double itemCost;

private:
	void getData(menuItemType item[], ofstream& outData);
	void showMenu(menuItemType item[], ofstream& outData);
	void printCheck (double& total, double& tax, double& totalF, ofstream& outData);

};


int main()
{

ofstream outData;
outData.open("OutMenu.txt");

cout<< "                Welcome !:)/>/>/>           " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;
				

outData << "             Welcome! :)/>/>/>        " << endl;
outData << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
outData << "  *To select an Item from the list below,  " << endl;
outData<< "just indicate the Item number for the item \n\n" << endl;


    menuItemType menu [8];
   /* getData(menu, outData); 
    showMenu(menu, outData);*/
	

    system("pause");

    return 0;
}

void menuItemType :: getData(menuItemType item[], ofstream& outData)
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);
	outData << fixed << setprecision(2);


    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }

}

void menuItemType :: showMenu(menuItemType item[],ofstream& outData) 
{
	int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    //the Menu
    for (int i=0; i<8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		outData << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		j++;
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y')
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) {
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break; }
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
				outData << endl;
            }
            cout << endl;
			outData << endl;

            cout << item[choice].itemName << " " << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cout << "Do you want to continue (Y/N): ";
			outData << item[choice].itemName << " " << item[choice].itemCost << endl;
            outData << "======================================================" << endl;
            outData << "Do you want to continue (Y/N): ";
            cin >> exit;

        }
	printCheck(total, tax, totalF, outData);
}
void menuItemType ::  printCheck(double& total, double& tax, double& totalF, ofstream& outData)
 {
     cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;

	 outData << "\n\nThanks for Dinning With Us!" << endl;
	 outData << "<><><><><><><><><><><><><><>\n" << endl;
	 outData << "Customer Receipt            " << endl;
	 outData << "SubTotal:               $" << total <<endl;
	 outData << "Tax:                    $" << tax << endl;
	 outData << "                        -----" << endl;
	 outData << "Total:                  $" << totalF << endl;
	 outData << "Please Come Again!" << endl;
 }




The code I just posted was before I switched public and private within the class. The Class I posted in my first question is the class I have now
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Calling a Function in a class?

Posted 03 December 2012 - 06:49 PM

What does this line of code do for you?

    menuItemType menu [8];



Understanding what classes represent will help you understand the solution to you question.
Was This Post Helpful? 0
  • +
  • -

#5 Juice24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 13-November 12

Re: Calling a Function in a class?

Posted 03 December 2012 - 06:54 PM

when my program was previously using a struct that was used to indicate that there is 8 items in my "menu" in my txt file
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Calling a Function in a class?

Posted 03 December 2012 - 08:45 PM

Let's take void getData(menuItemType item[], ofstream& outData);. This function is within the class. That makes it a member function.

Do you agree?

You should. Ok, so it's a member function. Here is the question you need to figure out.

Why is it within the class? Why not outside? Did you flip a coin and decide to stick it inside the class because the coin came up heads?

Why??
Was This Post Helpful? 0
  • +
  • -

#7 Juice24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 13-November 12

Re: Calling a Function in a class?

Posted 03 December 2012 - 09:16 PM

I redid it and am at this point, anyone see what im doing wrong in my constructor / destructor?

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


class menuItemType 
{
public:
	menuItemType();
	~menuItemType();
	void accessData();
private:
	void getData(menuItemType item[], ofstream& outData);
	void showMenu(menuItemType item[], ofstream& outData);
	void printCheck (double& total, double& tax, double& totalF, ofstream& outData);
    string itemName;
    double itemCost;
};

menuItemType :: menuItemType()
{
	getData(menuItemType item[], ofstream& outData);
}
menuItemType :: ~menuItemType()
{
	printCheck (double& total, double& tax, double& totalF, ofstream& outData);
}
int main()
{

ofstream outData;
outData.open("OutMenu.txt");

cout<< "                  Welcome                  " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;
				

outData << "                   Welcome                 " << endl;
outData << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
outData << "  *To select an Item from the list below,  " << endl;
outData<< "just indicate the Item number for the item \n\n" << endl;

menuItemType a;
a.accessData();



    system("pause");

    return 0;

}

void menuItemType :: getData(menuItemType item[], ofstream& outData)
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);
	outData << fixed << setprecision(2);


    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }

}

void menuItemType :: showMenu(menuItemType item[],ofstream& outData) 
{
	int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    //the Menu
    for (int i=0; i< 8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		outData << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		j++;
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y')
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) 
		{
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break;}
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
				outData << endl;
            }
            cout << endl;
			outData << endl;

            cout << item[choice].itemName << " " << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cout << "Do you want to continue (Y/N): ";
			outData << item[choice].itemName << " " << item[choice].itemCost << endl;
            outData << "======================================================" << endl;
            outData << "Do you want to continue (Y/N): ";
            cin >> exit;

        }
	printCheck(total, tax, totalF, outData);
}
void menuItemType :: accessData()
{
	showMenu();

}
void menuItemType ::  printCheck(double& total, double& tax, double& totalF, ofstream& outData)
 {
     cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;

	 outData << "\n\nThanks for Dinning With Us!" << endl;
	 outData << "<><><><><><><><><><><><><><>\n" << endl;
	 outData << "Customer Receipt            " << endl;
	 outData << "SubTotal:               $" << total <<endl;
	 outData << "Tax:                    $" << tax << endl;
	 outData << "                        -----" << endl;
	 outData << "Total:                  $" << totalF << endl;
	 outData << "Please Come Again!" << endl;
 }




Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

Re: Calling a Function in a class?

Posted 03 December 2012 - 09:38 PM

When you instantiate an object of the class, the default construct is called to construct the class...
So, this line would call the default construct:
048	menuItemType a;


And the constructor,
023	menuItemType :: menuItemType()
024	{
025	    getData(menuItemType item[], ofstream& outData);
026	}


the construct calls getData(), which by its name implies that it should get some data. But the problem is you have no data yet! And you are passing two arguments which contains nothing, and are used in the getData() function... Are you getting the point?

Also, where is the accessData() functions implemented?
049	a.accessData();

Was This Post Helpful? 0
  • +
  • -

#9 Juice24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 13-November 12

Re: Calling a Function in a class?

Posted 03 December 2012 - 09:40 PM

Im getting errors with my constructor and destructor, does anyone know what im doing wrong?

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


class menuItemType 
{
public:
	menuItemType();
	~menuItemType();
	void accessData();
private:
	void getData(menuItemType item[], ofstream& outData);
	void showMenu(menuItemType item[], ofstream& outData);
	void printCheck (double& total, double& tax, double& totalF, ofstream& outData);
    string itemName;
    double itemCost;
};

menuItemType :: menuItemType()
{
	getData(menuItemType item[], ofstream& outData);
}
menuItemType :: ~menuItemType()
{
	printCheck (double& total, double& tax, double& totalF, ofstream& outData);
}
int main()
{

ofstream outData;
outData.open("OutMenu.txt");
				

cout<< "                  Welcome!                 " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;
				

outData << "                  Welcome!                 " << endl;
outData << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
outData << "  *To select an Item from the list below,  " << endl;
outData<< "just indicate the Item number for the item \n\n" << endl;

menuItemType a;
a.accessData();



    system("pause");

    return 0;

}

void menuItemType :: getData(menuItemType item[], ofstream& outData)
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);
	outData << fixed << setprecision(2);


    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }

}

void menuItemType :: showMenu(menuItemType item[],ofstream& outData) 
{
	int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    //the Menu
    for (int i=0; i< 8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		outData << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		j++;
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y')
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) 
		{
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break;}
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
				outData << endl;
            }
            cout << endl;
			outData << endl;

            cout << item[choice].itemName << " " << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cout << "Do you want to continue (Y/N): ";
			outData << item[choice].itemName << " " << item[choice].itemCost << endl;
            outData << "======================================================" << endl;
            outData << "Do you want to continue (Y/N): ";
            cin >> exit;

        }
	printCheck(total, tax, totalF, outData);
}
void menuItemType :: accessData()
{
	showMenu();

}
void menuItemType ::  printCheck(double& total, double& tax, double& totalF, ofstream& outData)
 {
     cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;

	 outData << "\n\nThanks for Dinning With Us!" << endl;
	 outData << "<><><><><><><><><><><><><><>\n" << endl;
	 outData << "Customer Receipt            " << endl;
	 outData << "SubTotal:               $" << total <<endl;
	 outData << "Tax:                    $" << tax << endl;
	 outData << "                        -----" << endl;
	 outData << "Total:                  $" << totalF << endl;
	 outData << "Please Come Again!" << endl;
 }


Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Calling a Function in a class?

Posted 04 December 2012 - 04:26 AM

Hi,

There is a difference in declaring and using a function..

say I have a function

int Foo(int a, int aa ) ;

That would be its declaration... using the function

int c = 6;
int d = 8;

Foo(c,d ) ;

not

Foo (int c, int d ) ;

its implementation

int Foo(int a, int aa ) ;
{

return a+aa;

}

looking at your constructor... you would have to do something like

menuItemType :: menuItemType()
{
	menuItemType *item;
	//or
	//menuItemType item[100];
	//if size of array is known before
	//runtime otherwise dynamic memory
	//is needed
	ofstream outData;

	getData(item, outData);
}



Now I have put comments in the code....
This setup uses the * operator this is good for a dynamic memory solution
but it requires a malloc to allocate memory.
if the size of the array is roughly known before runtime you

can simply do

menuItemType item[100];

and this will allocate memory.

Regards

Snoopy.

This post has been edited by snoopy11: 04 December 2012 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,216
  • Joined: 23-August 08

Re: Calling a Function in a class?

Posted 04 December 2012 - 04:29 AM

Merged duplicate topics. Do not create a new topic on the same subject for which you already have one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1