C++头/实现文件中的默认和重载构造函数?

Default & Overloaded Constructors in C++ header/implementation files?

本文关键字:默认 重载 构造函数 实现 文件 C++      更新时间:2023-10-16

我正在做C++作业。我挂断了获取默认构造函数和重载构造函数以在头/实现文件中工作的挂断。我一直收到"('令牌'错误之前的预期主表达式,到目前为止,stackoverflow 和 google fu 没有帮助。

主要:

#include <iostream>
#include "Triangle.h"
#include "triangle.cpp"
using namespace std;
int main()
{
Triangle first;
// show that default is properly built
cout << "Default is 3, 4 5 triangle, should be scalene and right" << endl;
cout << " for default a is " << first.getA() << " b is " << first.getB() << " c is " << first.getC() << endl;
cout << " default " << (first.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
cout << " default " << (first.isScalene() ? "is" : "is not") << " scalene" << endl;
cout << " default " << (first.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
cout << " default " << (first.isRight() ? "is" : "is not") << " right" << endl << endl;
// test setter methods
first.setA(2);
first.setB(2);
first.setC(3);
cout << "Modified is 2, 2, 3 triangle, should be isosceles" << endl;
cout << " for modified a is " << first.getA() << " b is " << first.getB() << " c is " << first.getC() << endl;
cout << " modified " << (first.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
cout << " modified " << (first.isScalene() ? "is" : "is not") << " scalene" << endl;
cout << " modified " << (first.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
cout << " modified " << (first.isRight() ? "is" : "is not") << " right" << endl << endl;
// test overloaded constructor
Triangle second(4,4,4);
cout << "Second is 4,4,4 triangle, should be equalateral and isosceles" << endl;
cout << " for second a is " << second.getA() << " b is " << second.getB() << " c is " << second.getC() << endl;
cout << " second " << (second.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
cout << " second " << (second.isScalene() ? "is" : "is not") << " scalene" << endl;
cout << " second " << (second.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
cout << " second " << (second.isRight() ? "is" : "is not") << " right" << endl << endl;
return 0;
}

页眉:

#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle
{
public:
Triangle();
Triangle(int a, int b, int c);
// accessor methods for a, b, c.
int getA();
int getB();
int getC();
// mutator methods for a, b, c.
void setA(int x);
void setB(int y);
void setC(int z);
// equilateral triangles have sides of the same length.
bool isEquilateral();
// scalene triangles have unequal sides.
bool isScalene();
// isoceles triangles have 2 equal sides.
bool isIsosceles();
// right triangles use pythagorean theorem a^2 + b^2 = C^2. the other derivation will also word depending
// on how sides are declared.
bool isRight();
// variables set as private so that only the members of Triangle can modify
private:
int a, b, c;
};
#endif // TRIANGLE_H

三角形.cpp:

#include "Triangle.h"
#include <iostream>
using namespace std;
Triangle::Triangle()
{
// constructor setting sides a, b, c.
Triangle();{
a = 3;
b = 4;
c = 5;
}
}
Triangle::Triangle(int a, int b, int c)
{
Triangle(int a, int b, int c){
setA(a);
setB(b);
setC(c);
}
}
// accessor methods for a, b, c.
int Triangle::getA() {
return a;
}
int Triangle::getB() {
return b;
}
int Triangle::getC() {
return c;
}
// mutator methods for a, b, c.
void Triangle::setA(int x) {
a = x;
}
void Triangle::setB(int y) {
b = y;
}
void Triangle::setC(int z) {
c = z;
}
// equilateral triangles have sides of the same length.
bool Triangle::isEquilateral() {
if (a == b && b == c && a == c) {
return true;
}
else {
return false;
}
}
// scalene triangles have unequal sides.
bool Triangle::isScalene() {
if (a != b && b != c && a != c) {
return true;
}
else {
return false;
}
}
// isoceles triangles have 2 equal sides.
bool Triangle::isIsosceles() {
if (a == b && a != c) {
return true;
}
else if (a == c && a != b) {
return true;
}
else if (b == c && b != a) {
return true;
}
else {
return false;
}
}
// right triangles use pythagorean theorem a^2 + b^2 = C^2. the other derivation will also word depending
// on how sides are declared.
bool Triangle::isRight() {
if (((a * a) + (b * b)) == (c * c)) {
return true;
}
else if (((a * a) + (c * c)) == (b * b)) {
return true;
}
else if (((b * b) + (c * c)) == (a * a)) {
return true;
}
else {
return false;
}
}

运行代码时,我继续收到"('令牌"错误之前的预期主表达式。我无法克服这一点 - 任何帮助都值得赞赏。

我不确定您从哪里获得用于定义构造函数的语法。

对于默认构造函数,可以使用:

Triangle::Triangle()
{
a = 3;
b = 4;
c = 5;
}

更好的是,委托它使用其他构造函数。

Triangle::Triangle() : Triangle(3, 4, 6) {}

另一个构造函数可以通过使用以下方法进行更正和简化:

Triangle::Triangle(int a, int b, int c) : a(a), b(b), c(c) {}