C++头/实现文件中的默认和重载构造函数?
Default & Overloaded Constructors in C++ header/implementation files?
我正在做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) {}
相关文章:
- 从类重载的新运算符运行非默认构造函数
- C++头/实现文件中的默认和重载构造函数?
- 模板中默认类型的重载运算符
- 重载运算符是具有较小默认对齐方式的新增运算符
- 类X有多个默认构造函数和对重载函数的模糊调用
- 重载方法的类接受模板和基类 - 如何默认某个方法
- 将重载函数分配给功能指针作为默认值
- C++ 重载 ostream 不适用于带有空参数的默认构造
- 重载基类中的方法,默认为成员变量
- 是否在重载选择期间未考虑默认参数的转换
- 在解析分配给默认参数值的重载函数时,要考虑哪组函数
- C++选择'wrong'默认参数的重载方法
- 继承并重载默认构造函数
- 如果我们在c++中重载构造函数,那么默认构造函数是否仍然存在
- C++使用默认参数重载
- C++:函数重载vs变分函数vs变分模板vs默认参数
- 构造函数重载和默认参数
- 在C++中使用默认参数时,重载函数的多个实例
- Qt C++新的类似方法.我是重载方法、编写新方法还是使用默认参数
- G++:模糊情况下的默认重载