码迷,mamicode.com
首页 > 其他好文 > 详细

自考新教材-p217

时间:2020-02-04 18:37:40      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:info   return   name   结果   alt   对象   析构   space   set   

源程序:

#include<iostream>
using namespace std;

class BaseClass1
{
protected:
int v1, v2;
public:
BaseClass1();
BaseClass1(int, int);
~BaseClass1();
void SetValue(int, int);
void PrintValue();
};

BaseClass1::BaseClass1() :v1(0), v2(0)
{
cout << "BaseClass1 无参构造函数" << endl;
}

BaseClass1::BaseClass1(int m, int n)
{
v1 = m;
v2 = n;
cout << "BaseClass1 带2个参构造函数" << endl;
}

BaseClass1::~BaseClass1()
{
cout << "BaseClass1 析构函数" << endl;
}

void BaseClass1::SetValue(int m, int n)
{
v1 = m;
v2 = n;
}

void BaseClass1::PrintValue()
{
cout << "v1=" << v1 << "\tv2=" << v2;
}

class BaseClass2
{
protected:
int v1, v4;
public:
BaseClass2();
BaseClass2(int, int);
~BaseClass2();
void SetValue(int, int);
void PrintValue();
};

BaseClass2::BaseClass2() :v1(1), v4(1)
{
cout << "BaseClass2 无参构造函数" << endl;
}

BaseClass2::BaseClass2(int m, int n)
{
v1 = m;
v4 = n;
cout << "BaseClass2 带2个参构造函数" << endl;
}

BaseClass2::~BaseClass2()
{
cout << "BaseClass2 析构函数" << endl;
}

void BaseClass2::SetValue(int m, int n)
{
v1 = m;
v4 = n;
}

void BaseClass2::PrintValue()
{
cout << "v1=" << v1 << "\tv4=" << v4;
}

class DerivedClass :public BaseClass1, public BaseClass2
{
public:
int v3;
public:
DerivedClass();
DerivedClass(int);
DerivedClass(int, int, int, int);
~DerivedClass();
void SetValue(int m, int n, int k, int h)
{
BaseClass1::SetValue(m, n);
BaseClass2::SetValue(2 * m, h);
v3 = k;
}
void SetValue(int m, int n, int k)
{
BaseClass1::SetValue(m, n);
BaseClass2::SetValue(2 * m, 2 * n);
v3 = -1;
}
void PrintValue();
};

DerivedClass::DerivedClass() :BaseClass1(0, 0), BaseClass2(0, 0), v3(0)
{
cout << "DerivedClass 无参构造函数" << endl;
}

DerivedClass::DerivedClass(int k)
{
v3 = k;
cout << "DerivedClass 带1个参构造函数" << endl;
}

DerivedClass::DerivedClass(int m, int n, int k, int t) :BaseClass1(m, n), BaseClass2(m, t), v3(k)
{
cout << "DerivedClass 带4个参构造函数" << endl;
}

DerivedClass::~DerivedClass()
{
cout << "DerivedClass 析构函数" << endl;
}

void DerivedClass::PrintValue()
{
BaseClass1::PrintValue();
cout << "\tv3=" << v3 << endl;
BaseClass2::PrintValue();
cout << endl;
}

int main()
{
cout << "带参数对象的创建" << endl;
DerivedClass derivedCla1(1000, 2000, 3000, 4000);
derivedCla1.PrintValue();
system("pause");
return 0;
}

运行结果:

析构函数没有显示出来,后面应该显示:

DerivedClass 析构函数

BaseClass2 析构函数

BaseClass1 析构函数

技术图片

 

自考新教材-p217

标签:info   return   name   结果   alt   对象   析构   space   set   

原文地址:https://www.cnblogs.com/duanqibo/p/12260312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!