实验目的:
掌握派生类的声明方法和和派生类构造函数的定义方法
掌握不同方式下,基类成员在派生类中的访问属性和访问规则
程序段:
# include < iostream >
using namespace std;
class Base {
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
public:
int x;
};
class Derived : public Base
{
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << " Base :: x =" << x << endl; // Il 语句1
}
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); // 语句5
cout << " Base :: x =" << bb.x << endl; //语句6
cout << " Derived :: y =" << bb.y << endl;//语句7
cout << " Base :: x =" << bb.getx() << endl; //语句8
cout << " Derived :: y =" << bb.gety() << endl; //语句9
return 0;
}
运行结果:
1、将基类Base中数据成员x的访问权限改为private时,会出现哪些错误,为什么?
# include < iostream >
using namespace std;
class Base
{
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
private:
int x;
};
class Derived : public Base
{
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << " Base :: x =" << x << endl; // Il 语句1
}
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); // 语句5
cout << " Base :: x =" << bb.x << endl; //语句6
cout << " Derived :: y =" << bb.y << endl;//语句7
cout << " Base :: x =" << bb.getx() << endl; //语句8
cout << " Derived :: y =" << bb.gety() << endl; //语句9
return 0;
}
运行结果:
基类中的成员x不能直接访问private成员,由于成员x被保护起来,基类中的共有成员无法直接访问私有成员x的值,故语句6,程序段如下无法运行:
cout << " Base :: x =" << bb.x << endl; //语句6
但是基类中调用函数get可以通过函数调用访问x的值,故语句8,程序段如下可以运行:
cout << " Base :: x =" << bb.getx() << endl; //语句8
并且派生类中的公有成员也无法访问基类中的私有成员:故语句1,程序段如下无法运行:
cout << " Base :: x =" << x << endl; // 语句1
2、将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误,为什么?
# include < iostream >
using namespace std;
class Base
{
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
protected:
int x;
};
class Derived : public Base
{
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << " Base :: x =" << x << endl; // Il 语句1
}
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); // 语句5
cout << " Base :: x =" << bb.x << endl; //语句6
cout << " Derived :: y =" << bb.y << endl;//语句7
cout << " Base :: x =" << bb.getx() << endl; //语句8
cout << " Derived :: y =" << bb.gety() << endl; //语句9
return 0;
}
运行结果:
基类中的共有成员不能访问protected中x的值,故语句6无法运行。
cout << " Base :: x =" << bb.x << endl; //语句6
但是,get函数可以通过函数调用访问protected中x的值,运行结果如下:
3、在原程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误,为什么?
# include < iostream >
using namespace std;
class Base
{
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
public:
int x;
};
class Derived : private Base
{
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << " Base :: x =" << x << endl; // 语句1
}
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); // 语句5
cout << " Base :: x =" << bb.x << endl; //语句6
cout << " Derived :: y =" << bb.y << endl;//语句7
cout << " Base :: x =" << bb.getx() << endl; //语句8
cout << " Derived :: y =" << bb.gety() << endl; //语句9
return 0;
}
运行结果:
bb.setx(16);
第38行:错误,基类中的setx成员函数在派生类中为私有成员,不能被直接访问
第41行:错误,基类中的共有成员x在派生类中为私有成员,不能被直接访问
第43行:错误,基类中的getx成员函数在派生类中为私有成员,不能被直接访问
4、在原程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误,为什么?
# include < iostream >
using namespace std;
class Base
{
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
public:
int x;
};
class Derived : protected Base
{
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << " Base :: x =" << x << endl; // 语句1
}
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); // 语句5
cout << " Base :: x =" << bb.x << endl; //语句6
cout << " Derived :: y =" << bb.y << endl;//语句7
cout << " Base :: x =" << bb.getx() << endl; //语句8
cout << " Derived :: y =" << bb.gety() << endl; //语句9
return 0;
}
结果如下:
第38行:错误,基类中的setx成员函数在派生类中为保护成员,不能被直接访问
第41行:错误,基类中的共有成员x在派生类中为保护成员,不能被直接访问
第43行:错误,基类中的getx成员函数在派生类中为保护成员,不能被直接访问
派生类对基类成员的访问规则:
基类的成员可以有public、protected、private3中访问属性,基类的成员函数可以访问基类中其他成员,但是在类外通过基类的对象,就只能访问该基类的公有成员。同样,派生类的成员也可以有public、protected、private3种访问属性,派生类的成员函数可以访问派生类中自己增加的成员,但是在派生类外通过派生类的对象,就只能访问该派生类的公有成员。
派生类对基类成员的访问形式主要有以下两种:
内部访问:由派生类中新增的成员函数对基类继承来的成员的访问。
对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问。