一、实验目的
1、掌握继承机制。
2、掌握虚函数。
3、理解并掌握虚基类。
二、实验内容
1、编写一个程序:设计一个汽车类,数据成员有轮子个数、车重。小车类是汽车类的私有派生类,包含载客量。卡车类是汽车类的私有派生类,包含载客数和载重量。每个类都有数据的输出方法。
2、虚基类为Shape,从Shape派生出矩形类(宽、高)、椭圆类(横轴、纵轴)、三角形类、梯形类。给出各个类的构造函数,成员初始化,在基类中定义虚函数GetArea()(计算面积)和GetPeri()( 计算周长),在派生类中改写。写出该程序的实现。
3.定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass中定义虚析构函数,在主程序中将一个DerivedClass的对象地址赋给一个BaseClass的指针,并输出构造和析构信息。
4.下面是整型安全数组类的一部分:试完善类的定义,使下段程序能够正常运行且不会出现内存泄漏。
#include <iostream.h>
class Array
{
public:
Array(int x)
{
count=x;
p=new int[count];
}
int & operator [](int x)
{return *(p+x);}
protected:
int count;
int *p;
};
int main()
{
Array a(10);
Array b(a);
//后续程序段略
}
- 定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用BaseClass和DerivedClass的指针来调用fn1()、fn2()。
#include"iostream"
using namespace std;
class BaseClass
{
public:
virtual void fn1();
void fn2();
BaseClass()
{
cout<<" Destruct BaseClass "<<endl;
}
};
class DerivedClass:public BaseClass
{
public:
void fn1();
void fn2();
DerivedClass()
{
cout<<"Destruct DerivedClass"<<endl;
}
};
int main()
{
DerivedClass d1;
DerivedClass *d2=&d1;
BaseClass *b1=&d1;
d2->fn1();
d2->fn2();
b1->fn1();
b1->fn2();
return 0;
}
void BaseClass::fn1()
{
cout<<"the fn1 of BaseClass"<<endl;
}
void BaseClass::fn2()
{
cout<<"the fn2 of BaseClass"<<endl;
}
void DerivedClass::fn1()
{
cout<<"the fn1 of DerivedClass"<<endl;
}
void DerivedClass::fn2()
{
cout<<"the fn2 of DerivedClass"<<endl;
}
- 使用private和protected继承从基类创建两个新类。然后通过派生类的对象访问基类的私有数据成员(通过函数)。
三、源代码
#include <iostream>
using namespace std;
class car{
protected:
int wheels,weight;
public:
acar(int a,int b){
wheels=a;
weight=b;
}
void print(){
cout<<"车轮数为:"<<wheels<<endl;
cout<<"重量为:"<<weight<<endl;
}
};
class mincar:private car{
int minpassenger;
public:
car::acar;
car::print;
mincar(int wheels,int weight){
acar(wheels,weight);
minpassenger=4;
}
void print(){
cout<<"小车载客量为:"<<minpassenger<<endl;
car::print();
}
};
class maxcar:private car{
int maxpassenger,payload;
public:
car::acar;
car::print;
maxcar(int wheels,int weight){
acar(wheels,weight);
maxpassenger=9;
payload=2400;
}
void print(){
cout<<"卡车载客量为:"<<maxpassenger<<endl;
cout<<"卡车核载为:"<<payload<<endl;
car::print();
}
};
int main()
{
mincar a(4,3000);
maxcar b(8,5000);
a.print();
b.print();
return 0;
}
#include<iostream>
using namespace std;
#define pi 3.14
class Shape
{
public:
float x, y, wide, high,topwide;
Shape() {};
virtual void GetArea() = 0;//虚函数GetArea()(计算面积)
virtual void GetPeri() = 0;
virtual ~Shape() {};
};
//矩形类
class rectangle :virtual public Shape
{
public:
rectangle(float a, float b)//矩形类的构造函数
{
wide = a;
high = b;
}
void GetArea() {
cout << "矩形面积为:" << wide * high << endl;
}
void GetPeri() {
cout << "矩形周长为:" << 2 * (wide + high) << endl;
}
};
//椭圆类
class ellipse :virtual public Shape
{
public:
ellipse(float a, float b)//椭圆类的构造函数
{
x = a;
y = b;
}
void GetArea() {
cout << "椭圆面积为:" << pi * x/2 * y/2 << endl;
}
void GetPeri() {
cout << "椭圆周长为:" <<2*pi*y/2+2*(x-y)<< endl;
}
};
//三角形类
class triangle :virtual public Shape
{
public:
triangle(float a, float b)//三角形类的构造函数
{
wide = a;
high = b;
}
void GetArea() {
cout << "三角形面积为:" << wide*high/ 2 << endl;
}
void GetPeri() {
cout << "三角形周长为:" <<wide+high+5<< endl;
}
};
//梯形类
class trapezoid :virtual public Shape
{
public:
trapezoid(float a, float b, float c)//梯形类的构造函数
{
topwide = a;
wide = b;
high = c;
}
void GetArea() {
cout << "梯形面积为:" << (topwide+wide)* high / 2 << endl;
}
void GetPeri() {
cout << "梯形周长为:" << topwide + wide+ high+5 << endl;
}
};
int main()
{
rectangle b1(3, 4);
b1.GetArea();
b1.GetPeri();
ellipse b2(6,4);
b2.GetArea();
b2.GetPeri();
triangle b3(3, 4);
b3.GetArea();
b3.GetPeri();
trapezoid b4(2, 5,4);
b4.GetArea();
b4.GetPeri();
return 0;
}
#include"iostream"
using namespace std;
class BaseClass
{
public:
BaseClass()
{
cout<<"inside BaseClass"<<endl;
}
virtual ~BaseClass()
{
cout<<"outside BaseClass"<<endl;
}
};
class DerivedClass:public BaseClass
{
public:
DerivedClass()
{
cout<<"insode DerivedClass"<<endl;
}
~DerivedClass()
{
cout<<"outside DerivedClass"<<endl;
}
};
int main()
{
BaseClass *b=new DerivedClass;
delete b;
return 0;
}
4. #include <iostream>
using namespace std;
class Array
{
public:
Array(int x)
{
count=x;
p=new int[count];
}
int & operator [](int x)
{
if(x<count)
return *(p+x);
else{
cout<<"error!"<<endl;
return *p;
}
}
~Array(){
if(p)
delete[]p;
}
protected:
int count;
int *p;
};
int main()
{
Array a(10);
Array b(a);
return 0;
}
5. #include<iostream>
using namespace std;
class A
{
private:
int a;
public:
A(int x) :a(x) {}
void GetA()
{
cout << a << endl;
}
};
class B:protected A
{
private:
int c;
public:
B(int x, int y) :A(x),c(y) {}
void GetB()
{
GetA();
cout << c << endl;
}
};
class C :private A
{
private:
int c;
public:
C(int x, int y) :A(x),c(y) {}
void GetC()
{
GetA();
cout << c << endl;
}
};
int main()
{
C c1(1,3);
B b1(1, 2);
c1.GetC();
b1.GetB();
return 0;
}