[Inheritance]The Triangle Class

本文档描述了如何设计一个名为Triangle的类,该类继承自GeometricObject类。Triangle类包含三个表示三角形边长的数据字段side1、side2和side3,默认值为1.0。提供了默认构造函数和带参数的构造函数来初始化边长和颜色。此外,还包括获取边长、面积和周长的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Triangle Class

Description

Design a class named Triangle that extends GeometricObject class.
The class contains:
Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with color = “blue”, filled = true.
A constructor that creates a triangle with the specified side1, side2, side3 and color = “blue”, filled = true.
The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3().
A function named getArea() that returns the area of this triangle.
A function named getPerimeter() that returns the perimeter of the triangle.

class GeometricObject
{
public:
  GeometricObject(string color, bool filled)
  {
    this->color = color;
    this->filled = filled;
  }
  
  string getColor()
  { return color; }
  void setColor(string color)
  { this->color = color; }
  bool isFilled()
  { return filled; }
  void setFilled(bool filled)
  { this->filled = filled;}
  string toString()
  {
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
  }
private:
  string color;
  bool filled;
};

Hint

You may use "#include ".

Please submit the GeometricObject class and Triangle class.

Don’t submit the main() function.

framework.cpp
#include <iostream>
#include <string>
#include "source.h"
using namespace std;
int main()
{
  Triangle shape1;
  cout << shape1.getSide1() << endl;
  cout << shape1.getSide2() << endl;
  cout << shape1.getSide3() << endl;  
  cout << shape1.getArea() << endl;
  cout << shape1.getPerimeter() << endl;
  cout << shape1.getColor() << endl;
  cout << shape1.isFilled() << endl;

  Triangle shape(1, 1.5, 1);
  cout << shape.getColor() << endl;
  cout << shape.isFilled() << endl;

  shape.setColor("yellow");
  shape.setFilled(true);

  cout << shape.getSide1() << endl;
  cout << shape.getSide2() << endl;
  cout << shape.getSide3() << endl;  
  cout << shape.getArea() << endl;
  cout << shape.getPerimeter() << endl;
  cout << shape.getColor() << endl;
  cout << shape.isFilled() << endl;

  return 0;
}

source.h
#include <iostream>
#include <cmath>

using namespace std;

class GeometricObject
{
public:
    GeometricObject(string color, bool filled)
    {
        this->color = color;
        this->filled = filled;
    }
    string getColor()
    {
        return color;
    }
    void setColor(string color)
    {
        this->color = color;
    }
    bool isFilled()
    {
        return filled;
    }
    void setFilled(bool filled)
    {
        this->filled = filled;
    }
    string toString()
    {
        return "Geometric object color " + color +
               " filled " + ((filled) ? "true" : "false");
    }

private:
    string color;
    bool filled;
};
class Triangle:public GeometricObject
{
public:
    Triangle();
    Triangle(double side1,double side2,double side3);
    double getSide1();
    double getSide2();
    double getSide3();
    double getArea();
    double getPerimeter();
private:
    double _side1,_side2,_side3;

};
Triangle::Triangle()
    :GeometricObject("blue",true),
    _side1(1.0),_side2(1.0),_side3(1.0){}
Triangle::Triangle(double side1,double side2,double side3)
    :GeometricObject("blue",true),
    _side1(side1),_side2(side2),_side3(side3){}
double Triangle::getSide1()
{
    return _side1;
}
double Triangle::getSide2()
{
    return _side2;
}
double Triangle::getSide3()
{
    return _side3;
}
double Triangle::getArea()
{
    double p = (_side1 + _side2 + _side3) / 2;
    return sqrt(p * (p - _side1) * (p - _side2) * (p - _side3));
}
double Triangle::getPerimeter()
{
    return _side1 + _side2 + _side3;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值