[C++]:2.类和对象(下)

类和对象 下

前言

嗨,我是firdawn,今天我们就要开始学习 C++ 的类和对象(下) 了,本章将介绍C++中的const修饰的成员函数参数,static修饰的成员,友元函数,以及内部类和初始化列表。下面是本章的思维导图,那么让我们开始吧!

一,inline

1.1 概念

inline 是内联函数关键字,表示内联函数在当前调用它的函数内展开,不用建立函数栈桢进行调用,可以减少建立函数栈桢的时间消耗。

使用内联函数减少时间消耗示一种时间换空间的做法,如果建立函数栈桢,假如被调用的函数内需要执行10条指令,现在要调用1万次,如果是不展开,建立函数栈桢调用,就需要1万加10条指令的空间。如果被调用的函数在被调用的地方展开,就需要10万条指令的空间。

inline 对编译器而言只是一个建议,是否展开是由编译器决定的,一般10行代码左右编译器就会展开,更多的话就直接建立函数栈桢调用。

inline 放在函数定义的地方,注意:inline修饰的函数,默认在符号表里是没有函数名的,也就是说,如果函数的声明和定义,到时候main函数链接的时候就会找不到,inline修饰的函数的地址,也就找不到它,所以inline修饰的函数不要声明和定义分离

类的成员函数默认都是inline修饰的

1.2 代码示例

cpp 复制代码
#include<iostream>

inline Swap(int& x, int& y)
{
	int tmp=x;
	x=y;
	y=tmp;
}

int main()
{
	int x=3;
	int y=17;
	cout<<x<<" "<<y<<endl;
	Swap(x,y);
	cout<<x<<" "<<y<<endl;
	
	return 0;
}

二,初始化列表

2.1 概念

初始化列表就是指构造函数,花括号前的初始化列表,这里是成员变量定义的地方

有些成员变量是必须在初始化列表初始化的,

比如说const修饰的成员变量,引用类型的变量,无默认构造函数的类对象 ,

他们都是必须在初始化列表初始化的,因为const修饰的成员变量在初始化之后就无法修改了,所以必须在初始化的时候给上数值,引用类型的变量需要在初始化的时候告诉他,他是谁的引用,而无默认构造函数的类对象,需要在定义的时候主动传入参数

2.2代码示例

cpp 复制代码
class Date
{

public:
	Date(int year=0, int month=1, int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
	
	}


private:
	int _year;
	int _month;
	int _day;
};

三,const修饰的成员函数参数

3.1概念

当我们不需要修改传入参数的值的时候,我们可以尽量用const修饰的变量接受传入的参数 ,这样的话,不管你传的参数是const的还是非const的,我都可以接收

额外提一句,如果调用函数是传值的话,也可以尽量使用引用接收,或者传值返回也可以使用 类型& 传引用,减少拷贝的时间消耗

3.2代码示例

cpp 复制代码
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}

四,staic修饰的成员

4.1 概念

static可以修饰类的成员变量和成员函数。表示他们是放在金台区的,是该类的所有对象共有的,也就是说所有对象访问的该变量和成员函数都是同一个,对于成员函数来说,因为所有对象访问的都是同一个,所以就不需要this指针作为函数参数,如果该成员函数或者变量是公有的,我们可以直接通过类名作用域限定符,加上该函数名或者变量名,访问到这个函数或者变量

4.2 代码示例

cpp 复制代码
class Date
{
public:
	Date(int year=0, int month=1, int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
		_sum += _n;
		++_n;
	}

	static int GetSum()
	{
		return _sum;
	}

	void DatePrint()const
	{
		cout << _year << "-" << _month << "-" << _day
			<< " " << "Time:" << t._hour << "-" << t._minute 
			<< "-" << t._second << endl;
	}


private:
	int _year;
	int _month;
	int _day;
	
	static int _n;
	static int _sum;
};
int Date::_n=0;
int Date::_sum=0;

4.3 应用

4.3.1, 1+2+3+...+n

cpp 复制代码
class   SumOfOneToN
{
public:
    SumOfOneToN()
    {
        _sum+=_i;
        ++_i;
    }

    static int GetSum()
    {
        return _sum;
    }

    private:
        static int _i;
        static int _sum;

};
int SumOfOneToN::_i=1;
int SumOfOneToN::_sum=0;

class Solution {
public:
    //使用递归,用逻辑与进行条件判断
    // int Sum_Solution(int n) {

    //     n&&(n+=Sum_Solution(n-1));
    //     return n;
    // }

    //使用构造函数,因为使用循环或者递归的作用就是让程序执行n次
    //我们定义一个类对象时,会调用一次构造函数
    //我们可以定义n个对象,就会调用n次构造函数,在构造函数内求和
    int Sum_Solution(int n)
    {
        SumOfOneToN* SolArr=new SumOfOneToN[n];
        delete SolArr;
        int sum=SumOfOneToN::GetSum();
        return sum;
    }
   
};

五,友元函数

5.1 概念

如果一个类外面的函数想访问类里面的私有或保护的成员,我们可以让它成为该类的友元函数,只需要在类里面的最上面声明一下就好了,该友元函数就可以随意的访问和修改该类的,私有的或受保护的成员

5.2 代码示例

cpp 复制代码
	class _Time
	{
	public:
		_Time(int hour = 0, int minute = 0, int second = 0)
			:_hour(hour)
			, _minute(minute)
			, _second(second)
		{
		}

	private:
		int _hour;
		int _minute;
		int _second;
	};

private:
	int _year;
	int _month;
	int _day;

	static int _n;
	static int _sum;
};
int Date::_n = 1;
int Date::_sum = 0;

int SumOfOneToN(int n)
{
	n&&( n =n+ SumOfOneToN(n - 1));
	return n;
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << " ";

	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;

	return in;
}

5.3应用

5.3.1 <<运算符重载

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;


class Time
{
	friend class Date;
public:
	Time(int hour=0, int minute=0, int second=0)
		:_hour(hour)
		,_minute(minute)
		,_second(second)
	{
	}

private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	Date(int year=0, int month=1, int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
		_sum += _n;
		++_n;
	}

	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}

	static int GetSum()
	{
		return _sum;
	}

	void DatePrint()const
	{
		cout << _year << "-" << _month << "-" << _day
			<< " " << "Time:" << t._hour << "-" << t._minute 
			<< "-" << t._second << endl;
	}

	class _Time
	{
	public:
		_Time(int hour = 0, int minute = 0, int second = 0)
			:_hour(hour)
			, _minute(minute)
			, _second(second)
		{
		}

	private:
		int _hour;
		int _minute;
		int _second;
	};

private:
	int _year;
	int _month;
	int _day;
	Time t;
	static int _n;
	static int _sum;
};
int Date::_n = 1;
int Date::_sum = 0;

int SumOfOneToN(int n)
{
	n&&( n =n+ SumOfOneToN(n - 1));
	return n;
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << " ";

	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;

	return in;
}

int main()
{
	//求:1+2+3+..+n
	//不能使用乘除运算符,条件判断和循环关键字

	//1.使用递归,用逻辑与判断递归是否应该结束
	//int n = 10;
	//int sum = SumOfOneToN(n);

	//2.使用循环和递归的作用就是将程序执行n次,
	// 定义1个类对象就会执行一次构造函数,
	// 我们可以定义n个类对象,让构造函数执行n次
	//int n = 10;
	//Date* dateArr = new Date[n];
	//int sum = Date::GetSum();

	Date d1;
	Date d2(2024, 5, 7);
	Date d3;
	d3= d2;
	d1.DatePrint();
	d2.DatePrint();
	d3.DatePrint();

	int n = 4;
	cout << d1 << d2 << d3 << n << endl;
	//cin >> d1 >> d2>> d3;
	cout << d1 << d2 << d3 << n << endl;
	Date::_Time t;
	

	return 0;
}

六,内部类

6.1 概念

定义在类内部的类,叫做内部类。内部的可以,所以的访问外部类的成员,这里的外部类,就是说他所在的那个类。 并且想访问这个内部类,必须要类名,加域访问限定符,加上该类的名字

6.2 代码示例

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;


class Time
{
	friend class Date;
public:
	Time(int hour=0, int minute=0, int second=0)
		:_hour(hour)
		,_minute(minute)
		,_second(second)
	{
	}

private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	Date(int year=0, int month=1, int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
		_sum += _n;
		++_n;
	}

	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}

	static int GetSum()
	{
		return _sum;
	}

	void DatePrint()const
	{
		cout << _year << "-" << _month << "-" << _day
			<< " " << "Time:" << t._hour << "-" << t._minute 
			<< "-" << t._second << endl;
	}

	class _Time
	{
	public:
		_Time(int hour = 0, int minute = 0, int second = 0)
			:_hour(hour)
			, _minute(minute)
			, _second(second)
		{
		}

	private:
		int _hour;
		int _minute;
		int _second;
	};

private:
	int _year;
	int _month;
	int _day;
	Time t;
	static int _n;
	static int _sum;
};
int Date::_n = 1;
int Date::_sum = 0;

int SumOfOneToN(int n)
{
	n&&( n =n+ SumOfOneToN(n - 1));
	return n;
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << " ";

	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;

	return in;
}

int main()
{
	//求:1+2+3+..+n
	//不能使用乘除运算符,条件判断和循环关键字

	//1.使用递归,用逻辑与判断递归是否应该结束
	//int n = 10;
	//int sum = SumOfOneToN(n);

	//2.使用循环和递归的作用就是将程序执行n次,
	// 定义1个类对象就会执行一次构造函数,
	// 我们可以定义n个类对象,让构造函数执行n次
	//int n = 10;
	//Date* dateArr = new Date[n];
	//int sum = Date::GetSum();

	Date d1;
	Date d2(2024, 5, 7);
	Date d3;
	d3= d2;
	d1.DatePrint();
	d2.DatePrint();
	d3.DatePrint();

	int n = 4;
	cout << d1 << d2 << d3 << n << endl;
	//cin >> d1 >> d2>> d3;
	cout << d1 << d2 << d3 << n << endl;
	Date::_Time t;
	

	return 0;
}

结语

好啦,c++的类和对象到这里就结束啦,记得多敲代码,勤练习喔!我们下次见,拜拜!!

相关推荐
黎相思2 小时前
LRU Cache
c++·缓存
UIU1143 小时前
P1003 [NOIP 2011 提高组] 铺地毯
数据结构·c++·算法
xxwl5854 小时前
区域赛算法模版第四章---数据结构
数据结构·c++·算法
余额瞒着我当琳6 小时前
哈希表底层原理解析,直接定址法,哈希冲突,负载因子,哈希函数,开放定址法
java·c++·哈希算法·散列表
xxwl5856 小时前
区域赛算法模版第五章---数学与数论
c++·笔记·算法
局部有解6 小时前
LeetCode Hot100 438 找到字符串中所有字母异位词
c++·算法·leetcode
David猪大卫6 小时前
【C++修炼】string类:string类的模拟实现
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
WL学习笔记8 小时前
模板初阶(C++)
开发语言·c++·后端·模板初阶
埃伊蟹黄面8 小时前
面试常问题目 —— STL
c++·面试·职场和发展·stl