C++笔记 第四十五课 不同的继承方式---狄泰学院
如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux
第四十五课 不同的继承方式
1.被忽略的细节
冒号(:)表示继承关系,parent表示被继承的类,public的意义是什么?
2.有趣的问题
是否可以将继承语句中的public换成protected或者private?如果可以,与public继承有什么区别?
45-1 有趣的尝试—可以运行
#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;
}
3.不同的继承方式
C++中支持三种不同的继承方式
public继承
父类成员在子类中保持原有访问级别
private继承
父类成员在子类中变为私有成员
protected继承
父类中的公有成员变为保护成员,其他成员保持不变
45-2 继承与访问级别深度实践(**)
#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 protection inherits from Parent, so all Parent members become protected members, so the outside world cannot access
//Child_B保护继承自Parent,所以所有的Parent成员全部变成了protected成员,因此外界无法访问
c.m_c = 100;//Child_C protection inherits from Parent, so all Parent members become private members, so the outside world cannot access */
//Child_C保护继承自Parent,所以所有的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;
}
4.遗憾的事实
一般而言,C++工程项目中只使用public继承
C++的派生语言只支持一种继承方式(public继承)
protected和private继承带来的复杂性远大于实用性
45-3.d 45-3.cs 45-3.java C++派生语言初探
45-3.d linux运行指令:gdc 45-3.d
module D_Demo;
import std.stdio;
import std.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)
}
运行结果
D Demo
Point
P(1, 2)
45-3.cs linux运行: gmcs 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)
}
}
运行结果
C# Demo
Point
P(1, 2)
45-3.java linux运行:javac 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)
}
}
运行结果
Java Demo
Point
P(1, 2)
小结
C++中支持3种不同的继承方式
继承方式直接影响父类成员在子类中的访问属性
一般而言,工程中只使用public的继承方式
C++的派生语言中只支持public继承方式