类的继承与派生
什么是类的继承与派生?
所谓继承就是从先辈那里得到其同样的属性和行为特征,类的继承也是这样,是新的类从已有的类那里得到的已有的属性和特征。从已有的类产生新的类的过程就是类的派生。其中所有的类叫基类或父类,新产生的类叫派生类或子类。
派生类的定义语法为:
class 派生类名:继承方式 基类名1,继承方式 基类名2 |
下面将分类介绍三种方式的不同之处。 ##一、公有继承
、、、
#include<iostream>
#include<string>
using namespace std;
class person
{
private:string name;
protected:int age;
public:void setvalue_p(string namel, int agel);
void print();
};
void person::setvalue_p(string namel, int agel)
{
name = namel;
age = agel;
}
void person::print()
{
cout << "基类print函数" << endl;
cout << "The person's name:" << name << endl;
cout << "The person's age:" << age << endl;
}
class student :public person
{
private:int id;
int score;
public:void setvalue(string name,int age,int id,int score);
void output();
void print();
};
void student::setvalue(string name, int age, int id, int score)
{ //想在子类中利用基类的私有成员就需要在基类中打开一个接口与外部相通:setvalue_p()函数
setvalue_p(name,age); //调用基类的公有函数
/*this->name = name; //此处不可这样写,子类不能访问基类的私有成员,可以用共有函数setvalue_p()代替
this->age = age;*/
this->id = id;
this->score = score;
}
void student::print()
{
cout << "子类print函数" << endl;
/*cout<< "The student's name:" << name << endl;*/ //子类不可访问基类的私有成员
cout << "The student's age:" << age << endl; //子类可以访问基类的保护成员
}
void student::output()
{
print(); //隐藏从父类继承的函数,直接调用自己本身的函数
cout << "id:" << id << endl;
cout << "score:"<<score<<endl;
}
int main()
{
person p1;
p1.setvalue_p("schb",13);
p1.print();
student s1;
s1.setvalue("fbf", 12, 123456, 56);
s1.print();
s1.output();
return 0;
}
、、、
1.在派生类内部可以访问基类的公有成员和保护成员,但不能访问基类的私有成员
2.在派生类外,只能通过对象访问基类的公有成员,保护成员和私有成员都不能被访问到。
3.如果在派生类中出现了与基类公有成员中相同的函数名,那么在派生类中调用函数时会使用派生类的函数。
二、私有继承
、、、
#include<iostream>
#include<string>
using namespace std;
class person
{
private:string name;
protected:int age;
public:void setvalue_p(string namel, int agel);
void print();
};
void person::setvalue_p(string namel, int agel)
{
name = namel;
age = agel;
}
void person::print()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
}
class student :private person
{
private:int id;
int score;
public:void setvalue(string name, int age, int id, int score);
void print();
};
void student::setvalue(string name, int age, int id, int score)
{
setvalue_p(name, age);
/*this->name = name; //同样不能用基类的私有成员
this->age = age;*/
this->id = id;
this->score = score;
}
void student::print()
{
/*cout<< "The student's name:" << name << endl; */ //基类私有成员不可访问
person::print(); //通过声明相同的函数,将基类的一部分外部接口保证在派生类中存在,内部调用基类的公有函数
cout << "id:" << id << endl;
cout << "score:" << score << endl;
}
int main()
{
student s1,s2;
s1.setvalue("fbf", 12, 123456, 56);
s1.print();
return 0;
}
、、、
1.在派生类内部,基类的公有成员和保护成员作为私有成员在派生类中,内部可以访问它们。但是基类的私有成员不能访问
2.在类族外部,即使通过派生类对象也无法访问基类的公有成员和保护成员,私有成员更是不能访问。否则就会出现如下错误
3.为保证基类的外部接口特征在派生类中也存在,就必须在派生类中重新声明同名的函数。
如果进一步派生新的类访问基类成员会怎么样呢
这说明经过私有继承后,所有基类的成员都成了派生类的私有成员或不可访问的成员,如果进一步派生,就不能访问基类的成员,基类成员就不能在后面的派生类中发挥作用,相当于中止了基类功能的派生,所以很少情况下用私有继承。
三、保护继承
、、、
#include<iostream>
#include<string>
using namespace std;
class person
{
private:string name;
protected:int age;
public:void setvalue_p(string namel, int agel);
void print_p();
};
void person::setvalue_p(string namel, int agel)
{
name = namel;
age = agel;
}
void person::print_p()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
}
class student :protected person
{
private:int id;
int score;
public:void setvalue(string name, int age, int id, int score);
void print();
};
void student::setvalue(string name, int age, int id, int score)
{
setvalue_p(name, age);
/*this->name = name; //同样不能用基类的私有成员
this->age = age;*/
this->id = id;
this->score = score;
}
void student::print()
{
print_p();//访问基类公有成员
/*cout<< "The student's name:" << name << endl; */ //基类私有成员不可访问
cout << "id:" << id << endl;
cout << "score:" << score << endl;
}
int main()
{
student s1, s2;
s1.setvalue("fbf", 12, 123456, 56);
s1.print();
//cout << s1.name; //对象不可以访问基类私有成员
//cout << s1.age; //对象不可以访问基类保护成员
//s1.print_p(); //对象不可以访问基类公有成员
return 0;
}
、、、