C++类与对象

struct 与 class

struct Student	//Student结构体
{
	//定义成员
	char _name[10];
	char _gender[3];
	int _age;

void PrintStudent()
{
	cout << _name << _gender << _age << endl;
}

};

class Student
{
	//成员函数和成员变量
};

struct 默认访问权限: 公有 : 类外可以访问
class默认访问权限 : 私有 : 类外不能访问

类的两种定义方式
//声明 定义全部放在类内
class Person
{
public:
	void showInfo()
{
	cout << _name << "-" << _sex << "-" << _age;
}
	char* _name;
	char* _sex;
	int _age;
}

//声明 与定义分开
//person.h
class Person
{
public:
	void showInfo()
	char* _name;
	char* _sex;
	int _age;

person.cpp
#include "person.h"
void Person::showInfo()
{
	cout << _name << "-" << _sex << "-" << _age;
}
访问限定符

1.public 修饰的成员在类外可以直接访问
2.protected和private修饰的类外不能直接访问
3.访问权限作用域从该访问限定符到下一个访问限定符出现为止

类对象的大小

与struct大体一致
成员函数不存放在类内
sizeof 只有类成员的大小
若果是个空类 大小为 1

this指针

//定义的一个Date类
class Date
{
public:
	void Display()
	{
		cout << _year << _month << _day << endl;
	}
}

	void SetDate(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1, d2;
	d1.SetDate(2019, 10, 31);
	d2.SetDate(2019, 10, 31);
	d1.Display();
	d2.Display();
	system("pause");
	return 0;
}

C++编译器给每个“成员函数”增加一个隐藏的指针参数,让该指针指向当前对象,在函数体重所有成员变量的操作都是通过该指针进行访问。所有操作对用户透明,编译期自动完成

this指针的特点
1.this指针类型 : 类类型* const (所以指针不能变)
2.只能在“成员函数”内部使用
3.对象中不储存this指针
4.this指针是成员函数第一个隐含的指针形参
一般情况有编译器通过ecx(寄存器)自动传递

类的默认成员函数

任何一个类都会默认生成 6 个默认成员函数
包括空类(类中什么成员都没有)
1.构造函数:完成初始化
2.析构函数:完成清理
3.拷贝构造函数:使用同类对象初始化创建对象
4.赋值重载函数:把一个对象赋值给了另一个对象
5.取地址重载:包括普通对象和const对象取地址

1. 构造函数

构造函数是一个特殊的成员函数,名字与类名相同,创建类对象时编译器自动调用,且在对象的生命周期内只调用一次。
构造函数的任务是初始化对象,并不是开空间创造对象
特点:
1.函数名与类名相同
2.无返回值
3.对象实例化是编译器自动调用
4.构造函数可以重载

class Date()
{
public:
	//无参构造函数
	Date()
	{}
	//带参数构造函数
	Date(int year, int month, int day)
	//初始化
	//类中声明次序就是初始化列表的初始化顺序
	//与他在初始化列表中的先后次序无关
		:_year(year)
		,_month(month)
		,_day(day)
	//对变量进行初始化
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};

void Text()
{
	//调用无参构造函数
	Date d1;
	//调用无参构造函数对象后面不跟括号
	//否则就成为函数声明
	
	//调用带参构造函数
	Date d2(2019, 11, 1);
}

没有显式定义构造函数(用户自行构造的函数),C++会自动生成无参的默认构造函数
如果在一个类中定义自定义类型(用户自己定义的类型),生成的构造函数会 先去 自定义类型成员调用它的默认成员函数

class Time
{
public:
	Time()
	{
		cout << "Time" << endl;
		_hour = 0;
	}
private:
	int _hour;
};

class Date
{
	Date()
	{
		cout << "Date" << endl;
		_year = 0;
	}
private:
	int _year;
	Time _t;
}
//输出结果
//	time
//	Date
析构函数

析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。对象在销毁时会自动调用析构函数,完成类的资源清理工作
特征
1.析构函数名在类名前面加上字符 ~
2.无参数返回值
3. 一个类有且只有一个析构函数,若未显示定义,系统自动生成默认析构函数
4.在对象生命周期结束时,C++编译系统自动调用析构函数

拷贝构造函数

只有单个形参,对类类型对象的引用(一般用const修饰),用已存在的对象创建新对象时编译器自动调用
特征:
1.拷贝构造函数是构造函数的一个重载形式
2.拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用

class Date
{
public:
	Date()
	{}
	
	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//错误写法
	Date(Date d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//调用构造函数
	// Date d1;
	//调用拷贝构造函数
	// Date d2(d1); 这里面的 d1需要进行拷贝构造一份副本来进行对 d2的拷贝构造
	// 错误写法会引起死循环 一直在调用拷贝构造来拷贝d1 的副本
	
private:
	int _year;
	int _month;
	int _day;
};

若未显示定义,系统生成默认的拷贝构造函数 会对对象按内存储存字节序完成拷贝,叫做浅拷贝,或者值拷贝

//浅拷贝的缺陷
class String
{
public:
	String(const char* str = "jack")
	{
		_str = (char*) malloc (strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String " << endl;
		free(_str);
	}
private:
	char* _str;
};

int main()
{
	String s1("hello");
	String s2(s1);
	return 0;
	//在程序结束时 调用析构函数进行清理
	//但是在 第二步复制拷贝
	//使用系统默认构造的复制拷贝函数
	//是进行浅拷贝
	//拷贝过来的是s1的地址 而不是s1里面的值
	//在第二次调用析构函数的时候原来的地址空间被释放
	//所以在第二次调用时候系统崩溃
}
赋值运算符重载

返回值类型 operator 操作符 ( 参数列表 )
,* — :: — sizof ---- ?: ---- . 这五种运算符不能重载

//  == 的重载
class Date
{
public:
	Date(int year = 2019, int month = 11, int day = 1)
	:_year(year)
	,_month(month)
	,_day(day)
	{}
	bool operator==(const Date& d2)
	{
		return _year == d2._year
			&& _month == d2.month
			&& _day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

void Text()
{
	Date d1(2019, 11, 1);
	Date d2(2019, 11, 2);
	cout << (d1 == d2) << endl;
}

赋值运算符的重载
1.参数类型
2.返回值
3.检测是否自己给自己赋值
4.返回*this
5.没有显示定义,编译器也会生成一个,也是浅拷贝
与拷贝构造函数一样有缺陷

class String
{
public:
	String(const char* str = "")
	{
		_str = (char*) malloc (strlen(str) + 1);
		strcpy(_str, str);
	}

	~String()
	{
		cout << "String()" << endl;
		free(_str);
	}
private:
	char* _str;

int main()
{
	String s1("hello");
	String s2("world");
	s1 = s2;
	//跟拷贝构造一样的有缺陷
}
}
const成员

const修饰类的成员函数
将const 修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰改成原函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。(实际就是用const 关键字限制访问的权限)

class Date
{
public:
	void Show() const
	{
		cout << _year << endl;
	}
	//与下面这个函数等价
	void Show(const Date* this)
	{
		cout << this->_year << endl;
	}
private:
	int _year;
};
static成员

static的类成员称为类的静态成员,用static修饰的成员变量,称之为静态成员变量,用static修饰的成员函数,称之为静态成员函数静态的成员变量一定要在类外进行初始化

class A
{
public:
	A()
	{
		++_count;
	}
	A(const A& t)
	{
		++_count;
	}
	static int GetACount()
	{
		return _count;
	}
private:
	static int _count;
};

int A::_count = 0;

void Text()
{
	cout << A::GetACount() << endl;
	A a1, a2;
	A a3(a1);
	//静态成员函数可以通过对象访问
	//也可以 类名::函数名
	//cout << A::GetACount() << endl;
	cout << a1.GetACount() << endl;
}

int main()
{
	Text();
	return 0;
}

特点:
1.静态成员为所有类对象所共享
2.静态成员变量必须在类外定义
3.声明需要加上static 定义不需要
4.静态成员函数没有隐藏的this指针,也不能访问任何非静态成员

友元函数

实现 opertaor << 重载

class Date
{
public:
	Date(int year, int month, int day)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	//不能使用static , 因为要复制d中的内容,将他们放入控制台
	//所有用静态成员函数无法实现
	static ostream& operator<<(Date d, ostream& _cout)
	{
		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;
	}
	ostream& operator<<(ostream& _cout)
	{
		_cout << _year << "-" << _month << "-" << _day;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d(2017, 12, 24);
	//因为static的静态成员函数无法使用
	//Date::operator<<(d, cout);
	//可以使用 但是与平时所使用的的 cout << 不用
	//d << cout;
	return 0;
}

友元函数可以直接访问类的私有成员,他是定义在类外部的普通函数,不属于任何类,但是需要在类内部声明,生命时需要加friend关键字

class Date
{
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, const Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}
istream& operator>>(istream& _cin, const Date& d)
{
	_cin >> d._year;
	_cin >> d._month;
	_cin >> d._day;
	return _cin;
}
int main()
{
	Date d;
	cin >> d;
	cout << d << endl;
	return 0;
}

友元函数可以访问类的私有成员,但不是类的成员函数
友元函数不能用const修饰
友元函数可以在类的定义的任何地方声明,不受类访问限定符限制

2 友元类
友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。(当然公有的也是可以的)
Time类中 声明 Date类为友元类,那Date 可以访问 Time的 ,但是 Time不能访问Date的(类似于我承认你是我朋友 ,但是你不一定认为我是你的朋友) ,所以友元关系不能传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值