继承方式与权限问题
1.继承的写法
2.继承(子类中没有产生新的属性或者行为)派生(子类中有新的属性和行为产生)
3.继承实质与权限问题
(1)实质:父类的数据和成员子类有一份
(2)权限问题:继承方式只会增强父类属性在子类中的权限显示(父类的私有属性不能直接访问)
4.私有继承再继承无意义
5.子类构造无参函数,父类必须要有无参的构造函数
#include <iostream>
#include <string>
using namespace std;
class parent//父类 基类
{
public:
void print()
{
cout << name << "\t" << num << endl;
}
protected:
int num;
string name;
private:
string name1;//可以被继承,但不可被直接访问,可以间接访问
};
//子类 派生
class son1 :public parent//公有继承
{
public:
void printS()
{
cout << name << "\t" << num << endl;
print();
}
protected:
};
class son2 :protected parent//保护继承
{
public:
protected:
};
class son3 :private parent//私有继承
{
public:
protected:
};
继承中的构造函数
1.父类的属性通过父类的构造函数初始化
2.子类中的构造函数,必须调用父类构造函数,必须采用初始化参数列表的方式
3.单继承:只有一个父类
4.多继承:有两个及以上的父类
5.无论被继承多少次,类的属性都在,所以继承一般不会有很多层,会导致臃肿
#include <iostream>
#include <string>
using namespace std;
//单继承
class parent
{
public:
parent(string fsurname, string fname) :fsurname(fsurname), fname(fname){}
protected:
string fsurname;//父姓
string fname;//父名
};
class son :public parent
{
public:
son(string fsurname, string fname, string sname) :parent(fsurname, fname), sname(sname)
{
this->ssurname = fsurname;
}
void print()
{
cout << "父类" << fsurname + fname << endl;
cout << "子类" << ssurname + sname << endl;
}
protected:
string ssurname;//子姓
string sname;//子名
//string fname;继承属性
//string sname;
};
//多继承
class parent1
{
public:
parent1(string name1) :name1(name1){}
protected:
string name1;
};
class parent2
{
public:
parent2(string name2) :name2(name2){}
protected:
string name2;
};
class son1 :public parent1, public parent2
{
public:
son1(string name1, string name2, string sname) :parent1(name1), parent2(name2), sname(sname){}
protected:
string sname;
};
int main()
{
son a("姓", "名1", "名2");
a.print();
son1 a1("1", "2", "3");
return 0;
}
继承中的同名问题
1.数据成员同名
2.成员函数同名
3.无特殊说明,采用就近原则
#include <iostream>
#include <string>
using namespace std;
class parent
{
public:
parent(string name) :name(name){}
void print()
{
cout << name << endl;
}
protected:
string name;
};
class son :public parent
{
public:
son(string name) :parent("父类"), name(name){}
void print()
{
cout << name << endl;
cout << parent::name <<"\t"<< name<<endl;//类名限定
parent::print();
}
protected:
string name;
};
int main()
{
son a("子类");
a.print();
parent A("父类");
A.print();
son* ps = new son("子类");
ps->print();//子类
parent* pp = new parent("父类");
pp->print();//父类
//非正常指针
parent* pp1 = new son("子类");//允许子类对象初始化父类指针
pp1->print();//父类
//在没有virtual情况下看指针类型 在有virtual情况下看赋值对象
//父类对象初始化子类指针,不安全不被允许
delete ps;
ps=nullptr;
delete pp;
pp=nullptr;
delete pp1;
pp1=nullptr;
return 0;
}
4.虚继承(菱形继承)
#include <iostream>
using namespace std;
class A
{
public:
A(int a) :a(a){}
protected:
int a;
};
class B:virtual public A
{
public:
B(int a, int b) :A(a), b(b){}
protected:
int b;
};
class C:virtual public A
{
public:
C(int a, int c) :A(a), c(c){}
void printc()
{
cout << a << endl;
}
protected:
int c;
};
class D :public B, public C
{
public:
D() :B(1, 2), C(2, 3), A(999){}
void print()
{
cout << a << endl;//999
cout << B::a << endl;//999
cout << C::a << endl;//999
C::printc();//999
}
protected:
};
int main()
{
D d;
d.print();
return 0;
}
构造顺序问题
注:与初始化参数列表无关
1.单继承中构造顺序问题(先构造父类,再构造子类)
2.多继承中构造顺序问题(与继承顺序一致)
include <iostream>
using namespace std;
class A
{
public:
A(){ cout << "A" ; }
~A(){ cout << "A" ; }
};
class B:public A
{
public:
B(){ cout << "B" ; }
~B(){ cout << "B" ; }
};
class C
{
public:
C(){ cout << "C" ; }
~C(){ cout << "C" ; }
};
class D
{
public:
D(){ cout << "D" ; }
~D(){ cout << "D" ; }
};
class F:public A,public D,public C
{
public:
F(){ cout << "F" ; }
~F(){ cout << "F" ; }
};
int main()
{
{
B b;
}
cout << endl;
F f;
return 0;
}