构造函数,析构函数,拷贝构造函数
//堆区开辟中new/delete才会调用构造函数与析构函数, malloc/free不调用构造函数与析构函数
#include <iostream>
using namespace std;
class Student {
public:
char * name;
int age;
// : Student("kevin", 55) 相当于转接调用两个参数的构造函数
Student() : Student("kevin", 55){
cout << "无参构造" << endl;
}
//name(name) 等价于 this->name = name;
Student(char *name) : name(name){
cout << "一个参数的构造" << endl;
}
Student(char *name, int age) : name(name), age(age){
cout << "两个参数的构造" << endl;
}
//析构函数,对象释放时执行(比如构造中若开辟了堆区内存,析构函数需要把堆区内存释放)
~Student() {
cout << "析构函数" << endl;
}
//自定义覆盖系统拷贝构造函数,const Student & st表示传入参数只读
Student(const Student & st) {
cout << "拷贝构造函数" << endl;
// this->name = st.name;
// this->age = st.age;
this->name = "sss";
this->age = 100;
}
};
int main() {
//栈区
Student s1;//两个参数的构造 —> 无参构造
Student s2("li", 30);
//=执行拷贝构造函数,对象会有默认的拷贝构造函数
s2 = s1;
cout << s2.name << s2.age << endl;
//只有声明拷贝才会走自定义的拷贝构造函数
Student s3 = s1;
cout << s3.name << s3.age << endl;
//堆区
Student * s = new Student("li");
if (s) {
delete s;
s = NULL;
}
return 0;
}
static静态关键字
#include <iostream>
using namespace std;
class Student {
public:
//类中静态属性先声明后在类外初始化
static int age;
static int getAge() {
return age;
}
};
//静态属性初始化(必须初始化)
int Student::age = 10;
int main() {
//类名 :: 调用静态属性和静态方法
cout << Student::age << endl;
cout << Student::getAge() << endl;
return 0;
}
对象中this和const关键字关系
//默认构造方法中会在栈区开辟空间对外暴露指针this
//this另外有隐式的const修饰:Student * const this(【类型 * const this】),即指针常量。
//因此,this指针可以修改所对应的值,而无法修改指针地址,即set方法可以实现,但this = 0x1000会编译报错
//get方法后加上const则会将此时的this变成 const Student * const,即常量指针常量。
//因此get方法中是无法修改this指针所对应的值的
class Student {
private:
char * name;
int age;
public:
void setName(char * name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
char * getName() const {
return this->name;
}
int getAge() const {
return this->age;
}
};
友元函数和友元类
#include <iostream>
using namespace std;
class Person {
private:
int age = 15;
//友元函数(类中声明,外面实现)
friend int getAge(Person * person);
friend void changeAge(Person * person, int newAge);
//友元类(指向一个类使其可以访问自己的私有成员)
friend class YouY;
};
//被指定的友元类可以访问所有私有成员
class YouY {
public:
Person person;
int getPersonAge() {
return person.age;
}
};
//实现的友元函数可以访问其私有成员
int getAge(Person * person){
return person->age;
}
void changeAge(Person * person, int newAge){
person->age = newAge;
}
int main() {
Person person;
cout << getAge(&person) << endl;
YouY you;
cout << you.getPersonAge() << endl;
changeAge(&you.person, 55);
cout << you.getPersonAge() << endl;
return 0;
}
函数用法小结
//函数头文件定义(Book.h)
#include <iostream>
using namespace std;
//判断定义宏
#ifndef DEMO4_BOOK_H
#define DEMO4_BOOK_H
class Book {
private:
char * name;
public:
// 静态成员声明
static int id;
// 构造函数声明
Book();
Book(char *);
// 析构函数声明
~Book();
// 拷贝构造函数声明
Book(const Book & book);
// 普通函数声明
char * getName() const;
void setName(char *);
// 静态函数声明
static void changeId(int);
// 友元函数的声明
friend void changeName(Book * book, char * name);
};
//关闭宏
#endif
//头文件实现(Book.cpp)
#include "Book.h"
// 实现构造函数
Book::Book() {
cout << "默认构造函数" << endl;
}
Book::Book(char * name) {
cout << "1个参数构造函数" << endl;
}
// 实现析构函数
Book::~Book() {
cout << "析构函数" << endl;
}
// 实现拷贝构造函数
Book::Book(const Book &book) {
cout << "拷贝构造函数" << endl;
}
// 实现普通函数
char * Book::getName() const{
return this->name;
}
void Book::setName(char * name) {
this->name = name;
}
// 初始化静态属性【不需要增加 static关键字】
int Book::id = 878;
// 实现静态函数,【不需要增加 static关键字】
void Book::changeId(int id) {
Book::id = id;
}
// 友元的实现
// 友元特殊:不需要关键字,也不需要 对象:: ,只需要保证 函数名(参数)
void changeName(Book * book, char * name) {
book->name = name;
}