《C++语言基础》程序阅读——继承和派生
分类: C++ 2015-05-12 15:52 33人阅读 评论(0) 收藏 举报
返回:贺老师课程教学链接
(1)
#include <iostream>
using namespace std;
class Data
{
public:
Data(int i):x(i)
{
cout<<"A";
}
~Data()
{
cout<<"B";
}
private:
int x;
};
class Base
{
public:
Base(int i):b1(i)
{
cout<<"C";
}
~Base()
{
cout<<"D";
}
private:
int b1;
};
class Derived:public Base
{
public:
Derived (int i,int j):Base(i),d1(j)
{
cout<<"E";
}
~Derived()
{
cout<<"F";
}
private:
Data d1;
};
int main()
{
Derived obj(1,2);
return 0;
}
(2)
#include<iostream>
using namespace std;
class G
{
public:
static int m;
G( ) //构造函数
{
m++;
cout<<"G begins\n";
}
~G( )
{
cout<<"G ends\n";
m--;
}
};
int G::m=0;
class D:public G
{
public:
D( ) //构造函数
{
m++;
cout<<"D begins\n";
}
~D( )
{
cout<<"D ends\n" ;
m-- ;
}
};
int main( )
{
D objg;
cout<<G::m<<endl;
return 0;
}
(3)
#include<iostream>
using namespace std;
class A
{
private:
int x;
protected:
int y;
public:
int z;
A(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
void ShowA()
{
cout<< "x="<<x<<'\t';
cout<<"y="<<y<<'\t';
cout<<"z="<<z<<'\n';
}
};
class B:public A //修改点(见后面阅读要求)
{
private:
int m,n;
public:
B(int a,int b,int c,int d,int e):A(a,b,c)
{
m=d;
n=e;
}
void Show()
{
cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';
cout<<"x="<<Getx()<<'\t';
cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
}
int Sum()
{
return (Getx()+y+z+m+n);
}
};
int main()
{
B b1(1,2,3,4,5);
b1.ShowA();
b1.Show();
cout<< "Sum="<<b1.Sum()<<'\n';
cout<<"x="<<b1.Getx()<<'\t';
cout << "y=" <<b1.Gety()<<'\t';
cout << "z="<<b1.z<<'\n';
return 0;
}
?先阅读程序,写出你预想的运行结果,再记录运行结果,如果两者有差异,再读程序给出解释。
?观察基类A中成员的访问权限制以及派生类B中对这些成员的访问方法。
?请将class B:public A 中的public改为protected或者删除,对程序进行编译,解释出现的情况。
#include<iostream>
using namespace std;
class A
{
private:
int x;//x是私有的,只能在基类中被访问
protected:
int y;//y是受保护类型,能够在派生类中访问
public:
int z;//z是公有的,能够在基类和派生类中访问
A(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
void ShowA()
{
cout<< "x="<<x<<'\t';
cout<<"y="<<y<<'\t';
cout<<"z="<<z<<'\n';
}
};
class B:public A //修改点(见后面阅读要求)
{
private:
int m,n;
public:
B(int a,int b,int c,int d,int e):A(a,b,c)
{
m=d;
n=e;
}//派生类构造函数的初始化定义
void Show()
{
cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';
cout<<"x="<<Getx()<<'\t';//Getx()函数是一个public的访问属性,可以这样使用,但如果改成直接输出x就错了
cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
}
int Sum()
{
return (Getx()+y+z+m+n);
}
};
int main()
{
B b1(1,2,3,4,5);
b1.ShowA();
b1.Show();
cout<< "Sum="<<b1.Sum()<<'\n';
cout<<"x="<<b1.Getx()<<'\t';
cout << "y=" <<b1.Gety()<<'\t';
cout << "z="<<b1.z<<'\n';
return 0;
}
#include<iostream>
using namespace std;
class A
{
private:
int x;//x是私有的,只能在基类中被访问
protected:
int y;//y是受保护类型,能够在派生类中访问
public:
int z;//z是公有的,能够在基类和派生类中访问
A(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
void ShowA()
{
cout<< "x="<<x<<'\t';
cout<<"y="<<y<<'\t';
cout<<"z="<<z<<'\n';
}
};
class B:protected A //修改点(见后面阅读要求)
{
private:
int m,n;
public:
B(int a,int b,int c,int d,int e):A(a,b,c)
{
m=d;
n=e;
}//派生类构造函数的初始化定义
void Show()
{
cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';
cout<<"x="<<Getx()<<'\t';//Getx()函数是一个public的访问属性,可以这样使用,但如果改成直接输出x就错了
cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
}
int Sum()
{
return (Getx()+y+z+m+n);
}
};
int main()
{
B b1(1,2,3,4,5);
b1.ShowA();
b1.Show();
cout<< "Sum="<<b1.Sum()<<'\n';
cout<<"x="<<b1.Getx()<<'\t';
cout << "y=" <<b1.Gety()<<'\t';
cout << "z="<<b1.z<<'\n';
return 0;
}
不能运行的原因:
因为基类的公有成员函数在保护派生类中的属性是被保护的,外界不能调用,而且派生类的成员函数不能引用基类的私有成员。
(4)
#include <iostream>
using namespace std;
class Part //部件类
{
public:
Part();
Part(int i);
~Part();
private:
int val;
};
Part::Part()
{
val=0;
cout<<"调用Part的默认构造函数:"<<val<<endl;
}
Part::Part(int i)
{
val=i;
cout<<"调用Part的构造函数: "<<val<<endl;
}
Part::~Part()
{
cout<<"调用Part的析构函数: "<<val<<endl;
}
class Whole: public Part
{
public:
Whole();
Whole(int,int,int,int);
~Whole();
private:
Part one;
Part two;
int data;
};
Whole::Whole()
{
data=0;
cout<<"调用whole的默认构造函数: "<<data<<endl;
}
Whole::Whole(int p, int i,int j,int k):Part(p),one(j),two(i),data(k) //问题2
{
cout<<"调用whole的构造函数: "<<data<<endl;
}
Whole::~Whole()
{
cout<<"调用whole的析构函数: "<<data<<endl;
}
void f()
{
Whole w1;
Whole w2(1,2,3,4);
}
int main()
{
f();
return 0;
}
1.先分析程序的执行结果,在上机时运行程序进行对照,再通过单步执行跟踪程序的运行,达到理解基类、派生类中构造函数、析构函数执行过程的目的。
通过单步执行,执行派生类构造函数的顺序是:
调用基类的构造函数,再调用派生类本身的构造函数;
若派生类中同时调用基类的构造函数,例如:one(j),two(i),那么先执行后面的。
析构函数:
先执行派生类的析构函数,然后调用基类的析构函数,对基类进行清理。
2.将Whole类的构造函数(见注释//问题2)改为下面形式,请解释出现的警告信息。
Whole::Whole(int p, int i,int j,int k): Part(p),two(i),one(j),data(k) //问题2
在数据成员的声明时,我们事先声明的one子对象,然后在声明two;