日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C++grammar】继承与构造test1代码附录

發(fā)布時(shí)間:2023/12/1 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++grammar】继承与构造test1代码附录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 1、main.cpp
  • 2、circle.cpp
  • 3、circle.h
  • 4、rectangle.cpp
  • 5、rectangle.h
  • 6、Shape.h

1、main.cpp

#include <iostream> #include <string> #include "Shape.h" #include "circle.h" #include "rectangle.h"//創(chuàng)建Shape/Circle/Rectangle對(duì)象 //用子類對(duì)象調(diào)用基類函數(shù)toString() using namespace std; int main() {Shape s1{Color::blue,false};Circle c1{3.9,Color::green,true};Rectangle r1{4.0,1.0,Color::white,true };cout << s1.toString() << endl;cout << c1.toString() << endl;cout << r1.toString() << endl;return 0; }

2、circle.cpp

#include <iostream> #include "circle.h"Circle::Circle(){radius = 1.0; }//使用基類的構(gòu)造函數(shù)初始化基類的數(shù)據(jù)成員 Circle::Circle(double radius_, Color color_, bool filled_) : Shape{color_,filled_} {radius = radius_; }double Circle::getArea() {return (3.14 * radius * radius); }double Circle::getRadius() const {return radius; }void Circle::setRadius(double radius) {this->radius = radius; }

3、circle.h

#pragma once #include "Shape.h" //補(bǔ)全Circle 類,從shape繼承 class Circle : public Shape{double radius; public:Circle();Circle(double radius_, Color color_, bool filled_);double getArea();double getRadius() const;void setRadius(double radius); };

4、rectangle.cpp

#include "rectangle.h" Rectangle::Rectangle(double w, double h, Color color_, bool filled_) :width{ w }, height{h}, Shape{ color_,filled_ } {}double Rectangle::getWidth() const { return width; } void Rectangle::setWidth(double w) { width = w; } double Rectangle::getHeight() const { return height; } void Rectangle::setHeight(double h) { height = h; }double Rectangle::getArea() const { return width * height; }

5、rectangle.h

#pragma once #include "Shape.h" //創(chuàng)建rectangle類,從shape繼承 class Rectangle : public Shape { private:double width{1.0};double height{1.0}; public:Rectangle() = default;Rectangle(double w, double h, Color color_, bool filled_);//get函數(shù)被聲明為const類型,表明這個(gè)函數(shù)本身不去修改類的私有屬性double getWidth() const;void setWidth(double w);double getHeight() const;void setHeight(double h);double getArea() const; };

6、Shape.h

#pragma once #include <iostream> #include <string> #include <array> using std::string; //C++14的字符串字面量 using namespace std::string_literals; enum class Color {white , black , read , green , blue , yellow, };class Shape { private://就地初始化Color color{Color::black};bool filled{false}; public://無參構(gòu)造函數(shù),強(qiáng)制由編譯器生成Shape() = default;Shape(Color color_, bool filled_){color = color_;filled = filled_;}Color getColor() { return color; }void setColor(Color color_) { color = color_; }bool isFilled() { return filled; }void setFilled(bool filled_) {filled = filled_;}string toString() {//自動(dòng)轉(zhuǎn)化為string類型的對(duì)象std::array<string, 6> c{ "white"s,"black"s,"red"s,"green"s,"blue"s,"yellow"s,};return "Shape: " + c[static_cast<int>(color)] + " " + (filled ? "filled"s : "not filled"s);} };

總結(jié)

以上是生活随笔為你收集整理的【C++grammar】继承与构造test1代码附录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。