第七章
第五题感觉题目有点模糊,不懂究竟要干什么
6.
#include<iostream>
#include<cstdio>
using namespace std;
class Mamal
{
public:
Mamal(){cout << "Calling Mamal construction" << endl;}
~Mamal(){cout << "Destructing Mamal" << endl;}
};
class Dog:public Mamal
{
public:
Dog(){cout << "Calling Dog construction" << endl;}
~Dog(){cout << "Destructing Dog" << endl;}
};
int main()
{
Dog d;
return 0;
}
10.
#include <iostream>
using namespace std;
class object
{
private:
int Weight;
public:
object()
{
cout << "Constructing object" << endl;
Weight = 0;
}
int GetWeight(){ return Weight;}
void SetWeight(int n){ Weight = n;}
~object() { cout << "Destructing object" << endl;}
};
class box : public object
{
private:
int Height,Width;
public:
box()
{
cout << "Constructing box" << endl;
Height = Width = 0;
}
int GetHeight(){ return Height;}
void SetHeight(int n){ Height = n;}
int GetWidth(){ return Width;}
void SetWidth(int n){ Width = n;}
~box() { cout << "Destructing box" << endl;}
};
int main()
{
box a;
return 0;
}
11.
//这道题我不明白指针的指向,
#include <iostream>
using namespace std;
class BaseClass
{
public:
void fn1();
void fn2();
};
void BaseClass::fn1()
{
cout << "Calling the base class fn1()" << endl;
}
void BaseClass::fn2()
{
cout << "Calling the base class fn2()" << endl;
}
class DerivedClass : public BaseClass
{
public:
void fn1();
void fn2();
};
void DerivedClass::fn1()
{
cout << "Calling the derived class fn1()" << endl;
}
void DerivedClass::fn2()
{
cout << "Calling the derived class fn2()" << endl;
}
int main()
{
DerivedClass aDerivedClass;
DerivedClass *pDerivedClass = &aDerivedClass;
BaseClass *pBaseClass = &aDerivedClass;
aDerivedClass.fn1();
aDerivedClass.fn2();
pBaseClass->fn1();
pBaseClass->fn2();
pDerivedClass->fn1();
pDerivedClass->fn2();
return 0;
}