C++程序设计实验5详解.doc
文本预览下载声明
《》
实验报告
名: 钟玲
学 号:
专 业: 通信工程
实验时间: 2016.11.15
杭州电子科技大学
通信工程学院
一、实验目的实验内容
Sum() //计算某学生三门课总成绩
Print() //输出某学生学号、姓名和成绩
modify() //修改某学生学号、姓名和成绩
(3)在主函数中,先定义一个学生类对象数组,再通过for循环给对象数组赋上实际值,最后输出对象数组各元素的值。
三、实验过程#includeiostream
using namespace std;
class A
{
public:
A(){ coutA::A()called.\n; }
~A(){ coutA::~A()called.\n; }
};
class B:public A
{
public:
B(int i)
{
coutB::B()called.\n;
buf=new char[i];
}
virtual ~B()
{
delete []buf;
coutB::~B() called.\n;
}
private:
char *buf;
};
int main()
{
B b(10);
return 0;
}
实验结果:
程序2
#includeiostream
using namespace std;
class A{
public:
A(int a, int b):x(a), y(b){ coutA constructor...endl; }
void Add(int a, int b){ x+=a; y+=b; }
void display(){ cout(x,y); }
~A(){coutdestructor A...endl;}
private:
int x, y;
};
class B:private A{
private:
int i, j;
A Aobj;
public:
B(int a, int b, int c, int d):A(a, b), i(c), j(d), Aobj(1,1)
{ coutBconstructor..endl; }
void Add(int x1, int y1, int x2, int y2)
{
A::Add(x1, y1);
i+=x2; j+=y2;
}
void display(){
A::display();
Aobj.display();
cout(i,j(endl;
}
~B(){ coutdestructor B...endl;}
};
int main()
{
B b(1,2,3,4);
b.display();
b.Add(1,3,5,7);
b.display();
return 0;
}
实验结果:
程序3
#includeiostream
using namespace std;
class A{
public:
A(int a):x(a){ coutA constructor...xendl; }
int f(){ return ++x; }
~A(){coutdestructor A...endl;}
private:
int x;
};
class B:private virtual A{
private:
int y;
A Aobj;
public:
B(int a, int b, int c):A(a), y(c), Aobj(c)
{ coutB constructor..yendl; }
int f(){
A::f();
Aobj.f();
return ++y;
}
void display(){
coutA::f()\tAobj.f()\tf()endl;
}
~B(){ coutdestructor B...endl;}
};
class C: public B{
public:
C(int a, int b, int c): B(a, b, c), A(0){ coutC constructor...endl; }
};
class D:public C, public virtual A{
public:
D(int a, int b, int c): C(a, b, c), A(c){ coutD
显示全部