简体   繁体   中英

class and header for C++

I am trying to learn how to use class functions in c++.. I have code that I written here but something is wrong.. I know you don't do the homework..but if you can help me out to learn it with out using things like did you look here..I have done research ..google cpluplus site..but there is something i am not getting so hopefully you can help me out. I am using Dev-C++.

The code take two numbers and then calculates them based on choice of useing..add..subtract..multiply..and devide

this is my first error code

30  23  C:\Users\ddempsey\Desktop\class\addSubtractMain.cpp [Error] no matching function for call to 'addSubtract::initialize()'

here is my header code

#ifndef CLASS_addSubtract_h
#define CLASS_addSubtract_h

class addSubtract
{
    private:
       int one;
       int two;


    public:
        int add ();
        int sub ();
        int multi ();
        int devide ();
        void initialize (int n1, int n2);
};

class

using namespace std;

#include "addSubtract.h"



void addSubtract::initialize(int n1, int n2)
{
    one = n1;
    two = n2;
}
int addSubtract::add()
{

   return(one + two);
}
int addSubtract::sub()
{

   return(one - two);
}

int addSubtract::multi()
{

   return(one * two);
}

int addSubtract::devide()
{

   return(one / two);
}
//class function code

main

#include "addSubtract.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main(void)
{
    addSubtract numbers; //instantiate an object
    int n1;
    int n2;
    char choice;

    cout<<"Enter first number to calculate  ";
    cin>>n1;
    cout<<"\n";
    cout<<"Enter second number to calculate  ";
    cin>>n2;
    cout<<"\n";
    cout<<"what would you like to do\n ";
    cout<<" add enter      (+)\n ";
    cout<<" subtract enter (-)\n ";
    cout<<" multiply enter (*)\n ";
    cout<<" devide enter   (/)\n ";
    cin>>choice;

        switch (choice)
        {
            case '+' :
            numbers.initialize(int1, int2); 
            cout<<n1<<" + "<<n2<<" ="<<numbers.add()<<endl;
            break;
            case '-' :
            numbers.initialize(int1, int2); 
            cout<<n1<<" - "<<n2<<" = "<<numbers.sub()<<endl;
            break;
            case '*' :
            numbers.initialize(int1, int2); 
            cout<<n1<<" * "<<n2<<" = "<<numbers.multi()<<endl;
            break;
            case '/' :
            numbers.initialize(int1, int2); 
            cout<<n1<<" / "<<n2<<" = "<<numbers.devide()<<endl;
            break;  
            default:
            cout<<"invalid choice" <<endl;          
        }


system("PAUSE");    
return 0;

}

numbers.initialize(int1, int2); to numbers.initialize(n1, n2);

Except for this your code is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM