求C++程序设计.

来源:百度知道 编辑:UC知道 时间:2024/05/21 22:33:12
定义2个类Cat和Dog,都继承自Animal,并重载Identify函数,不但要显示动物的种类,还要显示动物的名字.

建立类triangle,用来存储直角三角形的宽与高.用重载输出运算符函数在屏幕上显示三角型.

#include <string>

class Animal{
public:
explicit Animal(std::string s) : name(s) {}
virtual ~Animal(){};
virtual std::string Identify() {return std::string("Animal:") + name;}
private:
std::string name;
};
class Cat : public Animal {
std::string Identify() {return std::string("Cat:") + name;}
};
class Dog : public Animal {
std::string Identify() {return std::string("Dog:") + name;}
};

#include <iostream>

class triangle {
public:
triangle(double width, double height): width_(width), height_(height) {};
double width() const {return width_;}
double height() const {return height_;}
private:
double width_, height_;
};
std::ostream& operator<< (std::ostream& out, const triangle& tri) {
out << "TRIANGLE: width is " << tri.width() << ", height is " &