
【C++】面向对象语法知识点
AI-摘要
Tianli GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
面向对象语法知识点
1. 重载
1.1 重载概述
重载可以解决诸多实际问题
- 增强代码可读性和可维护性
- 提高代码复用性
语法特征
- 要求在同一个区域内,包括,同一个文件,同一个结构体,同一个类,同一个命名空间
- 要求函数名称必须一致
- 要求函数的形式参数列表不一致。
1.2 重载案例
#include <iostream>
using namespace std;
/*
以下四个函数采用重载机制实现
1. 在同一个文件中
2. 函数名完全一致
3. 函数的形式参数列表不一致
实际调用过程中,编译器会根据当前函数名称和提供的实际参数
数据类型来选择执行目标函数。
*/
void test();
void test(int n);
void test(float n);
void test(string str);
/*
无法重载仅按返回类型区分的函数C/C++(311)
【注意】
返回值类型不作为重载依据和区分条件
*/
// int test(string str);
/*
【错误】
重载操作,参数名称不作为区分条件,关注的是参数的
数据类型。
*/
// void test(string n);
int main(int argc, char const *argv[])
{
test();
test(10);
test(3.5F);
test("233333");
return 0;
}
void test()
{
cout << "执行 无参 参数函数" << endl;
}
void test(int n)
{
cout << "执行 int 参数函数" << endl;
}
void test(float n)
{
cout << "执行 float 参数函数" << endl;
}
void test(string str)
{
cout << "执行 string 参数函数" << endl;
}
void test(string n)
{
cout << "执行 string 参数函数" << endl;
}
2. 权限修饰符
2.1 概述
限制当前类内成员变量,成员函数,外部的使用权限。涉及到三个权限修饰符
- public :公开
- protected :保护
- private :私有
使用权限修饰符之后,修饰内容对外的权限效果
解释:
- 类内:修饰内容是否可以在类内其他成员函数,构造函数中使用
- 类外:修饰内容是否可以通过实例化对象,在类外部进行使用
- 子类:修饰内容是否可以通过继承方式,延续到子类内容中·
权限修饰符 | 类内 | 类外 | 子类 |
---|---|---|---|
public | √ | √ | √ |
protected | √ | × | √ |
private | √ | × | × |
2.2 基本语法案例
#include <iostream>
using namespace std;
class Type
{
public:
int public_variable;
void public_function() { cout << "public 修饰函数" << endl; }
/*
public, protected, private 修饰的内容,都可以直接在类内使用
*/
void test()
{
public_variable = 10;
public_function();
protected_variable = 20;
protected_function();
private_variable = 30;
private_function();
}
protected:
int protected_variable;
void protected_function() { cout << "protected 修饰函数" << endl; }
private:
int private_variable;
void private_function() { cout << "private 修饰函数" << endl; }
};
int main(int argc, const char *argv[])
{
Type t1;
t1.public_function();
t1.public_variable = 200;
/*
private 和 protected 修饰内容,类外无法通过实例化对象或者其他手段
调用使用。
t1.private_function();
t1.private_variable = 200;
t1.protected_function();
t1.protected_variable = 200;
*/
return 0;
}
3. 继承【重点】
3.1 继承有什么用
游戏开发中,通常会设定一个基础的模版,在基本的模版之上进行特征化调整,从而降低开发压力。涉及到两个知识点【封装】和【继承】。
在开发中继承的目标
- 数据类型的延续性和一致性
- 函数的延续性和特征化。
3.2 继承基本语法
格式要求
class Type_A : 权限修饰符 Super_Type
{
};
继承之后,子类可以延续父类的相关成员内容,但是需要考虑到继承采用的权限修饰符,和原父类中成员权限修饰符。
#include <iostream>
using namespace std;
class Super_Type
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};
/*
当前 Sub_Type 通过 public 修饰公开继承 Super_Type
Sub_Type 是 Super_Type 的子类
Super_Type 是 Sub_Type 的父类
*/
class Sub_Type : public Super_Type
{
public:
/*
当前子类类内函数,使用从父类继承而来的成员变量内容,因为
权限权限修饰符问题
发现
public_var protected_var 可以使用
private_var 无法使用
【注意】
子类无法通过继承手段,得到父类中的私有化修饰成员内容。
*/
void test()
{
public_var = 10;
protected_var = 200;
// private_var = 500;
}
};
int main(int argc, char const *argv[])
{
Sub_Type st;
/*
目前实例化子类对象,使用的相关成员变量都是通过继承从父类获取
到的成员变量内容,但是因为权限修饰问题,部分成员变量类外无法使用
发现
protected_var
private_var
类外无法使用
*/
st.public_var = 10;
// st.protected_var = 10;
// st.private_var = 10;
return 0;
}
3.3 不同权限继承效果【重点】
public 继承
#include <iostream>
using namespace std;
class Super_Type
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};
class Sub_Type : public Super_Type
{
/*
public 继承父类中的内容
父类中的成员内容继承到子类中的权限情况
父类中 public 继承到子类还是 public
父类中 protected 继承到子类还是 protected
父类中 private 无法被子类继承。
目前子类中的成员情况可以认为是
{
public:
int public_var;
protected:
int protected_var;
}
*/
public:
void test()
{
public_var = 10;
protected_var = 200;
// private_var = 500;
}
};
int main(int argc, char const *argv[])
{
Sub_Type st;
st.public_var = 10;
// st.protected_var = 10;
// st.private_var = 10;
return 0;
}
protected 继承
#include <iostream>
using namespace std;
class Super_Type
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};
class Sub_Type : protected Super_Type
{
/*
protected 继承父类中的内容
父类中的成员内容继承到子类中的权限情况
父类中 public 继承到子类还是 protected
父类中 protected 继承到子类还是 protected
父类中 private 无法被子类继承。
目前子类中的成员情况可以认为是
{
protected:
int public_var;
int protected_var;
}
*/
public:
void test()
{
public_var = 10;
protected_var = 20;
// private_var = 3000;
}
};
class Sub_Sub_Type : protected Sub_Type
{
/*
因为 protected 修饰的成员内容,可以通过继承
延续到子类中,当前类可以证明在 Sub_Type 中
public_var 和 protected_var 都是 protected 修饰
*/
public:
void test()
{
public_var = 10;
protected_var = 20;
// private_var = 3000;
}
};
int main(int argc, char const *argv[])
{
Sub_Type st;
/*
因为当前 protected 继承之后,父类中public 和 protected
修饰内容在子类中的权限为 proteced 修饰,不可以在类外使用。
*/
st.public_var = 100;
st.protected_var = 10;
st.private_var = 10;
return 0;
}
private 继承
#include <iostream>
using namespace std;
class Super_Type
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};
class Sub_Type : private Super_Type
{
/*
private 继承父类中的内容
父类中的成员内容继承到子类中的权限情况
父类中 public 继承到子类还是 private
父类中 protected 继承到子类还是 private
父类中 private 无法被子类继承。
目前子类中的成员情况可以认为是
{
private:
int public_var;
int protected_var;
}
*/
public:
void test()
{
public_var = 10;
protected_var = 20;
// private_var = 3000;
}
};
class Sub_Sub_Type : public Sub_Type
{
/*
因为 private 修饰成员内容,无法延续到子类中
可以通过 Sub_Sub_Type 证明 Sub_Type 中
public_var protected_var 都是 private 权限修饰
*/
public:
void test()
{
public_var = 10;
protected_var = 20;
// private_var = 3000;
}
};
int main(int argc, char const *argv[])
{
Sub_Type st;
/*
因为当前 private 继承之后,父类中public 和 protected
修饰内容在子类中的权限为 private 修饰,不可以在类外使用。
*/
st.public_var = 100;
st.protected_var = 10;
st.private_var = 10;
return 0;
}
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载必须注明来自 卡卡罗特
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果