设计类A和A的派生类B,要求: 1)A有两个数据成员a和b(都为int型);B中继承了A中的a和b,又定义了自己的

设计类A和A的派生类B,要求:
1)A有两个数据成员a和b(都为int型);B中继承了A中的a和b,又定义了自己的数据成员c(int型)。
2)为A添加含有两个参数的构造方法,对a和b初始化。
3)为B添加含有一个参数的构造方法,对c初始化。
4)完成主控方法,在主控方法中用B e1=new B(5)创建对象,把a、b、c分别初始化成3、4、5,最后输出它们的和。

第1个回答  2010-12-17
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
A(int s1=3,int s2=4)
{
a = s1;
b =s2;
}

int a;
int b;
};

class B:public A
{
public:
B(int s3)
{
c= s3;
}

int c;
};
void main(int argc, char* argv[])
{

B *e1 = new B(5);

cout<<(*e1).a+(*e1).b+(*e1).c<<endl;
getchar();

delete e1;
}本回答被网友采纳
第2个回答  2010-12-16
a
第3个回答  2010-12-16
a