大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
**作用: **给变量起别名
语法: 数据类型 &别名 = 原名
引用是一种特殊的指针类型,引用一旦被定义就不能重新赋值,并且不能被设置为空值。
int main() {
int a = 10;
int &b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
结果:
a = 10
b = 10
a = 100
b = 100
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参
#include <iostream>
using namespace std;
//1. 值传递
void mySwap01(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//2. 地址传递
void mySwap02(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
//3. 引用传递
void mySwap03(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10;
int b = 20;
//值传递
mySwap01(a, b);
cout << "a:" << a << " b:" << b << endl;
//地址传递
mySwap02(&a, &b);
cout << "a:" << a << " b:" << b << endl;
//引用传递
mySwap03(a, b);
cout << "a:" << a << " b:" << b << endl;
system("pause");
}
结果:
a:10 b:20
a:20 b:10
a:20 b:10
实参通常是通过值传递给函数的,这意味着形参接收的只是发送给它们的值的副本,它们存储在函数的本地内存中。对形参值进行的任何更改都不会影响原始实参的值。
如果不想修改实参,就用值传递,如果想修改实参,就用地址传递或者引用传递
作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值
#include <iostream>
using namespace std;
//返回局部变量引用
int& test01() {
int a = 10; //局部变量
return a;
}
//返回静态变量引用
int& test02() {
static int a = 20;
return a;
}
int main(){
//不能返回局部变量的引用
int& ref = test01();
cout << ref << endl; //结果:10
cout << ref << endl;//结果:2037160328
int& ref2 = test02();
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
//如果函数做左值,那么必须返回引用
test02() = 1000;
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
return 0;
}
int& ref2 = test01();
这里ref1的引用时 test01方法,则应用的时改方法返回值内容。而在局部变量存放在四区的栈区中。在方法结束后就进行了释放。第一次钓鱼执行结果时正确的结果,时因为我编译器做了保留。而在一二次调用的时候内存进行了释放。本质:引用的本质在c++内部实现是一个指针常量.
作用:常量引用主要用来修饰形参,防止误操作
void showValue(const int& v) {
cout << v << endl;
}
int main(){
const int& ref = 10;
cout << ref << endl;
int a = 10;
showValue(a);
showValue(ref);
}
在C++中,函数的形参列表中的形参是可以有默认值的。
语法: 返回值类型 函数名 (参数= 默认值){}
int func(int a = 10, int b = 20) {
return a + b;
}
int main(){
int a = func();
cout << a << endl;
}
C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法: 返回值类型 函数名 (数据类型){}
void func(int a, int) {
cout << "this is func" << endl;
}
int main() {
func(10,10); //占位参数必须填补
system("pause");
return 0;
}
作用:函数名可以相同,提高复用性
函数重载满足条件:
//函数重载需要函数都在同一个作用域下
void func()
{
cout << "func 的调用!" << endl;
}
void func(int a)
{
cout << "func (int a) 的调用!" << endl;
}
void func(double a)
{
cout << "func (double a)的调用!" << endl;
}
void func(int a ,double b)
{
cout << "func (int a ,double b) 的调用!" << endl;
}
void func(double a ,int b)
{
cout << "func (double a ,int b)的调用!" << endl;
}
//函数返回值不可以作为函数重载条件
//int func(double a, int b)
//{
// cout << "func (double a ,int b)的调用!" << endl;
//}
int main() {
func();
func(10);
func(3.14);
func(10,3.14);
func(3.14 , 10);
system("pause");
return 0;
}
void func(int& a)
{
cout << "func (int &a) 调用 " << endl;
}
void func(const int& a)
{
cout << "func (const int &a) 调用 " << endl;
}
int main(){
int a = 10;
func(a); //调用无const
func(10);//调用有const
}
func(a);
为什么调用的是没有const的方法。原因是因为a是一个变量。而func(10);
,10是常量。如果加载时上面的void func(int& a)
方法,是不合法的。引用必须有一个合法的内存空间,而这里10是在常量区里面。语法: class 类名{ 访问权限: 属性 / 行为 };
#include <iostream>
using namespace std;
const double PI = 3.14;
class Circle
{
public:
int m_r;//半径
double calculateZC()
{
//2 * pi * r
//获取圆的周长
return 2 * PI * m_r;
}
};
int main() {
Circle cl;
double bl = cl.calculateZC();
cout << bl << endl;
return 0;
}
在C++中 struct和class唯一的区别就在于 默认的访问权限不同
区别:
优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性
class Person {
public:
//姓名设置可读可写
void setName(string name) {
m_Name = name;
}
string getName()
{
return m_Name;
}
//获取年龄
int getAge() {
return m_Age;
}
//设置年龄
void setAge(int age) {
if (age < 0 || age > 150) {
cout << "你个老妖精!" << endl;
return;
}
m_Age = age;
}
//情人设置为只写
void setLover(string lover) {
m_Lover = lover;
}
private:
string m_Name; //可读可写 姓名
int m_Age; //只读 年龄
string m_Lover; //只写 情人
};
int main() {
Person p;
//姓名设置
p.setName("张三");
cout << "姓名: " << p.getName() << endl;
//年龄设置
p.setAge(50);
cout << "年龄: " << p.getAge() << endl;
//情人设置
p.setLover("苍井");
//cout << "情人: " << p.m_Lover << endl; //只写属性,不可以读取
system("pause");
return 0;
}
c++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。
对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供
编译器提供的构造函数和析构函数是空实现。
构造函数语法:类名(){}
析构函数语法: ~类名(){}
class Person
{
public:
//构造函数
Person()
{
cout << "Person的构造函数调用" << endl;
}
//析构函数
~Person()
{
cout << "Person的析构函数调用" << endl;
}
};
void test01()
{
Person p;
}
int main() {
test01();
system("pause");
return 0;
}
相同点:与类名相同,没有返回值,如果用户不定义,系统也会自动生成一个空的析构函数。而一旦用户定义,则对象在销毁时自动调用。
不同点:虽然他俩都为公开类型。构造可以重载,有多个兄弟,而析构却不能重载,但它可以是虚函数,一个类只能有一个析构函数。
两种分类方式:
按参数分为: 有参构造和无参构造
按类型分为: 普通构造和拷贝构造
三种调用方式:
括号法
显示法
隐式转换法
class Person {
public:
//无参(默认)构造函数
Person() {
cout << "无参构造函数!" << endl;
}
//有参构造函数
Person(int a) {
age = a;
cout << "有参构造函数!" << endl;
}
//拷贝构造函数
Person(const Person& p) {
age = p.age;
cout << "拷贝构造函数!" << endl;
}
//析构函数
~Person() {
cout << "析构函数!" << endl;
}
public:
int age;
};
//2、构造函数的调用
//调用无参构造函数
void test01() {
Person p; //调用无参构造函数
}
//调用有参的构造函数
void test02() {
//2.1 括号法,常用
Person p1(10);
//注意1:调用无参构造函数不能加括号,如果加了编译器认为这是一个函数声明
//Person p2();
//2.2 显式法
Person p2 = Person(10);
Person p3 = Person(p2);
//Person(10)单独写就是匿名对象 当前行结束之后,马上析构
//2.3 隐式转换法
Person p4 = 10; // Person p4 = Person(10);
Person p5 = p4; // Person p5 = Person(p4);
//注意2:不能利用 拷贝构造函数 初始化匿名对象 编译器认为是对象声明
//Person p5(p4);
}
int main() {
test01();
return 0;
}
C++中拷贝构造函数调用时机通常有三种情况
class Person {
public:
Person() {
cout << "无参构造函数!" << endl;
mAge = 0;
}
Person(int age) {
cout << "有参构造函数!" << endl;
mAge = age;
}
Person(const Person& p) {
cout << "拷贝构造函数!" << endl;
mAge = p.mAge;
}
//析构函数在释放内存之前调用
~Person() {
cout << "析构函数!" << endl;
}
public:
int mAge;
};
//1. 使用一个已经创建完毕的对象来初始化一个新对象
void test01() {
Person man(100); //p对象已经创建完毕
Person newman(man); //调用拷贝构造函数
Person newman2 = man; //拷贝构造
//Person newman3;
//newman3 = man; //不是调用拷贝构造函数,赋值操作
}
//2. 值传递的方式给函数参数传值
//相当于Person p1 = p;
void doWork(Person p1) {}
void test02() {
Person p; //无参构造函数
doWork(p);
}
//3. 以值方式返回局部对象
Person doWork2()
{
Person p1;
cout << (int *)&p1 << endl;
return p1;
}
void test03()
{
Person p = doWork2();
cout << (int *)&p << endl;
}
int main() {
//test01();
//test02();
test03();
system("pause");
return 0;
}
class Person {
public:
//初始化列表方式初始化
Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {}
void PrintPerson() {
cout << "mA:" << m_A << endl;
cout << "mB:" << m_B << endl;
cout << "mC:" << m_C << endl;
}
private:
int m_A;
int m_B;
int m_C;
};
int main() {
Person p(1, 2, 3);
p.PrintPerson();
system("pause");
return 0;
}
静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员
class Person
{
public:
static int m_A;
private:
static int m_B; //静态成员变量也是有访问权限的
};
静态成员函数
class Person
{
public:
static void func()
{
cout << "func调用" << endl;
m_A = 100;
}
this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
class Person
{
public:
int age;
Person(int age) {
this->age = age;
}
Person& PersonAddperson(Person person) {
this->age += person.age;
return *this;
}
};
void test() {
Person p1(10);
cout << "p1.age = " << p1.age << endl;
Person p2(10);
p2.PersonAddperson(p1).PersonAddperson(p1).PersonAddperson(p1);
}
int main() {
test();
system("pause");
return 0;
}
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性。
class Person
{
public:
int age;
void ShowClassName() {
cout << "我是Person类!" << endl;
}
void ShowPerson() { //但是如果成员函数中用到了this指针,就不可以了
if (this == NULL) {
return;
}
cout << mAge << endl;
}
public:
int mAge;
};
void test01()
{
Person* p = NULL;
p->ShowClassName(); //空指针,可以调用成员函数
//p->ShowPerson(); //错误,但是如果成员函数中用到了this指针,就不可以了
}
int main() {
test01();
system("pause");
return 0;
}
p->ShowPerson();
失败,其原因是因为ShowPerson()
方法中使用了mAge
而这个变量为this->mAge
,该位置是空指针,this为空。所以这里报错了。常函数:
void showPerson() const{
}
常对象:
const Person p;
p.a = 100;//报错
友元的目的就是让一个函数或者类 访问另一个类中私有成员
友元的三种实现
class Building
{
friend void goodGay(Building* building);//告诉编译器 goodGay全局函数 ,可以访问类中的私有内容
public:
Building() {
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom; //卧室
};
void goodGay(Building* building)
{
cout << "好基友正在访问: " << building->m_SittingRoom << endl;
cout << "好基友正在访问: " << building->m_BedRoom << endl;
}
void test01()
{
Building b;
goodGay(&b);
}
int main() {
test01();
system("pause");
return 0;
}
实际上就是在类的头部声明一个friend修饰的函数,也就是编写一个全局函数。然后使用该类去进行访问类中的private修饰的变量等。
#include <iostream>
using namespace std;
class Building;
class goodGay
{
public:
goodGay();
void visit();
private:
Building *building;
};
class Building
{
friend class goodGay;
public:
Building();
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom;//卧室
};
Building::Building() //类外实现
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit() {
cout << "好基友正在访问" << building->m_SittingRoom << endl;
cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void test01()
{
goodGay gg;
gg.visit();
}
int main() {
system("pause");
return 0;
}
这里看起来比较绕,当时这里逻辑捋清楚就好了。首先是创建一个goodGay实例,创建实例的构造方法中会去new一个Building。Building类构造方法则对m_SittingRoom
,m_BedRoom
进行赋值。
下面visit方法则打印两个变量。这里之所以可以访问类的私有变量是因为friend class goodGay;
这行代码。声明了该类有权限进行访问。
class Building
{
//告诉编译器 goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
friend void goodGay::visit();
}
与上同理。
继承的语法:class 子类 : 继承方式 父类
继承方式一共有三种:
继承中 先调用父类构造函数,再调用子类构造函数,析构顺序与构造相反
int main()
{
Son s;
cout << "Son下的m_A = " << s.m_A << endl;
cout << "Base下的m_A = " << s.Base::m_A << endl;
s.func();
s.Base::func();
s.Base::func(10);
}
总结:
C++允许一个类继承多个类
语法: class 子类 :继承方式 父类1 , 继承方式 父类2...
多继承可能会引发父类中有同名成员出现,需要加作用域区分
多态满足条件:
1、有继承关系 2、子类重写父类中的虚函数 多态使用: 父类指针或引用指向子类对象
class Animal
{
public:
virtual void speak() {
cout << "动物在说话" << endl;
}
private:
};
class Cat :public Animal
{
public:
void speak()
{
cout << "小猫在说话" << endl;
}
};
class Dog :public Animal
{
public:
void speak()
{
cout << "小狗在说话" << endl;
}
};
void DoSpeak(Animal &an)
{
an.speak();
}
int main() {
Cat cat;
DoSpeak(cat);
}
这里是编写了一个方法传递引用Animal,并且调用speak
在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容。
语法:virtual 返回值类型 函数名 (参数列表)= 0 ;
当类中有了纯虚函数,这个类也称为抽象类。
抽象类特点:
class Base
{
public:
virtual void func() = 0;
};
class Son :public Base
{
void func() {
cout << "Son func调用" << endl;
}
};
void dofunc(Base &base) {
base.func();
}
int main() {
Base * base = NULL;
base = new Son; //base = new Base; // 错误,抽象类无法实例化对象
base->func();
}
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
解决方式:将父类中的析构函数改为虚析构或者纯虚析构
虚析构和纯虚析构共性:
虚析构和纯虚析构区别:
虚析构语法:
virtual ~类名(){}
纯虚析构语法:
virtual ~类名() = 0;
类名::~类名(){}
写文件步骤如下:
打开方式 | 解释 |
---|---|
ios::in | 为读文件而打开文件 |
ios::out | 为写文件而打开文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 追加方式写文件 |
ios::trunc | 如果文件存在先删除,再创建 |
ios::binary | 二进制方式 |
int main() {
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:18" << endl;
ofs.close();
return 0;
}
总结:
读文件步骤如下:
#include <fstream>
void test() {
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
return;
}
char c;
while ((c = ifs.get()) != EOF)
{
cout << c;
}
ifs.close();
}
int main() {
test();
return 0;
}
总结:
二进制方式写文件主要利用流对象调用成员函数write
函数原型 :ostream& write(const char * buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
void test() {
ofstream ofs("person.txt", ios::out | ios::binary);
Person p = { "张三" , 18 };
ofs.write((const char*)&p, sizeof(p));
}
int main(){
test()
}
二进制方式读文件主要利用流对象调用成员函数read
函数原型:istream& read(char *buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test() {
ifstream ifs("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
}
Person p;
ifs.read((char*)&p, sizeof(p));
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
int main() {
test();
return 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/119859.html原文链接:https://javaforall.cn