问题描述
- 初学者提问,关于c++中class的问题
-
#include using namespace std;enum Type{HOME, OFFICE, FAX, CELL, PAGER};class PhoneNum{public: PhoneNum() { country_code = 0; area_code = 0; num = 0; type = HOME; } PhoneNum(int, int, int, Type); PhoneNum(int, Type); int GetCountryCode() const; int GetAreaCode() const; int GetPhoneNum() const; Type GetType() const; void SetCountryCode(int); void SetAreaCode(int); void SetPhoneNum(int); void SetType(Type); bool Equal(PhoneNum);private: int country_code; int area_code; int num; Type type;};PhoneNum :: PhoneNum(int a, int b, int c, Type t){ a = country_code; b = area_code; c = num; type = t;}PhoneNum :: PhoneNum(int d, Type t1){ num = d; type = t1; country_code = 86; area_code = 20;}int PhoneNum :: GetCountryCode()const{ return country_code;}int PhoneNum :: GetAreaCode() const{ return area_code;}int PhoneNum :: GetPhoneNum()const{ return num;}Type PhoneNum ::GetType()const{ return type;}void PhoneNum ::SetCountryCode(int f){ country_code = f;}void PhoneNum ::SetAreaCode(int g){ area_code = g;}void PhoneNum ::SetPhoneNum(int h){ num = h;}void PhoneNum ::SetType(Type t3){ type = t3;}bool PhoneNum ::Equal(PhoneNum n) { return((country_code == n.country_code) && (area_code == n.area_code) && (num == n.num) && (type == n.type));}
找不出代码问题,可提交了总是答案错误,求大神
解决方案
4处错误:
ComplexNum :: ComplexNum(double a, double b)
{
real = a;//修改
imag = b;//修改
}
void ComplexNum :: divide(ComplexNum c4)
{
double c = real;
double d = imag;
real = (c * c4.real + d * c4.imag) / (c4.real * c4.real + c4.imag * c4.imag);//修改
imag = (d * c4.real - c * c4.imag) / (c4.real * c4.real + c4.imag * c4.imag);//修改
}
解决方案二:
首先你代码太难看,要学会缩进
解决方案三:
不然没人看你代码,就不会有人找错
解决方案四:
#include
#include
using namespace std;
class ComplexNum
{
public:
ComplexNum()
{
real = 0;
imag = 0;
}
ComplexNum(double, double);
double GetRealPart() const;
double GetImaginaryPart() const;
void add(ComplexNum);
void subtract(ComplexNum);
void multiply(ComplexNum);
void divide(ComplexNum);
void SetRealPart(double);
void SetImaginaryPart(double);
double GetModulus() const;
private:
double real;
double imag;
};
ComplexNum :: ComplexNum(double a, double b)
{
a = real;
b = imag;
}
double ComplexNum :: GetRealPart() const
{
return real;
}
double ComplexNum :: GetImaginaryPart() const
{
return imag;
}
void ComplexNum :: add(ComplexNum c1)
{
real = real + c1.real;
imag = imag + c1.imag;
}
void ComplexNum :: subtract(ComplexNum c2)
{
real = real - c2.real;
imag = imag - c2.imag;
}
void ComplexNum :: multiply(ComplexNum c3)
{
double a = real;
double b = imag;
real = a * c3.real - b * c3.imag;
imag = a * c3.imag + c3.real * b;
}
void ComplexNum :: divide(ComplexNum c4)
{
double c = real;
double d = imag;
real = (c * c4.real + d * c4.imag) / (c4.real * c4.real + cn4.imag * cn4.imag);
imag = (d * c4.real - c * c4.imag) / (c4.real * c4.real + cn4.imag * cn4.imag);
}
void ComplexNum :: SetRealPart(double r)
{
real = r;
}
void ComplexNum :: SetImaginaryPart(double i)
{
imag = i;
}
double ComplexNum :: GetModulus() const
{
return sqrt(real * real + imag * imag);
}
解决方案五:
#include
using namespace std;
enum Type{HOME, OFFICE, FAX, CELL, PAGER};
class PhoneNum
{
public:
PhoneNum()
{
country_code = 0;
area_code = 0;
num = 0;
type = HOME;
}
PhoneNum(int, int, int, Type);
PhoneNum(int, Type);
int GetCountryCode() const;
int GetAreaCode() const;
int GetPhoneNum() const;
Type GetType() const;
void SetCountryCode(int);
void SetAreaCode(int);
void SetPhoneNum(int);
void SetType(Type);
bool Equal(PhoneNum);
private:
int country_code;
int area_code;
int num;
Type type;
};
PhoneNum :: PhoneNum(int a, int b, int c, Type t)
{
a = country_code;
b = area_code;
c = num;
type = t;
}
PhoneNum :: PhoneNum(int d, Type t1)
{
num = d;
type = t1;
country_code = 86;
area_code = 20;
}
int PhoneNum :: GetCountryCode()const
{
return country_code;
}
int PhoneNum :: GetAreaCode() const
{
return area_code;
}
int PhoneNum :: GetPhoneNum()const
{
return num;
}
Type PhoneNum ::GetType()const
{
return type;
}
void PhoneNum ::SetCountryCode(int f)
{
country_code = f;
}
void PhoneNum ::SetAreaCode(int g)
{
area_code = g;
}
void PhoneNum ::SetPhoneNum(int h)
{
num = h;
}
void PhoneNum ::SetType(Type t3)
{
type = t3;
}
bool PhoneNum ::Equal(PhoneNum n)
{
return((country_code == n.country_code) && (area_code == n.area_code)
&& (num == n.num) && (type == n.type));
}
两个答案错误,不知道为什么,求解
解决方案六:
main函数没有呢?代码不全,正确的运行结果是什么?代码放到代码片里。
解决方案七:
main函数没有呢?代码不全,正确的运行结果是什么?代码放到代码片里。