目录
前言
1.再探构造函数
2.类型转换
3.static成员
4. 友元
5.内部类
6.匿名对象
7.对象拷贝时的编译器优化
结束语
前言
在前面的博客中,我们对类的默认成员函数都有了一定了解,同时实现了一个日期类对所学的没内容进行扩展延伸,本节我们将对类与对象进行大致的最终学习。
1.再探构造函数
#include <iostream>
using namespace std;
class Time {
public:Time(int hour=1): _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 x = 1;Date d1(x);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;
}

补充题目
下面程序的运行结果是什么(D)A. 输出 1 1 B. 输出 2 2 C. 编译报错D. 输出 1 随机值 E. 输出 1 2 F. 输出 2 1
#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();
}
_a2
和 _a1
的初始化顺序不符合它们在类中声明的顺序。这将导致 _a2
使用未初始化的 _a1
值,所以输出的_a2是个随机值
2.类型转换
C++ 支持内置类型(如 int
、float
等)隐式转换为类类型对象,只要类中定义了一个接受该内置类型作为参数的构造函数。这种构造函数通常称为单参数构造函数,能够允许编译器在需要时自动创建对象。
#include <iostream>
using namespace std;class MyClass {
public:// 单参数构造函数,接受一个 int 类型MyClass(int value) : _value(value) {cout << "MyClass constructed with value: " << _value << endl;}void Print() const {cout << "Value: " << _value << endl;}private:int _value;
};int main() {MyClass obj = 10; // 隐式转换,从 int 到 MyClassobj.Print(); // 输出: Value: 10MyClass anotherObj(20); // 显式构造anotherObj.Print(); // 输出: Value: 20return 0;
}
注意事项
-
隐式转换的风险:
- 尽管隐式转换很方便,但可能会导致代码的可读性降低,尤其是在较大的代码库中。为了避免不必要的隐式转换,可以将构造函数声明为
explicit
,防止不小心的隐式转换:
- 尽管隐式转换很方便,但可能会导致代码的可读性降低,尤其是在较大的代码库中。为了避免不必要的隐式转换,可以将构造函数声明为
class MyClass {
public:
explicit MyClass(int value)
: _value(value) {}
// ...
};
-
多重构造:
- 如果类中有多个构造函数,确保它们能够明确区分,以避免二义性的问题。
3.static成员
#include <iostream>
using namespace std;
class A {
public:A() {++_count;}A(const A& count) {++_count;}~A() {--_count;}static int getcount() {return _count;}
private://类里面声明static int _count;
};
int A::_count = 520;
int main() {cout << A::getcount() << endl; // 输出:520A t1; // _count 增加到 521A t2(t1); // _count 增加到 522cout << A::getcount() << endl; // 输出:522// 此时 t1 和 t2 仍然存在cout << t1.getcount() << endl;//522cout << t2.getcount() << endl;//522{A t3(t1); // _count 增加到 523cout << A::getcount() << endl; // 输出:523} // t3 超出作用域, _count 减少到 522cout << A::getcount() << endl; // 输出:522return 0;
}
题目练习
C c;int main() {A a;B b;static D d;return 0;}
A:D B A C B:B A D C C:C D B AD:A B D C E:C A B D F:C D A B
在全局或局部作用域中,构造函数的调用顺序如下:
- 全局和静态对象的构造:在程序启动时,全局对象(如果有)和静态对象会首先被构造。
- 局部对象的构造:然后,当程序进入
main()
函数时,局部对象的构造按定义顺序调用。
析构函数的调用顺序与构造函数的顺序相反。析构函数会在对象的生命周期结束时被调用,顺序如下:
- 局部对象的析构:当程序退出
main()
函数时,局部对象按定义的相反顺序析构。 - 全局和静态对象的析构:在
main()
函数结束后,全局对象和静态对象会被析构。
4. 友元
#include <iostream>
using namespace std;class B;//前置声明
class A {friend void func(const A& a, const B& b);
private:int _a = 520;int _b = 1314;
};
class B {friend void func(const A& a, const B& b);
private:int _a = 1314;int _b = 520;
};
void func(const A& a, const B& b) {cout << a._a << endl;cout << b._b << endl;
}int main() {A a;B b;func(a, b);return 0;
}
#include<iostream>
using namespace std;
class A
{// 友元声明friend class B;
private:int _a1 = 520;int _a2 = 1314;
};
class B
{
public:void func1(const A& aa){cout << aa._a1 << endl;cout << _b2 << endl;}void func2(const A& aa){cout << aa._a2 << endl;cout << _b1 << endl;}
private:int _b1 = 520;int _b2 = 1314;
};
int main()
{A aa;B bb;bb.func1(aa);bb.func2(aa);return 0;
}
5.内部类
#include <iostream>
using namespace std;class A {
private:static int _a; // 静态成员int _b; // 非静态成员
public:class B {public:void print(const A& a) {cout << _a << endl; // 访问静态成员cout << a._b << endl; // 访问非静态成员}};
};int A::_a = 520; // 静态成员初始化int main() {cout << "A类的大小:" << sizeof(A) << endl; // 输出 A 类的大小A::B b; // 创建 B 类的对象A aa; // 创建 A 类的对象b.print(aa); // 调用 print 函数return 0;
}
6.匿名对象
#include <iostream>
using namespace std;
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;}
};bool myfunction(int i, int j) { return (i > j); }int main()
{A aa1; //有名对象// 不能这么定义对象,因为编译器无法识别下面是一个函数声明,还是对象定义//A aa2();// 生命周期只在当前一行A(); // 匿名对象A(1);Solution st;cout << st.Sum_Solution(10) << endl;// 为了更方便cout << Solution().Sum_Solution(10) << endl;return 0;
}
7.对象拷贝时的编译器优化
#include <iostream>
using namespace std;
class A {
public:A(int a=0):_a(a){cout << "A(int a)" << endl;}A(const A& aa) :_a(aa._a){cout << "A(const A& aa) " << endl;}A& operator=(const A& aa){cout << "A& operator=(const A& aa)" << endl;if (this != &aa){_a = aa._a;}return *this;}~A() {cout << "~A()" << endl;}void Print(){cout << "A::Print->" << _a << endl;}A& operator++(){_a += 100;return *this;}
private:int _a ;
};
void f1(A aa)
{}
A f2()
{A aa(1);++aa;cout << "##########" << endl;return aa;
}
int main() {A aa1 ;aa1.Print();//const A& aa2 = 2;//A aa3(aa2);//A aa1;//f1(aa1);//cout << endl;// 隐式类型,连续构造+拷⻉构造->优化为直接构造//f1(1);// ⼀个表达式中,连续构造+拷⻉构造->优化为⼀个构造//f1(A(2));//cout << endl;//cout << "***********************************************" << endl;// 传值返回// 返回时⼀个表达式中,连续拷⻉构造+拷⻉构造->优化⼀个拷⻉构造 (vs2019)// ⼀些编译器会优化得更厉害,进⾏跨⾏合并优化,直接变为构造。(vs2022)//f2();//cout << endl;// 返回时⼀个表达式中,连续拷⻉构造+拷⻉构造->优化⼀个拷⻉构造 (vs2019)// ⼀些编译器会优化得更厉害,进⾏跨⾏合并优化,直接变为构造。(vs2022)// A aa2 = f2();// cout << endl;// ⼀个表达式中,连续拷⻉构造+赋值重载->⽆法优化aa1 = f2();cout << endl;A ret = f2();ret.Print();cout << "*********" << endl << endl;//return 0;}
结束语
本节内容到此结束,类与对象的学习也暂时告别一段落了,希望接下来继续能和大家探讨C++的学习,最后呢,感谢各位友友的支持,讲解不足之处也希望大家多多包涵!!!