C++的类和标头
class and header for C++
本文关键字:C++ 更新时间:2023-10-16
我正在尝试学习如何在c ++中使用类函数。我有我在这里写的代码,但有些问题。我知道你不做功课。但是,如果您能帮助我学习它,而无需使用诸如您看这里之类的东西..我做过研究..谷歌CPLUPLUS网站..但是有些东西我没有得到,所以希望你能帮助我。我正在使用开发C++。
代码取两个数字,然后根据选择的使用来计算它们。加。。减去。。乘。。和设计
这是我的第一个错误代码
30 23 C:UsersddempseyDesktopclassaddSubtractMain.cpp [Error] no matching function for call to 'addSubtract::initialize()'
这是我的标题代码
#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
主要
#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 don ";
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);
到numbers.initialize(n1, n2);
除此之外,您的代码很好。
相关文章:
- 没有找到相关文章