问题及代码
/*
*Copyright(c)2014,烟台大学计算学院
*All rights reserved.
*文件名称:test.cpp
*作者:陈银丽
*完成日期:2015年7月4日
*版本号:v1.0
*
*问题描述:
*输入描述:
存在错误的程序行列class
Flower:private Plant
{
private:
int Florescence; //花期
public:
Flower(int id,string name,int Florescence):Plant(id,name),Florescence(Florescence) {}
void show()
{
/*****修改的起始行******/
cout<<id<<" "<<name;
cout<<" "<<Florescence<<endl;
/*****修改的终止行*****/
}
};
*程序输出:
*/
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class Plant
{
private:
int id;
string name;
public:
Plant(int id,string name):id(id),name(name) {}
void show()
{
cout<<id<<" "<<name;
}
};
class Flower:private Plant
{
private:
int Florescence; //花期
public:
Flower(int id,string name,int Florescence):Plant(id,name),Florescence(Florescence) {}
void show()
{
/*****修改的起始行******/
Plant::show();
cout<<" "<<Florescence<<endl;
/*****修改的终止行*****/
}
};
int main()
{
int id,Florescence;
string name;
cin>>id>>name>>Florescence;
Flower f1(id,name,Florescence);
f1.show();
return 0;
}
运行结果:
知识点总结:考点是继承与派生。这道题是私有继承。私有基类的私有成员在派生类中成为不可访问的成员,只有基类的成员函数可以引用他们。所以,在这道题中的id变成了不可访问的,
学习心得:关于继承与派生的概念理论也要熟悉。