基于C++的类编程总结

时间:2023-03-08 16:11:36

1. 类中public, protected, private这三个属性的区别: public意味着所有事物都能查询具有该属性的食物。(也即所有人可以在我不知情的情况下,查看我账户里还有多少钱)。

protected属性意味着只有我自己和我的子孙后代才能查我还有多少钱。private属性表明只有我自己才能查我自己账户里还有多少钱。

class father {
public:
int publicmoney;
protected:
int protectedmoney;
private:
int privatemoney;
};

2. 类继承过程中public, protected, private这三种继承方式的区别:

class child1: public father
{
\\ publicmoney is public;
\\ protectedmoney is protected.
\\ privatemoney is not accessible from child1.
} class child2: protected father
{
\\ publicmoney is protected.
\\ protectedmoney is protected.
\\ privatemoney is not accessible from child2.
} class child3: private father
{
\\ publicmoney is private.
\\ protectedmoney is private.
\\ privatemoney is not accessible from child3.
}