C++ 继承:各种派生对象的基本属性
C++ Inheritance : Base attributes over various derived objects
我目前正在学习 C++ 继承,所以如果这个问题很愚蠢,我提前道歉。
实现这个场景:
超级class有一个颜色属性,可以是任何颜色(假设颜色用整数表示)。
假设我初始化了这个超级 class,颜色为红色。
我还将初始化一个子class的不同对象,它们也共享红色。
我的问题是,有没有什么方法可以将这个属性颜色初始化为红色(或任何颜色),并且它会自动被它的子class的对象继承,而不是每次初始化这些对象之一时都将属性设置为红色?
如果我在这里遗漏了一个基本概念,再次道歉,但我似乎无法在网上找到任何关于此的内容。
每个请求的伪代码:
超class码:
class Shape {
int color;
Shape::Shape(int c) : color(c) { } //constructor
}
子Class代码:
class Square {
int length, width;
Square::Square(int l, int w, int c)
: length(l),
width(w),
color(c)
{ }
}
class Circle {
int radius;
Square::Square(int r, int c)
: radius(r),
color(c)
{ }
}
我想说的是正方形和圆形都需要有相同的颜色,无论如何(也许来自超级class?)声明这种颜色(例如红色),两种形状的颜色设置是否相同?
在 C++11 之前,执行此操作的主要方法如下:
class baseClass
{
int color;
baseClass() { color = RED; }
};
class subClass : public baseClass
{
subclass() { }
};
对于 C++11 及更高版本,您可以在 class 声明中分配默认值:
class baseClass
{
int color = RED;
baseClass() { }
};
这会被继承。
编辑:如下所述,在这种情况下会自动调用默认的 baseClass
构造函数。
class 和 class 的实例之间存在差异。 class 类似于一个结构,根据它可以创建更多的实例。 class 类似于描述,是关于如何构建对象的说明。继承与 classes 有关,与对象无关。至于字段,继承的 class 具有来自父 class 的所有相同字段以及您可以添加的一些新字段。
给你一个简单的例子:
class MyBase {
public:
int color;
};
class MyChild : public MyBase {
public:
double length;
}
int main() {
MyBase myBaseObj;
myBaseObj.color = 1;
MyChild myChildObj;
myChildObj.color = 2;
myChildObj.length = 3.14;
return 0;
}
is there anyway (maybe from the super class? ) to declare this color (ex. red), and both shapes would have this color set the same?
在您发布的示例中,是的。 super-class 构造函数可以将其变量设置为它想要的任何值,例如:
Shape::Shape(int c) : color(7) { } // 7 == RED
如果 Shape
定义了构造函数,那么每个子class 的实例将代表红色形状。
此示例程序打印 7,覆盖用户选择的颜色:
#include <iostream>
class Shape {
public:
int color;
Shape(int c) : color(7) { } //constructor
};
class Square : public Shape {
public:
int length, width;
Square(int l, int w, int c)
: Shape(c),
length(l),
width(w)
{ }
};
class Circle : public Shape {
public:
int radius;
Circle(int r, int c)
: Shape(c),
radius(r)
{ }
};
int main() {
Square q(4,8,0);
std::cout << q.color << "\n";
}
您可以使用静态 default_color
完成您想要的操作,它在未明确指定颜色时使用,并在指定颜色时设置。
struct Shape {
static int default_color;
int color;
Shape(int c) : color(c)
{
default_color = c;
}
Shape() : color(default_color) {}
};
Shape::default_color = BLACK;
struct Square : public Shape {
int length, width;
Square(int l, int w, int c)
: Shape(c),
length(l),
width(w),
{ }
Square(int l, int w)
: length(l),
width(w)
{ }
}
struct Circle : public Shape {
int radius;
Circle(int r, int c)
: Shape(c),
radius(r)
{ }
Circle(int r)
: radius(r)
{ }
}
int main()
{
Square sq(2, 3, RED);
Circle cir(10); // Automatically red, since that's
the last color explicitly specified
}
不过我会说这是一个糟糕的设计。像这样的全局状态很容易出错。现在,无论何时创建形状,您都必须考虑整个程序的状态。最好将 Circle
简单地创建为 Circle cir(10, sq.color);
。这使得你的 Circle
是什么颜色变得明确,并减少了程序员的认知负担。
我目前正在学习 C++ 继承,所以如果这个问题很愚蠢,我提前道歉。
实现这个场景:
超级class有一个颜色属性,可以是任何颜色(假设颜色用整数表示)。
假设我初始化了这个超级 class,颜色为红色。
我还将初始化一个子class的不同对象,它们也共享红色。
我的问题是,有没有什么方法可以将这个属性颜色初始化为红色(或任何颜色),并且它会自动被它的子class的对象继承,而不是每次初始化这些对象之一时都将属性设置为红色?
如果我在这里遗漏了一个基本概念,再次道歉,但我似乎无法在网上找到任何关于此的内容。
每个请求的伪代码:
超class码:
class Shape {
int color;
Shape::Shape(int c) : color(c) { } //constructor
}
子Class代码:
class Square {
int length, width;
Square::Square(int l, int w, int c)
: length(l),
width(w),
color(c)
{ }
}
class Circle {
int radius;
Square::Square(int r, int c)
: radius(r),
color(c)
{ }
}
我想说的是正方形和圆形都需要有相同的颜色,无论如何(也许来自超级class?)声明这种颜色(例如红色),两种形状的颜色设置是否相同?
在 C++11 之前,执行此操作的主要方法如下:
class baseClass
{
int color;
baseClass() { color = RED; }
};
class subClass : public baseClass
{
subclass() { }
};
对于 C++11 及更高版本,您可以在 class 声明中分配默认值:
class baseClass
{
int color = RED;
baseClass() { }
};
这会被继承。
编辑:如下所述,在这种情况下会自动调用默认的 baseClass
构造函数。
class 和 class 的实例之间存在差异。 class 类似于一个结构,根据它可以创建更多的实例。 class 类似于描述,是关于如何构建对象的说明。继承与 classes 有关,与对象无关。至于字段,继承的 class 具有来自父 class 的所有相同字段以及您可以添加的一些新字段。
给你一个简单的例子:
class MyBase {
public:
int color;
};
class MyChild : public MyBase {
public:
double length;
}
int main() {
MyBase myBaseObj;
myBaseObj.color = 1;
MyChild myChildObj;
myChildObj.color = 2;
myChildObj.length = 3.14;
return 0;
}
is there anyway (maybe from the super class? ) to declare this color (ex. red), and both shapes would have this color set the same?
在您发布的示例中,是的。 super-class 构造函数可以将其变量设置为它想要的任何值,例如:
Shape::Shape(int c) : color(7) { } // 7 == RED
如果 Shape
定义了构造函数,那么每个子class 的实例将代表红色形状。
此示例程序打印 7,覆盖用户选择的颜色:
#include <iostream>
class Shape {
public:
int color;
Shape(int c) : color(7) { } //constructor
};
class Square : public Shape {
public:
int length, width;
Square(int l, int w, int c)
: Shape(c),
length(l),
width(w)
{ }
};
class Circle : public Shape {
public:
int radius;
Circle(int r, int c)
: Shape(c),
radius(r)
{ }
};
int main() {
Square q(4,8,0);
std::cout << q.color << "\n";
}
您可以使用静态 default_color
完成您想要的操作,它在未明确指定颜色时使用,并在指定颜色时设置。
struct Shape {
static int default_color;
int color;
Shape(int c) : color(c)
{
default_color = c;
}
Shape() : color(default_color) {}
};
Shape::default_color = BLACK;
struct Square : public Shape {
int length, width;
Square(int l, int w, int c)
: Shape(c),
length(l),
width(w),
{ }
Square(int l, int w)
: length(l),
width(w)
{ }
}
struct Circle : public Shape {
int radius;
Circle(int r, int c)
: Shape(c),
radius(r)
{ }
Circle(int r)
: radius(r)
{ }
}
int main()
{
Square sq(2, 3, RED);
Circle cir(10); // Automatically red, since that's
the last color explicitly specified
}
不过我会说这是一个糟糕的设计。像这样的全局状态很容易出错。现在,无论何时创建形状,您都必须考虑整个程序的状态。最好将 Circle
简单地创建为 Circle cir(10, sq.color);
。这使得你的 Circle
是什么颜色变得明确,并减少了程序员的认知负担。