常对象、常对象引用、常对象指针:所表示的对象的属性是只读的,通过它们不能调用非常方法;
常方法更加通用,它既能被常对象(常对象引用、常对象指针)调用,也能被非常对象(非常对象引用、非常对象指针)调用。
如果常方法和非常方法构成重载,那么常对象(常对象引用、常对象指针)调用的将是常方法,而非常对象(非常对象引用、非常对象指针)调用的将是非常方法。
static 关键字和 const 关键字可以联合使用,用于修饰属性或方法,即静态常属性和静态常方法。
开发规范
类的声明和实现分离,即将类的声明代码写在头文件中,类的实现代码写在源文件中,并且文件名建议就是类名。
C++标准版本
C++ 98
C++ 03
C++ 0X
C++ 11:初始化列表,自动类型推导(auto/decltype)等;
C++ 14
C++ 17
C++ 2a
编译器默认为C++ 11,可以手动控制标准,编译时g++ 源文件 -std=c++98
拷贝构造函数(Copy Constructor)
通过某个已存在的对象创建一个新对象需要调用拷贝构造函数。
如果我们不显式定义拷贝构造函数,C++编译器会自动提供一个默认的拷贝构造函数,这个默认的拷贝构造函数只是实现了简单对象复制操作,即浅拷贝效果。在某些情况下,这个默认拷贝构造函数会带来一些问题,此时我们就需要自己定义这个拷贝构造函数,即深拷贝效果。
实例一:静态常、常对象、常对象引用、常对象指针
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int sno = 0;
string name = "";
//静态常属性定义的两种方式:
//声明初始化;
static const int m = 5;
//声明不初始化,定义初始化(静态全局)
static const int q;
static int n;
public:
void show() const
{
cout << "show const" << endl;
}
void show()
{
cout << "show" << endl;
}
void fun()
{
cout << "fun" << endl;
}
};
// const int Student::m = 3;
const int Student::q = 3;
int Student::n = 5;
int main()
{
const Student s1; // 常对象
// s1.sno = 100;
// cout << s1.name << endl;
// s1.show();
// s1.fun();
Student s2;
const Student& s3 = s2; // 常对象引用
const Student* s4 = &s2; // 常对象指针
s2.show();
s4->show();
// s3.sno = 100;
// cout << s3.name << endl;
// s3.show();
// s3.fun();
// s4->sno = 100;
// cout << s4->name << endl;
// s4->show();
// s4->fun();
cout << Student::m << endl;
cout << s2.m << endl;
cout << s4->m << endl;
return 0;
}
实例二:C++11版本前后的初始化标准对比
#include <iostream>
#include <cstring>
using namespace std;
struct Point
{
int x;
int y;
};
class Student
{
public:
int sno {0};
string name {"匿名"}; //默认初始化
Student(int sno, string name):sno(sno),name(name)
{}
};
int main()
{
// // 数据初始化
// int i = 3;
// int nums[5] = {5, 3, 4, 1, 2};
// Point p = {10, 9};
// Student s1 = {1001,"张三"};
// Student s2(1002,"李四");
// // C++11 数据初始化标准
// int i {3};
// int j = {5};
// double d {3.47};
// double b = {-3e-5};
// int nums1[5] {5, 5, 3, 8, 9};
// Point p1 {1003, "王五"};
// int* m = new int {100};
// int* n = new int[3] {3, 5, 8};
//自动类型推导
auto i {3};
auto d {3.147};
auto m {3.14F};
auto p2 = {1006,1};
cout << sizeof(p2) << endl;
short o {10};
decltype('a' + o) k;
return 0;
}
实例三:拷贝构造函数的使用
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
private:
int p_sno = 0;
//char p_name[20] = "";
char* p_name = nullptr;
public:
Person()
{
p_name = new char[20];
p_name[0]= '\0';
//strcpy(p_name,"");
}
Person(int p_sno,const char* p_name):p_sno(p_sno)
{
this->p_name = new char[20];
strcpy(this->p_name,p_name);
}
void show()
{
cout << p_sno << " " << p_name << endl;
}
//拷贝构造函数
Person(const Person &p)
{
// 浅拷贝
p_name = new char[20];
memcpy(&p_sno,&(p.p_sno),sizeof(p_sno));
memcpy(p_name,p.p_name,sizeof(p_name));
//memcpy(this, &p, sizeof(p));
}
~Person()
{
cout << p_name << "快死了" << endl;
delete[] p_name; //p_name为数组
}
};
int main()
{
Person p1 {1001,"张三"};
Person p2 {p1};
Person p3;
Person p4 {p3};
return 0;
}