小问题!如何在子类中写构造方法?并且子类的构造方法中用到父类的某个构造方法! 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include <vector>#include<iostream>using namespace std;class Shape { public: Shape() { type = 0; printf("hi, shape.type = %d\n", type); } int type; virtual ~Shape() { }};class Shape2 : public Shape{public: Shape2(int type) { this->type = type; printf("hi, shape2.type = %d\n", this->type); }};int main(){ Shape s; Shape2 s2(2); return 0;} 输出如下:hi, shape.type = 0hi, shape.type = 0hi, shape2.type = 2--------------------------第 1 行表示 Shape s;第 2, 3 行表示 Shape2 s2(2); 即, 它先调用父类 Shape 的构造函数, 再调用子类 Shape2 的构造函数. 如果 Shape(int type) { this->type = type; printf("hi, shape.type = %d\n", type); } int type; virtual ~Shape() { }Sharp(int type1 int type2){} Sharp(int type1 int type2){} Sharp2(int type1,int type2) :Sharp(type1,type2){} 手动调一下即可, 为说明方便, 只在父类中提供一行打印, code 如下:#include<iostream>using namespace std;class Shape {public: Shape() { type = 0; } Shape(int type) { type = 9; printf("hi, shape.type = %d\n", type);} int GetType(void) { return this->type; } virtual ~Shape() { } int type;};class Shape2 : public Shape{public: // 在这里手动调用 Shape2(int type) { Shape::Shape(type); }};int main(){ Shape2 s2(2); return 0;}输出为:hi, shape.type = 9 VS2008下MFC简单绘图的问题 怎样写一个类似输入法的UI程序 电子白板分割视图之后在线人数不能显示 (有源码下载) CHeaderCtrl 求一个 tcp/ip的好的代码示例(一个服务带3个客户各自互发信息的) 不要www.vckbase.com里面的例子,最好是多线程的 请问ISampleGrabberCB中的BufferCB是怎么被调用的?? 分割窗口的视图切换问题 用vc++ 编屏保 有没有知道深圳大学研究生的学号怎么排的? 来者有分. SOCKET编程写的程序,只有关了防火墙才能使用,为什么? 模态对话框的结束 我想问一下,在VC中有没有向DELPHI中的控件啊?
#include<iostream>
using namespace std;class Shape
{
public:
Shape()
{
type = 0;
printf("hi, shape.type = %d\n", type);
}
int type;
virtual ~Shape() { }
};class Shape2 : public Shape
{
public:
Shape2(int type)
{
this->type = type;
printf("hi, shape2.type = %d\n", this->type);
}
};int main()
{
Shape s;
Shape2 s2(2); return 0;
}
hi, shape.type = 0
hi, shape2.type = 2--------------------------
第 1 行表示 Shape s;
第 2, 3 行表示 Shape2 s2(2);
即, 它先调用父类 Shape 的构造函数, 再调用子类 Shape2 的构造函数.
如果 Shape(int type)
{
this->type = type;
printf("hi, shape.type = %d\n", type);
}
int type;
virtual ~Shape() { }
Sharp(int type1 int type2)
{
}
{
}
:Sharp(type1,type2)
{
}
#include<iostream>
using namespace std;class Shape
{
public:
Shape() { type = 0; }
Shape(int type) { type = 9; printf("hi, shape.type = %d\n", type);}
int GetType(void) { return this->type; }
virtual ~Shape() { } int type;};class Shape2 : public Shape
{
public:
// 在这里手动调用
Shape2(int type) { Shape::Shape(type); }
};int main()
{
Shape2 s2(2); return 0;
}输出为:
hi, shape.type = 9