一、函数
1、基础函数
int main()
{
int ret = 1;
count<"ret is "<<ret<<endl;
return 0;
}
2、分离式编程
函数被使用之前需要声明,类似于变量。
2.1 在本文件中:
static void func2(void); //将 func2的作用域拉至此
void func1(void)
{
func2();
}
void func2(void)
{
count << "func2 opt" << endl;
}
2.2 在多文件中:
//func2.h
#ifndef __FUNC_2_H__ //防止 .h 文件多层嵌套导致重复声明
#define __FUNC_2_H__
void func2(void);
#endif //__FUNC_2_H__
//func2.c
void func2(void)
{
count << "func2 opt" << endl;
}
//func1.c
#include "func2.h" //将func2的函数声明拉至此
void func1(void)
{
func2();
}
3 传入引用参数
static void test_change_value(int& i)
{
i = 999;
}
static void test_value(void)
{
int iValue = 2;
cout << "int value:" << iValue <<endl;
test_change_value(iValue);
cout<< "out value:" << iValue <<endl;
}
int value:2
out value:999
4 main:处理命令行选项
int main(int argc, char *argv[])
{
//int argc 参数个数
//char *argv[] 字符串数组
}
5 C11特性
static vector<string> test_return_value(void)
{
return {"str1", "str2", "str3"};//根据 vector<string> 形式写
}
static void test_return_in(void)
{
vector<string> ve_str = test_return_value();
//vector<string>::iterator it = ve_str.begin(); //标准写法
auto it = ve_str.begin();//使用auto
for(; it != ve_str.end(); ++it){
cout << *it <<endl;
}
}
6 不要返回局部对象的引用或指针
const string &mainip()
{
string ret;
if(!ret.empty())
return ret; //error:返回局部变量的引用
else
return "Empty"; //error:局部临时变量
}
7 重载函数
7.1 main 函数不能重载
7.2 不允许两个函数除了返回类型外其他的所有要素都相同。
7.3 重载和const
// 1
void func1(int)
void func1(const int) //重复声明
// 2
void func2(int&)
void func2(const int&) //新函数
顶层const不影响传入函数的对象。
8 重载和作用域
void func1(const string&);
void func1(double);
void func2(void)
{
void func1(int); //此时只能用【void func1(int);】其他两个被隐藏
func1(1);
func1("test");//error
}
9 内联函数和constexpr函数
9.1 内联函数一般用于优化规模较小、流程直接、调用频繁的函数。
inline int func1(int a, int b)
{
return a+b;
}
9.2 constexpr函数指能用于常量表达式,函数的返回类型及参数都是字面值类型,而且函数体中有且只有return语句。
constexpr int new_sz(){return 42;}
constexpr int foo = new_sz(); //类似内联函数
9.3 inline和constexpr 通常声明在头文件中。
二、类的基础知识
C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心特性,通常被称为用户定义的类型。
类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。
1、C++ 类定义
定义一个类,本质上是定义一个数据类型的蓝图。这实际上并没有定义任何数据,但它定义了类的名称意味着什么,也就是说,它定义了类的对象包括了什么,以及可以在这个对象上执行哪些操作。
2、定义 C++ 对象
类提供了对象的蓝图,所以基本上,对象是根据类来创建的。声明类的对象,就像声明基本类型的变量一样
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
3、访问数据成员
类的对象的公共数据成员可以使用直接成员访问运算符 . 来访问。
4、Public private protected区别
5、友元
//pc.h
#pragma once
#include <string>
#include "friend_service.h"
class Computer
{
// 使用全局函数作为友元函数,可以访问私有成员了
friend void out_upgrade(Computer* computer);
// 使用类的成员函数,作为友元函数
friend void ComputerService::upgrade(Computer* comptuer);
public:
Computer();
std::string description();
private:
std::string cpu; //CPU芯片
};
//pc.cpp
#include "friend_pc.h"
#include<sstream>
Computer::Computer()
{
this->cpu = "i7";
}
std::string Computer::description()
{
std::stringstream ret;
ret << "CPU:" << cpu;
return ret.str();
}
//service.h
#pragma once
class Computer;//要点:直接声明不用包含Computer头文件
class ComputerService
{
public:
void upgrade(Computer* computer);
};
void out_upgrade(Computer* computer);
//service.cpp
#include "friend_service.h"
#include "friend_pc.h"
void ComputerService::upgrade(Computer* computer) {
computer->cpu = "i9";//类的成员函数直接访问对象的私有数据成员!!!
}
void out_upgrade(Computer* computer) {
computer->cpu = "i8"; //函数直接访问对象的私有数据成员!!!
}
void main(void)
{
Computer shanxing;
ComputerService service;
std::cout << shanxing.description() << std::endl;
//友元函数访问Computer类
out_upgrade(&shanxing);
std::cout << shanxing.description() << std::endl;
//友元类成员函数访问Computer类
service.upgrade(&shanxing);
std::cout << shanxing.description() << std::endl;
}