实验六 派生与继承—多基派生
作业代写代做 6.1 实验目的 1.理解多基派生的定义;2.理解多基派生中构造函数与析构函数的调用顺序;3.理解多基派生中虚基类的作用;1.理解下面的程序并运行,然后回答后面的问题。问题一:改正以上程序中的错误,并分析输出结果。2.理解下面的程序并运行,然后回答后面的问题。
6.1 实验目的 作业代写代做
1.理解多基派生的定义;
2.理解多基派生中构造函数与析构函数的调用顺序;
3.理解多基派生中虚基类的作用;
6.2 实验内容
6.2.1程序阅读
1.理解下面的程序并运行,然后回答后面的问题。
class CBase1
{
public:
CBase1(int a)
:a(a)
{
cout<<"base1 structure..."<<endl;
}
~CBase1()
{
cout<<"base1 destructure..."<<endl;
}
void print()
{
cout<<"a="<<a<<endl;
}
protected:
int a;
};
class CBase2
{
public:
CBase2(int b)
:b(b)
{
cout<<"base2 structure..."<<endl;
}
~CBase2()
{
cout<<"base2 destructure..."<<endl;
}
void print()
{
cout<<"b="<<b<<endl;
}
protected:
int b;
};
class CDerive : public CBase1, public CBase2
{
public:
CDerive()
{
cout<<"derive structure..."<<endl;
}
~CDerive()
{
cout<<"derive destructure..."<<endl;
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout<<"c="<<c<<endl;
}
private:
CBase1 b1;
CBase2 b2;
int c;
};
void main()
{
CDerive d;
d.print();
}
问题一:改正以上程序中的错误,并分析输出结果。
2.理解下面的程序并运行,然后回答后面的问题。 作业代写代做
#include "iostream.h"
class CBase
{
public:
CBase(int a)
:a(a)
{
}
int a;
};
class CDerive1 : public CBase
{
public:
CDerive1(int a)
:CBase(a)
{
}
};
class CDerive2 : public CBase
{
public:
CDerive2(int a)
:CBase(a)
{
}
};
class CDerive : public CDerive1,public CDerive2
{
public:
CDerive(int a,int b)
:CDerive1(a),CDerive2(b)
{
}
};
void main()
{
CDerive d(1,2);
cout<<d.a<<endl;
}
问题一:在不改变原有程序意图的前提下,分别用三种方法改正以上程序,并使程序正确输出。

实验七 多态性—函数与运算符重载 作业代写代做
7.1 实验目的
1.理解静态联编和动态联编的概念;
2.掌握成员函数方式运算符重载;
3.掌握友元函数方式运算符重载;
4.掌握++、–、=运算符的重载。
7.2 实验内容
7.2.1程序阅读
1.理解下面的程序并运行,然后回答后面的问题。
#include "iostream.h"
class CComplex
{
public:
CComplex()
{
real = 0;
imag = 0;
}
CComplex(int x,int y)
{
real = x;
imag = y;
}
int real;
int imag;
CComplex operator + (CComplex obj1)-----------------------------------------------①
{
CComplex obj2(real + obj1.real, imag + obj1.imag);
return obj2;
}
};
void main()
{
CComplex obj1(100,30);
CComplex obj2(20, 30);
CComplex obj;
obj = obj1+obj2; ------------------------------------------------------------------②
cout << obj.real <<endl;
cout << obj.imag << endl;
}
问题一:①处的运算符重载,为什么该函数的返回值要设计成CComplex类型? 作业代写代做
问题二:②处的运算符重载函数调用就相当于“obj=operator+(obj1,obj2);”,请问CComplex类中的运算符重载函数为什么只有一个参数?
2.理解下面的程序并运行,然后回答后面的问题。
#include "iostream.h"
class CComplex
{
public:
CComplex()
{
real = 0.0;
imag = 0.0;
}
CComplex(float x, float y)
{
real = x;
imag = y;
}
CComplex operator + (CComplex &obj1, CComplex &obj2)
{
CComplex obj3(obj1.real + obj2.real, obj1.imag + obj2.imag);
return obj3;
}
CComplex &operator++(CComplex &obj)
{
obj.real += 1;
obj.imag +=1;
return obj;
}
void print()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
private:
float real;
float imag;
};
CComplex &operator--(CComplex &x)
{
x.real -= 1;
x.imag -= 1;
return x;
}
void main()
{
CComplex obj1(2.1,3.2);
CComplex obj2(3.6,2.5);
cout<<"obj1=";
obj1.print();
cout<<"obj2=";
obj2.print();
CComplex obj3 = obj1 + obj2;
cout<<"befor++, obj3=";
obj3.print();
++obj3;
cout<<"after++, obj3=";
obj3.print();
--obj3;
cout<<"after--, obj3=";
obj3.print();
CComplex obj4 = ++obj3;
cout<<"obj4=";
obj4.print();
}
问题一:以上程序中的三个运算符重载都有错误,试改正并分析输出结果。
7.2.2 程序设计
1.把7.2.1中第一道题的程序改造成采取友元函数重载方式来实现“+”运算符,并采取友元函数重载方式增加前置和后置“++”以及“–”运算符重载,并设计主函数来验证重载运算符的用法。
实验八 多态性—类型转换与虚函数 作业代写代做
8.1 实验目的
1.理解运算符[]、()的重载;
2.理解类型转换;
3.掌握虚函数的作用;
4.掌握利用虚函数实现C++的运行时多态性;
5.理解纯虚类和抽象类。
8.2 实验内容
8.2.1程序阅读
1.理解下面的程序并运行,然后回答后面的问题。
#include <iostream.h>
#include "stdlib.h"
class CComplex
{
public:
CComplex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
int operator int()
{
return (int)real;
}
void Display(void)
{
cout << "(" << real << "," << imag << ")" << endl;
}
protected:
double real;
double imag;
};
class CVector
{
public:
CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4)
{
objArray[0] = obj1;
objArray[1] = obj2;
objArray[2] = obj3;
objArray[3] = obj4;
}
friend CComplex &operator[](CVector obj, int n);
private:
CComplex objArray[4];
};
CComplex &operator[](CVector obj, int n)
{
if(n<0 || n>3)
{
cout<<"Out of range!"<<endl;
exit(0);
}
return obj.objArray[n];
}
void main()
{
CComplex c1(1.1, 1.1);
CComplex c2(2.2, 2.2);
CComplex c3(3.3, 3.3);
CComplex c4(4.4, 4.4);
CVector v(c1,c2,c3,c4);
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
v[0] = 5.5; ----------------------------------------------------------①
v[1] = CComplex(6.6); -------------------------------------------②
v[2] = int(CComplex(7.7)); --------------------------------------③
v[3] = int(CComplex(8.8,9.9)); ----------------------------------④
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
}
问题一:上述程序存在两个错误,请改正。
问题二:①处的转换属于显式转换还是隐式转换,解释该转换过程。 作业代写代做
问题三:②处的转换属于显式转换还是隐式转换,解释该转换过程。
问题四:解释③处的转换过程。
问题五:解释④处的转换过程。

