在派生类构造函数的定义中可以省略对基类构造函数的调用,其条件是基类中必须有默认的构造函数

随文1552022-10-04 11:39:541条回答

在派生类构造函数的定义中可以省略对基类构造函数的调用,其条件是基类中必须有默认的构造函数
这句话该请怎么理解,请举例说明,

已提交,审核后显示!提交回复

共1条回复
timeprowl 共回答了20个问题 | 采纳率90%
这个动动手,写段下面这样的代码,试一下即可.
class Base
{
public Base()
{printf("I am base.");}
}
public Derived : public Base
{
public Derived()
{
// 这个构造函数会自动调用Base的构造函数
printf("I am derived.");
}
}
然后,你在main()里Dervied c = new Derived();试一下输出结果就知道了.
1年前

相关推荐

通过继承来计算圆、球、圆柱和圆锥的表面积和体积.定义一个圆类,含半径成员.球类、圆柱类和圆锥类作为圆类的派生类.计算圆、
通过继承来计算圆、球、圆柱和圆锥的表面积和体积.定义一个圆类,含半径成员.球类、圆柱类和圆锥类作为圆类的派生类.计算圆、球、圆柱和圆锥的表面积和体积.
提示:
1. Ax 在C++中可以用函数pow(A,x)来表示;
2. sqrt(x)用来表示x的开根号.
3. 如果要用上述函数,请在头文件出添加#include .
追在山尖上1年前1
lzm9332 共回答了25个问题 | 采纳率76%
#include
#include
#define N 3.14159 // 宏定义π=3.14159
using namespace std;
// 定义基类
class base
{
protected:
double r; // 定义保护数据成员——半径
float circlearea,area,height,bulk; // 定义保护数据成员底圆面积、表面积、高和体积
public:
void input()
{
cin >> r;
}
void basearea() // 计算输入的半径所对应的圆面积
{
circlearea = N * pow(r, 2);
}
};
// 定义求圆面积和体积的派生类
class circle:public base
{
public:
void Ccircle()
{
cout
对A、B两个类进行多重继承得到派生类C,则创建派生类C的对象时,从基类B中继承过来的成员变量由()类的构造函数进行初始化
对A、B两个类进行多重继承得到派生类C,则创建派生类C的对象时,从基类B中继承过来的成员变量由()类的构造函数进行初始化
1.A 2.B 3.C 4.以上都可以
丰岛1年前1
糊涂的糊涂 共回答了15个问题 | 采纳率93.3%
答案:ABC
原因AB是C的基类,那么在堆栈里AB在C的下面.
所以在构造时要先执行AB的构造函数再执行C的构造函数.
同样的析构时要先执行C的析构函数在执行AB的析构函数.
一个派生类去继承两个基类,但两个基类中有同名函数,于是在派生类中有歧义,
一个派生类去继承两个基类,但两个基类中有同名函数,于是在派生类中有歧义,
形如:
class ABC
{
public:
x05ABC(){};
x05void test(int){};
};
class XYZ
{
public:
x05XYZ(){};
x05void test(double){};
};
class child:public ABC,public XYZ
{
public:
x05child(){};
};
void main()
{
x05child a;
x05a.test(1);
}
纪达是1号傻B1年前1
她的芸豆 共回答了14个问题 | 采纳率78.6%
如果想调用ABC的test就用a.ABC::test(1);
想要XYZ的就用a.XYZ::test(1);
void main()
{
child a;
a.ABC::test(1);
a.XYZ::test(1);
}
vc++ 应用纯虚函数的使用,定义几个派生类如三角形,矩形,圆形等,求出所有形状的总面积.
machewww1年前1
lolofish 共回答了19个问题 | 采纳率84.2%
/*Shape是一个几何图形的基类,它至少有求自身周长函数Circumference()和
求自身面积函数Area().从Shape类派生出Circle类、Ellipse类、Triangle类和
Rectangle类,分别继承基类Shape的Circumference()和Area(),并增加新的成员.
编写主函数,定义各派生类对象,要求用一个函数实现求多派生类对象的周长之和、面积之和.*/
#include
#include
#define PI 3.1415926
class CShape
{
public:
x05virtual float Circumference()=0;
x05virtual float Area()=0;
};
class CCircle:public CShape
{
public:
x05CCircle(float r1)
x05{
x05x05r=r1;
x05}
x05 float Circumference()
x05 {
return (float)PI*r*2;
x05 }
x05 float Area()
x05 {
x05x05 return (float)PI*r*r;
x05 }
private:
x05float r;
};
//L = pi(1.5(a+b)-sqrt(ab)),其中a,b分别为椭圆长轴和短轴.
class CEllipse:public CShape
{
public:
x05CEllipse(float a1,float b1)
x05{
x05x05a=a1;
x05x05b=b1;
x05}
x05float Circumference()
x05{
x05x05float temp1=(float)(1.5)*(a+b);
x05x05float temp2=(float)sqrt(a*b);
return (float)PI*(temp1-temp2);
x05}
x05float Area()
x05{
x05x05return (float)PI*a*b;
x05}
private:
x05float a;
x05float b;
};
//Triangle面积公式
//s=根号下:p(p-a)(p-b)(p-c) 其中p=1/2(a+b+c) 这个公式叫海伦公式
class CTriangle:public CShape
{
public:
x05CTriangle(float a1,float b1,float c1)
x05{
x05x05a=a1;
x05x05b=b1;
x05x05c=c1;
x05}
x05x05float Circumference()
x05x05{
x05x05x05return (a+b+c);
x05x05}
x05x05float Area()
x05x05{
x05x05x05float p=(a+b+c)/2;
x05x05x05return (float)sqrt(p*(p-a)*(p-b)*(p-c));
x05x05}
private:
x05float a;
x05float b;
x05float c;
};
class CRectangle:public CShape
{
public:
x05CRectangle(float a1,float b1)
x05{
x05x05a=a1;
x05x05b=b1;
x05}
x05float Circumference()
x05{
x05x05return 2*(a+b);
x05}
x05float Area()
x05{
x05x05return (float)a*b;
x05}
private:
x05float a;
x05float b;
};
void main()
{
x05void sum(CShape *p[],int n,float &zc,float &mj);
x05CShape *p[4];
x05CCircle q(1.0);//圆
CEllipse w(2.0,1.0);//椭圆
x05CTriangle e(3.0,4.0,5.0);//三角形
x05CRectangle r(1.0,2.0);//长方形
x05p[0]=&q;
x05p[1]=&w;
x05p[2]=&e;
p[3]=&r;
x05float zc=0,mj=0;
x05sum(p,4,zc,mj);
x05cout
C++类的继承声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根
C++类的继承
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根据给出的圆心坐标和半径计算圆的面积;(2)根据给出的正方形中心坐标和一个顶点坐标计算正方形的面积。
求代码?
78678sdf1年前1
知我者谓我何求 共回答了20个问题 | 采纳率85%
可执行程序,希望对你有帮助
#include
#include
using namespace std;
class Shape
{
public:
Shape(){}
~Shape(){}
//纯虚函数
virtual float GetArea() const=0;
};
class Circle : public Shape
{
public:
Circle(float sz):r(sz)
{}
~Circle(){}
float GetArea()const;
private:
//半径
float r;
};
//圆只用半径就可以计算面积了
float Circle::GetArea()const
{
const float pi=3.1415926f;
return pi*r*r;
}
class Square : public Shape
{
public:
Square(float x1,float y1,float x2,float y2)
{
cx=x1;
cy=y1;
px=x2;
py=y2;
}
~Square(){}
float GetArea()const;
private:
//中心点
float cx;
float cy;
//顶点
float px;
float py;
};
//正方形面积=对角线乘机的一半
float Square::GetArea()const
{
return powf(2*sqrt((cx-px)*(cx-px)+(cy-py)*(cy-py)),2)/2;
}
int main()
{
Circle c(10.0);
Square s(10.0,10.0,4.0,4.0);
cout
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),
要求利用多态性的概念,分别以虚函数的形式完成对圆和正方形的周长及面积的计算.
要求:Shape类的数据成员包括中心点的坐标,Circle类和Square类初始值分别给出:圆的圆心和半径;正方形的中心和一个顶点.
爱猫YY1年前1
学好不学怀 共回答了17个问题 | 采纳率94.1%
#include
#include
#define PI 3.14
class Point
{
public:
Point(){x=0;y=0;}
Point(float x,float y){this->x=x;this->y=y;}
Point(){}
float getX(){ return x;}
float getY(){ return y;}
void setX(float x){this->x=x;}
void setY(float y){this->y=y;}
private:
float x;
float y;
};
class Shape
{
public:
Shape(){this->center.setX(0);this->center.setY(0);}
Shape(Point center){
this->center.setX(center.getX());
this->center.setY(center.getY());
}
Shape(){}
virtual float getArea(){return 0;}
virtual float getCirc(){return 0;}
protected:
Point center;
};
class Circle:public Shape
{
public:
Circle(){}
Circle(Point center,float radius)
{
this->center.setX(center.getX());
this->center.setY(center.getY());
this->radius=radius;
}
Circle(){}
float getArea(){return radius*radius*PI;}
float getCirc(){return 2*PI*radius;}
private:
float radius;
};
class Square:public Shape
{
public:
Square(){}
Square(Point center,Point top){
this->center.setX(center.getX());
this->center.setY(center.getY());
this->top.setX(top.getX());
this->top.setY(top.getY());
}
Square(){}
float getArea(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 2*(a*a+b*b);
}
float getCirc(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 4*sqrt(2)*sqrt(a*a+b*b);
}
private:
Point top;
};
void main(){
Point p1(0,0),p2(4,4);
Circle c1=Circle(p1,4);
Square s1=Square(p1,p2);
Shape s=c1;
cout
在C#中,定义派生类时,指定基类应使用的语句是( ).A.Inherits B.:C.Class D.Overrides
在C#中,定义派生类时,指定基类应使用的语句是( ).A.Inherits B.:C.Class D.Overrides 谢
取了黑多名字1年前1
千分之7思远 共回答了18个问题 | 采纳率72.2%
c#使用冒号“:”来指定基类,所以选B
如下,表示派生类X,继承自Y
public class X:Y
{
/////
}
几道小题.第一题:带有虚基类的多层派生类构造函数的成员初始化列表中都要列出虚基类构造函数,这样将对虚基类的子对象初始化几
几道小题.
第一题:
带有虚基类的多层派生类构造函数的成员初始化列表中都要列出虚基类构造函数,这样将对虚基类的子对象初始化几次?
a> 与虚基类下面的派生类个数有关 b> 多次 c> 2次 d> 1次
第二题:
若要强制c++对虚函数的调用使用静态联编,则在调用中对该函数使用?
a> 成员名限定 b> 指针 c> 引用 d> virtual关键字
第三题:
模板和继承的关系是
a>模板反映的是不同类型的相同操作;而同一类系的各种类处理的数据很多情况下是同一类型的
b> 继承类较模板类有更多的动态特征
c> 模板类系的成员较继承类系的成员稳定
第四题:
派生类的对象与基类的对象的关系是?
a> 属于前者则不属于后者 b> 属于前者,不一定属于后者
c> 不属于前者则一定不属于后者
d> 属于前者则一定属于后者
第五题:
下列程序段中具有相同值的是?
class base {int b;};
class base1:virtual public base{int b1;};
class base2:virtual public base{int b2;};
class derived:public base1,public base2{int b3;};
derived d;
答案中说d.b与d.base1::b有相同值.可是d.b这种用法不会产生二义性吗?
第六题:
类b是类a的共有派生类,;类a和类b中都定义了虚函数 func(),p是一个指向类a对象的指针,则p->a::func()将?
a> 调用类a中的函数func() b> 调用b中的.
c> 根据p所指的对象类型来确定
c选项为什麽不行呢?是::起作用了?
第七题:
下例程序中错误的语句是?
class a
{
public:virtual void f()=0; //a
void g(){f();} //b
a(){f();} //c
};
第八题:
下面程序段中,说法正确的是?
class location
{
public:
int getx();
};
class rectangle:private location
{
public:
int getw();
};
a> 类location中的public成员在类rectangle中是不可访问
b> void f()
{
rectangle r;
int x=r.getx();
}
其中 int x=getx();是***语句
第九题:
“在构造函数和析构函数中调用虚函数时,采用‘静态联编’.”
“若析构函数是虚函数,则delete对析构函数的调用采用‘动态联编’.”
这两句话迷糊我了,
GZ_WING1年前1
葡萄球菌 共回答了12个问题 | 采纳率100%
oh,my god!
c++解决二义性的方法解决类继承中产生的二义性的方法不包括  A)使用作用域运算符限定访问的成员函数  B)在派生类中定
c++解决二义性的方法
解决类继承中产生的二义性的方法不包括
  A)使用作用域运算符限定访问的成员函数
  B)在派生类中定义同名函数,且参数表必须和基类的保持一致
  C)在派生类中定义同名函数,且参数表可以和基类的不同
  D)采用虚基类解决多重继承中的共同基类产生的二义性
请解释一下
飞机头1年前1
maidou0416 共回答了22个问题 | 采纳率95.5%
解决方法:
法一 、类名限定:
调用时指名调用的是哪个类的函数,如:
c1.A::f();
c1.B::f();
法二、 同名覆盖:
在C中声明一个同名函数,该函数根据需要内部调用A的f或者是B的f。如:
class C:public A,public B{
public:
void g();
void h();
void f(){
A::f();
}
};
法三、虚基类(用于有共同基类的场合):
virtual 修饰说明基类,如:
class B1:virtual public B