C++基础(4)——类和对象(下)

目录

1.再探构造函数

2. 类型转换

 3. static成员

4. 友元

5. 内部类

 6. 匿名对象


1.再探构造函数

1.之前我们实现构造函数时,初始化成员变量主要使⽤函数体内赋值,构造函数初始化还有⼀种⽅式,就是初始化列表,初始化列表的使⽤⽅式是以⼀个冒号开始,接着是⼀个以逗号分隔的数据成 员列表,每个"成员变量"后⾯跟⼀个放在括号中的初始值或表达式。

每个成员变量在初始化列表中只能出现⼀次,语法理解上初始化列表可以认为是每个成员变量定义初始化的地⽅。

2.引⽤成员变量,const成员变量,没有默认构造的类类型变量,必须放在初始化列表位置进⾏初始 化,否则会编译报错。

3.C++11⽀持在成员变量声明的位置给缺省值,这个缺省值主要是给没有显⽰在初始化列表初始化的 成员使⽤的。

4.尽量使⽤初始化列表初始化,因为那些你不在初始化列表初始化的成员也会⾛初始化列表,如果这 个成员在声明位置给了缺省值,初始化列表会⽤这个缺省值初始化。如果你没有给缺省值,对于没 有显⽰在初始化列表初始化的内置类型成员是否初始化取决于编译器,C++并没有规定。对于没有 显⽰在初始化列表初始化的⾃定义类型成员会调⽤这个成员类型的默认构造函数,如果没有默认构造会编译错误。

5.初始化列表中按照成员变量在类中声明顺序进⾏初始化,跟成员在初始化列表出现的的先后顺序⽆关。建议声明顺序和初始化列表顺序保持⼀致。

敲黑板: 

⽆论是否显⽰写初始化列表,每个构造函数都有初始化列表;

⽆论是否在初始化列表显⽰初始化,每个成员变量都要⾛初始化列表初始化;

#include<iostream>

using namespace std;

class Time

{

public:
 Time(int hour)
 :_hour(hour)
 {
 cout << "Time()" << endl;
 }

private:
 int _hour;
};

class Date

{

public:
 Date(int& x, int year = 1, int month = 1, int day = 1)
 :_year(year)
 ,_month(month)
 ,_day(day)
 ,_t(12)
 ,_ref(x)
 ,_n(1)
 {
 // error C2512: “Time”: 没有合适的默认构造函数可⽤ 
 // error C2530 : “Date::_ref” : 必须初始化引⽤ 
 // error C2789 : “Date::_n” : 必须初始化常量限定类型的对象 
 }
 void Print() const
{
 cout << _year << "-" << _month << "-" << _day << endl;
 }

private:
 int _year;
 int _month;
 int _day;
 Time _t; // 没有默认构造 
 int& _ref; // 引⽤ 
 const int _n; // const 

};

int main()
{
 int i = 0;
 Date d1(i);
 d1.Print();
 return 0;
}
#include<iostream>

using namespace std;

class Time

{

public:
 Time(int hour)
 :_hour(hour)
 {
 cout << "Time()" << endl;
 }

private:
 int _hour;
};

class Date

{

public:
 Date()
 :_month(2)
 {
 cout << "Date()" << endl;
 }
void Print() const

 {
 cout << _year << "-" << _month << "-" << _day << endl;
 }

private:
 // 注意这⾥不是初始化,这⾥给的是缺省值,这个缺省值是给初始化列表的 
 // 如果初始化列表没有显⽰初始化,默认就会⽤这个缺省值初始化 
 int _year = 1;
 int _month = 1;
 int _day;
 Time _t = 1;
 const int _n = 1;
 int* _ptr = (int*)malloc(12);
};

int main()
{
 Date d1;
 d1.Print();
 return 0;
}

下⾯程序的运⾏结果是什么:

#include<iostream>

using namespace std;

class A

{

public:
 A(int a)
 :_a1(a)
 , _a2(_a1)
{}
 void Print() {
 cout << _a1 << " " << _a2 << endl;
 }

private:
 int _a2 = 2;
 int _a1 = 2;
};

int main()
{
 A aa(1);
 aa.Print();
}

结果:1和随机值

2. 类型转换

• C++⽀持内置类型隐式类型转换为类类型对象,需要有相关内置类型为参数的构造函数。

• 构造函数前⾯加explicit就不再⽀持隐式类型转换。

• 类类型的对象之间也可以隐式转换,需要相应的构造函数⽀持。

#include<iostream>

using namespace std;

class A

{

public:
 // 构造函数explicit就不再⽀持隐式类型转换 
 // explicit A(int a1)

 A(int a1)
 :_a1(a1)
 {}
 //explicit A(int a1, int a2)

 A(int a1, int a2)
 :_a1(a1)
 , _a2(a2)
 {}
 void Print()
 {
 cout << _a1 << " " << _a2 << endl;
 }
int Get() const

 {
 return _a1 + _a2;
 }

private:
 int _a1 = 1;
 int _a2 = 2;
};

class B

{

public:
 B(const A& a)
 :_b(a.Get())
 {}

private:
 int _b = 0;
};

int main()
{
 // 1构造⼀个A的临时对象,再⽤这个临时对象拷⻉构造aa3 
 // 编译器遇到连续构造+拷⻉构造->优化为直接构造 
 A aa1 = 1;
 aa1.Print();
 const A& aa2 = 1;
 // C++11之后才⽀持多参数转化 
 A aa3 = { 2,2 };
 // aa3隐式类型转换为b对象 
 // 原理跟上⾯类似 
 B b = aa3;
 const B& rb = aa3;
 return 0;
}

 3. static成员

• ⽤static修饰的成员变量,称之为静态成员变量,静态成员变量⼀定要在类外进⾏初始化。

• 静态成员变量为所有类对象所共享,不属于某个具体的对象,不存在对象中,存放在静态区。

• ⽤static修饰的成员函数,称之为静态成员函数,静态成员函数没有this指针。

• 静态成员函数中可以访问其他的静态成员,但是不能访问⾮静态的,因为没有this指针。

• ⾮静态的成员函数,可以访问任意的静态成员变量和静态成员函数。

• 突破类域就可以访问静态成员,可以通过类名::静态成员或者对象.静态成员来访问静态成员变量和静态成员函数。

• 静态成员也是类的成员,受public、protected、private访问限定符的限制。

• 静态成员变量不能在声明位置给缺省值初始化,因为缺省值是个构造函数初始化列表的,静态成员 变量不属于某个对象,不⾛构造函数初始化列表。

// 实现⼀个类,计算程序中创建出了多少个类对象? 

#include<iostream>

using namespace std;

class A

{

public:
 A() 
 { 
 ++_scount;
 }
 A(const A& t) 
 { 
 ++_scount; 
 }
 ~A() 
 { 
 --_scount;
 }
 static int GetACount() 
 { 
 return _scount;
 }

private:
 // 类⾥⾯声明 
 static int _scount;
};

// 类外⾯初始化 

int A::_scount = 0;
int main()
{
 cout << A::GetACount() << endl;
 A a1, a2;
 A a3(a1);
 cout << A::GetACount() << endl;
 cout << a1.GetACount() << endl;
 // 编译报错:error C2248: “A::_scount”: ⽆法访问 private 成员(在“A”类中声明) 
 //cout << A::_scount << endl;

 return 0;
}

求1+2+3+...+n_⽜客题霸_⽜客⽹icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=11200&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

class Sum

{

public:
 Sum()
 {
 _ret += _i;
 ++_i;
 }
 static int GetRet()
 {
 return _ret;
 } 

private:
 static int _i;
 static int _ret;
};

int Sum::_i = 1;

int Sum::_ret = 0;

class Solution {

public:
 int Sum_Solution(int n) {
 // 变⻓数组 
 Sum arr[n];
 return Sum::GetRet();
 }
};

设已经有A,B,C,D4个类的定义,程序中A,B,C,D构造函数调⽤顺序为?()E

设已经有A,B,C,D4个类的定义,程序中A,B,C,D析构函数调⽤顺序为?()B

4. 友元

• 友元提供了⼀种突破类访问限定符封装的⽅式,友元分为:友元函数和友元类,在函数声明或者类 声明的前⾯加friend,并且把友元声明放到⼀个类的⾥⾯。

• 外部友元函数可访问类的私有和保护成员,友元函数仅仅是⼀种声明,他不是类的成员函数。

• 友元函数可以在类定义的任何地⽅声明,不受类访问限定符限制。

• ⼀个函数可以是多个类的友元函数。

• 友元类中的成员函数都可以是另⼀个类的友元函数,都可以访问另⼀个类中的私有和保护成员。

• 友元类的关系是单向的,不具有交换性,⽐如A类是B类的友元,但是B类不是A类的友元。

• 友元类关系不能传递,如果A是B的友元,B是C的友元,但是A不是C的友元。

• 有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多⽤。

#include<iostream>
using namespace std;
// 前置声明,都则A的友元函数声明编译器不认识B 
class B;
class A
{
 // 友元声明 
friend void func(const A& aa, const B& bb);
private:
 int _a1 = 1;
 int _a2 = 2;
};
class B
{
 // 友元声明 
 friend void func(const A& aa, const B& bb);
private:
 int _b1 = 3;
 int _b2 = 4;
};
void func(const A& aa, const B& bb)
{
 cout << aa._a1 << endl;
 cout << bb._b1 << endl;
}
int main()
{
 A aa;
 B bb;
 func(aa, bb);
 return 0;
}
#include<iostream>
using namespace std;
class A
{
 // 友元声明 
 friend class B;

private:
 int _a1 = 1;
 int _a2 = 2;
};
class B
{
public:
 void func1(const A& aa)
 {
 cout << aa._a1 << endl;
 cout << _b1 << endl;
 }
 void func2(const A& aa)
 {
 cout << aa._a2 << endl;
 cout << _b2 << endl;
 }
private:
 int _b1 = 3;
 int _b2 = 4;
};
int main()
{
 A aa;
 B bb;
 bb.func1(aa);
 bb.func1(aa);
 return 0;
}

5. 内部类

• 如果⼀个类定义在另⼀个类的内部,这个内部类就叫做内部类。内部类是⼀个独⽴的类,跟定义在全局相⽐,他只是受外部类类域限制和访问限定符限制,所以外部类定义的对象中不包含内部类。

• 内部类默认是外部类的友元类。

• 内部类本质也是⼀种封装,当A类跟B类紧密关联,A类实现出来主要就是给B类使⽤,那么可以考 虑把A类设计为B的内部类,如果放到private/protected位置,那么A类就是B类的专属内部类,其他地⽅都⽤不了。

#include<iostream>

using namespace std;

class A

{
private:
 static int _k;
 int _h = 1;

public:
 class B // B默认就是A的友元 
 {
 public:
 void foo(const A& a)
 {
 cout << _k << endl; //OK

 cout << a._h << endl; //OK

 }
 };
};

int A::_k = 1;

int main()
{
 cout << sizeof(A) << endl;
 A::B b;
 A aa;
 b.foo(aa);
 return 0;
}

求1+2+3+...+n_⽜客题霸_⽜客⽹icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=11200&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

class Solution {
 // 内部类 
 class Sum

 {
 public:
 Sum()
 {
 _ret += _i;
 ++_i;
 }
 };
 static int _i;
static int _ret;

public:
 int Sum_Solution(int n) {
 // 变⻓数组 
 Sum arr[n];
 return _ret;
 }
};

int Solution::_i = 1;

int Solution::_ret = 0;

 6. 匿名对象

• ⽤ 类型(实参) 定义出来的对象叫做匿名对象,相⽐之前我们定义的 类型 对象名(实参)定义出来的 叫有名对象

• 匿名对象⽣命周期只在当前⼀⾏,⼀般临时定义⼀个对象当前⽤⼀下即可,就可以定义匿名对象。

class A

{

public:
 A(int a = 0)
 :_a(a)
 {
 cout << "A(int a)" << endl;
 }
 ~A()
 {
 cout << "~A()" << endl;
 }

private:
 int _a;
};

class Solution {

public:
 int Sum_Solution(int n) {
 //...

 return n;
 }
};

int main()
{
 A aa1;
 // 不能这么定义对象,因为编译器⽆法识别下⾯是⼀个函数声明,还是对象定义 
 //A aa1();

 // 但是我们可以这么定义匿名对象,匿名对象的特点不⽤取名字, 
 // 但是他的⽣命周期只有这⼀⾏,我们可以看到下⼀⾏他就会⾃动调⽤析构函数 
 A();
 A(1);
 A aa2(2);
 // 匿名对象在这样场景下就很好⽤,当然还有⼀些其他使⽤场景,这个我们以后遇到了再说 
 Solution().Sum_Solution(10);
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西阳未落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值