归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏? + 留言?📝
缺乏明确的目标,一生将庸庸碌碌!
一起加油!
#pragma once
#include <iostream>
using namespace std;
enum CRL
{
BLACK,WHITE,GREEN,YELLOW,BROWN,RED,ORANGE
};
class Shape
{
private:
CRL color;
int Pos_x, Pos_y;
public:
Shape(int px=0, int py=0, CRL cor=BLACK) {
Pos_x = px;
Pos_y = py;
color = cor;
cout << "Construct Shape " << endl;
}
int Get_x() {
return Pos_x;
}
int Get_y() {
return Pos_y;
}
double GetArea() {
return 0;
}
double GetPerimeter() {
return 0;
}
CRL GetColor() {
return color;
}
};
#pragma once
#include"Shape.h"
class Rectangle:public Shape
{
private:
int w;
int h;
public:
Rectangle(int px , int py , int pw , int ph , CRL cor= BLACK):Shape(px,py,cor) {
w = pw;
h = ph;
cout << "Construct Rectangle" << endl;
}
Rectangle(int pw , int ph ) {
w = pw;
h = ph;
cout << "Construct Rectangle" << endl;
}
int GetWidth() {
return w;
}
int GetHeight(){
return h;
}
double GetArea() {
return w * h;
}
double GetPerimeter() {
return 2 * (w + h);
}
};
#include"Rectangle.h"
#include<string>
int main() {
CRL color[7] = { BLACK,WHITE,GREEN,YELLOW,BROWN,RED,ORANGE };
Rectangle r1(3, 4);
cout << "矩形1的信息: " << endl;
cout << "矩形1的位置:(" << r1.Get_x() << "," << r1.Get_y() << ")" << endl;
cout << "矩形1的面积:" << r1.GetArea() << endl;
cout << "矩形1的周长:" << r1.GetPerimeter() << endl;
cout << "矩形1的颜色:" << color[static_cast<int>(r1.GetColor())] << endl;
Rectangle r2(15, 15, 8, 10, RED);
cout << "矩形2的信息: " << endl;
cout << "矩形2的位置:(" << r2.Get_x() << "," << r2.Get_y() << ")" << endl;
cout << "矩形2的面积:" << r2.GetArea() << endl;
cout << "矩形2的周长:" << r2.GetPerimeter() << endl;
cout << "矩形2的颜色:" << color[static_cast<int>(r2.GetColor())] << endl;
return 0;
}
????????以上就是我对C++继承与派生——(5)继承中的构造函数及其调用顺序的理解,希望本篇文章对你有所帮助,也希望可以支持支持博主,后续博主也会定期更新学习记录,记录学习过程中的点点滴滴。如果有不懂和发现问题的小伙伴,请在评论区说出来哦,同时我还会继续更新对C++继承与派生的理解,请持续关注我哦!!!?