C++ 命名空间、引用、指针、容器、强转、类、友元函数、友元类、单例、重载操作符、继承、多态、虚函数、模板(泛型)

本文主要是介绍C++ 命名空间、引用、指针、容器、强转、类、友元函数、友元类、单例、重载操作符、继承、多态、虚函数、模板(泛型),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

命名空间
类似包名 区分重名

using namespace std;//命名空间
namespace first_space {void fun() {cout << "first_space" << endl;}
}
namespace second_space {void fun() {cout << "second_space" << endl;}
}int main(int argc, const char * argv[]) {//命名空间first_space::fun();second_space::fun();return 0;
}//first_space
//second_space

引用 和 指针
在这里插入图片描述

引用直接用& 即可
int& r = xxxx;

//引用int iforYinYong = 10;double dforYinYong = 9.0;int* pYinYong = &iforYinYong;  //指针int& r = iforYinYong;	//引用cout << "Value of iForYinYong reference: " << r << endl;

容器

#include <vector>
#include <set>
#include <map>int main(int argc, const char * argv[]) {//容器  stl: 标准模板库//序列式 关联式//序列式容器:元素排列顺序 与 元素本身 无关,由添加顺序决定。 stack//vector list dequeue queue stack priority queuevector<int> vec1;//声明 一个元素空间vector<int> vec2(1);//6个元素 值都是1vector<int> vec3(6, 1);vector<int> vec4(vec3);//添加元素vec1.push_back(10);//通过下标 获取元素cout << "通过下标获取元素:" << vec1[0] << endl;//获取对头 队尾的元素vec1.front();vec1.back();vec1.clear();//清空vec1.erase(vec1.begin(), vec1.end());//区间cout << "容器大小: " << vec1.capacity() << endl; //容器大小//关联式 set map hashmap//set 集合  元素不可重复set<int> set1 = {1, 2, 3, 4};set1.insert(1); //已存在 不会加进去pair<set<int>::iterator, bool> _pair = set1.insert(6); //返回值 添加了什么,是否成功std::cout << "set集合里元素个数有:" << set1.size() << endl;set<int>::iterator itt = set1.begin();set1.end();//最后一个元素的 下一个元素for (; itt != set1.end(); itt++) {cout << *itt << endl;}//mapmap<int, string> map1;map<int, string> map2 = {{1, "a"}, {2, "b"}};map2.insert({3, "c"});//修改map2[1] = "d"; //1是keymap<int, string>::iterator ittm = map2.begin();for (; ittm != map2.end(); ittm++) {cout << ittm->first << ":" << ittm->second << endl;}
}

c的强转
在这里插入图片描述

C++ 分四种转换
在这里插入图片描述

在这里插入图片描述

class Parent {
public:void test() {cout << "p" << endl;}
};
class Child: public Parent {
public:void test() {cout << "c" << endl;}
};//强转//const_cast  相互转化const char *a7; //相当于java的finalchar *b7 = const_cast<char*>(a7);char *a8;const char *b8 = const_cast<const char*>(a8);//static_cast  基本类型转换  父子转换   编译时Parent *parent = new Parent;Child *c = static_cast<Child*>(parent);c->test();//dynamic_cast  运行时


通过有缘函数或者有缘类 可以访问修改私有成员

头文件 Student.h

#ifndef Student_h
#define Student_h
class Student {//友元函数friend void test(Student*);//友元类friend class Teacher;int i;public:Student(int i, int j);~Student();//析构函数void setJ(int j);void setK(int j) const;int getB() {return b;}int getA() {return a;}private:int a;private:int b;protected:int c;};class Teacher {public:void call(Student* s) {s->a = 1000;};};#endif /* Studentr_h */

Student.cpp

#include "Student.hpp"
#include "Student.h"
#include <iostream>
using namespace std;//相当于安卓里的 onCreate
Student::Student(int i, int j):i(i) {//:i(i) 省去写this 直接给成员变量赋值
//    this->i = i;cout << "构造方法" << endl;
}//可以通过方法给成员变量赋值
void Student::setJ(int j) {this->a = j;
}
//常量函数
//表示不会 也不允许 修改类中的成员
void Student::setK(int j) const{
//    this->a = j;
}//相当于安卓里的 onDestroy
Student::~Student() {cout << "析构方法" << endl;
}

main函数

#include <iostream>
#include "Student.h"void test(Student* stu) {stu->b = 100;
}int main(int argc, const char * argv[]) {//构造Student student(10, 20);test(&student);std::cout << student.getB() << std::endl;Teacher teacher;teacher.call(&student);std::cout << student.getA() << std::endl;return 0;
}

单例
Single.h

class Single{
private:static Single* instance;Single();public:static Single* getInstance();
};

Single.cpp

#include "Single.hpp"
#include "Single.h"
Single* Single::instance = 0;Single* Single::getInstance() {if (!instance) {instance = new Single();}return instance;
};

main

#include <iostream>
#include "Single.h"int main(int argc, const char * argv[]) {Single* single = Single::getInstance();std::cout << single <<  std::endl;return 0;
};

操作符
Test.h

#ifndef Test_h
#define Test_h
class Test {
public:int i;Test operator + (const Test& t) {Test temp;temp.i = this->i + t.i;return temp;};
};#endif /* Test_h */

main

#include <iostream>
#include "Test.h"
int main(int argc, const char * argv[]) {Test test1;test1.i = 100;Test test2;test2.i = 200;Test test3 = test1 + test2;std::cout << test3.i << std::endl;return 0;
}

继承
多态:父类引用指向子类对象
静态多态,调用的是父类方法。
动态多态,调用子类方法。
Extend.h

#ifndef Extend_h
#define Extend_h
#include <iostream>
using namespace std;
class Parent1 {
public://动态多态virtual void eatting() {cout << "parent1" << endl;}//纯虚函数 类似抽象方法virtual void abstractMethod() = 0;
};class Parent2 {
public:void eatting() {}
};class Child : public Parent1, Parent2 {
public:void eatting() {//super.eatting()Parent1::eatting();cout << "child" << endl;}//子类实现 纯虚函数  也就是抽象方法void abstractMethod() override {cout << "子类重写了父类的 抽象方法" << endl;};
};#endif /* Extend_h */

main

#include <iostream>
#include "Extend.h"
int main(int argc, const char * argv[]) {Child child;child.eatting();//静态多态Parent1* child2 = new Child();child2->eatting(); // 因为是静态 在编译时期,就认为是parent1的eatting方法,没等创建child呢。//动态多态 需要把 Parent1类的方法前 加关键字 virtual//将其声明为虚函数//注意事项:1、构造方法永远不要设置为虚函数  如果父类是 虚函数构造,子类就没办法创建了//       2、析构方法 声明为虚函数  好让真正的子类去释放内存return 0;
}

模板(泛型)

#include <iostream>
//泛型基础 模板编程//函数模板  java的泛型方法/**T method(T t) {}*/template <typename T>T methodA(T t1, T t2) {return t1 > t2 ? t1: t2;}//类模板  java的类泛型template <class T, class E>
class Q {
public:T test(T t, E e) {return t + e;}
};int main(int argc, const char * argv[]) {//方法模板int result = methodA(1, 2);std::cout << result << std::endl;//类模板Q<int, float> q;std::cout << q.test(1, 2.0) << std::endl;return 0;
}

这篇关于C++ 命名空间、引用、指针、容器、强转、类、友元函数、友元类、单例、重载操作符、继承、多态、虚函数、模板(泛型)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/921171

相关文章

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

MySQL字符串常用函数详解

《MySQL字符串常用函数详解》本文给大家介绍MySQL字符串常用函数,本文结合实例代码给大家介绍的非常详细,对大家学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql字符串常用函数一、获取二、大小写转换三、拼接四、截取五、比较、反转、替换六、去空白、填充MySQL字符串常用函数一、

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域