注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:9.2.0
一、被忽视的细节
冒号(:)表示继承关系,Parent表示被继承的类,public的意义是什么?
class Parent
{
};
class Child : public Parent
{
};
二、有趣的问题
是否可以将继承语句中的public换成protected或者private?如果可以,与public继承有什么区别?
编程实验
有趣的尝试
45-1.cpp
#include <iostream>
#include <string>
using namespace std;
class Parent
{
};
class Child_A : public Parent
{
};
class Child_B : protected Parent
{
};
class Child_C : private Parent
{
};
int main()
{
return 0;
}
操作:
1) g++ 45-1.cpp -o 45-1.out编译正确,打印结果:无
三、不同的继承方式
1)C++中支持三种不同的继承方式
- public继承
* 父类成员在子类中保持原有访问级别
- private继承
* 父类成员在子类中变为私有成员
- protected继承
* 父类中的公有成员变为保护成员,其它成员保持不变
编程实验
继承与访问级别深度实践
45-2.cpp
#include <iostream>
#include <string>
using namespace std;
class Parent
{
protected:
int m_a;
protected:
int m_b;
public:
int m_c;
void set(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
};
class Child_A : public Parent
{
public:
void print()
{
cout << "m_a" << m_a << endl;
cout << "m_b" << m_b << endl;
cout << "m_c" << m_c << endl;
}
};
class Child_B : protected Parent
{
public:
void print()
{
cout << "m_a" << m_a << endl;
cout << "m_b" << m_b << endl;
cout << "m_c" << m_c << endl;
}
};
class Child_C : private Parent
{
public:
void print()
{
cout << "m_a" << m_a << endl;
cout << "m_b" << m_b << endl;
cout << "m_c" << m_c << endl;
}
};
int main()
{
Child_A a;
Child_B b;
Child_C c;
a.m_c = 100;
// b.m_c = 100; // Child_B 保护继承自 Parent, 所以所有的 public 成员全部变成了 protected 成员, 因此外界无法访问
// c.m_c = 100; // Child_C 私有继承自 Parent, 所以所有的成员全部变成了 private 成员, 因此外界无法访问
a.set(1, 1, 1);
// b.set(2, 2, 2); //无法被直接调用
// c.set(3, 3, 3); //无法被直接调用
a.print();
b.print();
c.print();
return 0;
}
操作:
1) g++ 45-2.cpp -o 45-2.out编译正确,打印结果:
m_a1
m_b1
m_c1
m_a-1077311364
m_b-1219492691
m_c-1217948732
m_a-1216729088
m_b134515179
m_c-1217949696
四、遗憾的事实
1)一般而言,C++工程项目中只使用public继承
2)C++的派生语言只支持一种继承方式(public继承)
3)protected和private继承带来的复杂性远大于实用性
编程实验
C++派生类语言初探
45-3.d 45-3.cs 45-3.java
45-3.d
module D_Demo;
import std.stdio; //联想到stdio
import std.string; //联想到string
class Obj
{
protected:
string mName;
string mInfo;
public:
this()
{
mName = "Object";
mInfo = "";
}
string name()
{
return mName;
}
string info()
{
return mInfo;
}
}
class Point : Obj
{
private:
int mX;
int mY;
public:
this(int x, int y)
{
mX = x;
mY = y;
mName = "Point";
mInfo = format("P(%d, %d)", mX, mY);
}
int x()
{
return mX;
}
int y()
{
return mY;
}
}
void main(string[] args)
{
writefln("D Demo"); // D Demo
Point p = new Point(1, 2);
writefln(p.name()); // Point
writefln(p.info()); // P(1, 2)
}
45-3.cs
class Obj
{
protected string mName;
protected string mInfo;
public Obj()
{
mName = "Object";
mInfo = "";
}
public string name()
{
return mName;
}
public string info()
{
return mInfo;
}
}
class Point : Obj
{
private int mX;
private int mY;
public Point(int x, int y)
{
mX = x;
mY = y;
mName = "Point";
mInfo = "P(" + mX + ", " + mY + ")";
}
public int x()
{
return mX;
}
public int y()
{
return mY;
}
}
class Program
{
public static void Main(string[] args)
{
System.Console.WriteLine("C# Demo"); // C# Demo
Point p = new Point(1, 2);
System.Console.WriteLine(p.name()); // Point
System.Console.WriteLine(p.info()); // P(1, 2)
}
}
45-3.java
class Obj
{
protected String mName;
protected String mInfo;
public Obj()
{
mName = "Object";
mInfo = "";
}
public String name()
{
return mName;
}
public String info()
{
return mInfo;
}
}
class Point extends Obj
{
private int mX;
private int mY;
public Point(int x, int y)
{
mX = x;
mY = y;
mName = "Point";
mInfo = "P(" + mX + ", " + mY + ")";
}
public int x()
{
return mX;
}
public int y()
{
return mY;
}
}
class Program {
public static void main(String[] args){
System.out.println("Java Demo"); // Java Demo
Point p = new Point(1, 2);
System.out.println(p.name()); // Point
System.out.println(p.info()); // P(1, 2)
}
}
小结
1)C++中支持3种不同的继承方式
2)继承方式直接影响父类成员在子类中的访问属性
3)一般而言,工程中只使用public的继承方式
4)C++的派生语言中只支持public继承方式