作用:在结构体中的成员可以是另一个结构体,例如每个老师有自己的信息,包括他带的学生,然后每个学生也有自己的信息。
- #include<iostream>
- Using namespace std;
- //定义学生的结构体
- struct student
- {
- int name;
- int age;
- int score;
- }
- //定义老师的结构体
- struct teacher
- {
- int id;
- string name;
- int age;
- struct student;
- }
- int main()
- {
- teacher t; //声明一个老师
- t.name=”wang” //初始化老师的数据
- t.age=50;
- t.stu.name=”Li”; //初始化老师里面的学生
- t.stu.age=20;
- t.stu.score=60;
- }
作用:将结构体作为参数向函数中传递,传递方式有两种:值传递(形参改变不改变实参)和地址传递(形参改变将改变实参)。
- #include<iostream>
- #include<string>
- using namespace std;
- //定义学生的结构体
- struct student
- {
- string name;
- int age;
- int score;
- };
- //值传递
- void printStudent1(student s)
- {
- s.age = 100; //在值传递里面修改了年龄
- cout << s.name << s.age << s.score << endl;
- }
- //地址传递
- void printStudent2(student* s)
- {
- //虽然上面修改了形参,但是输出的实参没有变化,因此程序运行到printStudent2
- //时,age依然等于20
- cout << s->name << s->age << s->score << endl;
- //修改地址,就会发生改变
- s->age = 100;
- cout << s->name << s->age << s->score << endl;
- }
-
- int main()
- {
- student s;
- s.name = "Wang";
- s.age = 20;
- s.score = 80;
- printStudent1(s);
- printStudent2(&s);
- return 0;
- }
如果你不想修改主函数数据的话,那就用值传递。反之若修改主函数中的数据,则使用地址传递。
作用:用const来防止误操作。
- #include<iostream>
- #include<string>
- using namespace std;
- //定义学生的结构体
- struct student
- {
- string name;
- int age;
- int score;
- };
- //地址传递:将函数中的形参改为指针,可以减少内存空间
- void printStudent(const student *s)
- {
- //若在函数体内不小心改写了消息,此时会报错,可以防止误操作
- s->age = 150;
- cout << s->name << s->age << s->score << endl;
- }
- int main()
- {
- student s;
- s.name = "Wang";
- s.age = 20;
- s.score = 80;
- printStudent(&s);
- return 0;
- }
在C++中,这两个唯一的区别就是默认的访问权限不同,struct默认为public,class默认为private。
- #include<iostream>
- using namespace std;
- class C1
- {
- int m_A;//默认权限为私有
- };
- struct C2
- {
- int m_A;//默认为私有
- };
- int main()
- {
- C1 c1;
- c1.m_A = 100; //错误,类外不能访问私有成员
- C2 c2;
- c2.m_A = 100; //正确,因为结构体中默认权限为public
- return 0;
- }
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- Person()
- {
- cout << "构造函数被调用了!" << endl;
- }
- //构造函数没有返回值,不用写void
- //函数名与类名相同,且可以有参数,可以重载
- //在创建对象时,构造函数自动被调用且仅被调用一次
-
- //析构函数
- //没有返回值,在名称前+“~”
- //析构函数不能有参数,因此不能重载
- //对象在销毁前会自动调用析构函数,切仅一次
- ~Person() {
- cout << "析构函数被调用了" << endl;
- }
- };
- void test01()
- {
- Person p;
- }
- int main()
- {
- test01();
- return 0;
- }
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- Person()
- {
- cout << "构造函数被调用了!" << endl;
- }
- ~Person() {
- cout << "析构函数被调用了" << endl;
- }
- };
- int main()
- {
- Person p;
- //可以发现,只有构造函数的语句被输出了,这是因为左侧的test01函数执行结束后将自动调用析构函数,打印语句。但是该例中Person p语句仅执行了构造函数,由于main函数结束后才能打印语句,因此在dos界面的我们是不一定可以看到的。
- //关闭dos界面的瞬间,将释放对象,调用析构,打印语句。
- return 0;
- }
按有无参数,构造函数可分为有参构造和无参构造(默认构造);按类型可分为普通构造和拷贝构造。
构造函数有三种调用方式:括号法,显示法和隐式转换法。
拷贝构造需要传入一个相同的数据类型的对象。
不要用括号法调用默认构造,会被编译器误解为函数的声明。
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- //普通构造
- //无参构造
- Person() {
- cout << "调用无参构造函数" << endl;
- }
- //有参构造(默认构造函数)
- Person(int a) {
- age = a;
- cout << "调用有参构造函数" << endl;
- }
-
- //拷贝构造
- Person(const Person& p) {
- age = p.age; //将传输的人的身上的所有属性拷贝到当前对象身上
- cout << "调用拷贝构造函数" << endl;
- }
- //析构函数
- ~Person()
- {
- cout << "调用析构函数" << endl;
- }
- private:
- int age;
- };
- void test01()
- {
- //括号法调用
- Person p1(10); //调用有参构造函数
-
- Person p2;
- Person p2(); //并不会创建对象,编译器会认为这是一个函数的声明
-
- Person p3(p2);//调用拷贝构造函数
- //显示法调用
- Person p1;
- Person p2 = Person(10);//显示法调用有参构造
- Person p2 = Person(p1);//显示法调用拷贝构造
-
- Person(10);//这是一个匿名对象,当前行执行结束后,系统会立即回收
- Person(p3);//不要利用拷贝构造函数初始化匿名对象,编译器会认为这是一个对象的声明
-
- //隐式转换法调用
- Person p4 = 10;//相当于Person p4= person(10);
- Person p5 = p4;//相当于拷贝构造
- }
那么,什么时候使用拷贝构造函数?
通常有三种情况:一是使用一个已经创建完毕的对象来初始化另一个对象;二是值传递的方式给函数参数传值;三是以值方式返回局部对象。
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- Person()
- {
- cout << "Person的默认构造函数调用" << endl;
- }
- Person(int age)
- {
- m_Age = age;
- cout << "Person的有参构造函数调用" << endl;
- }
- Person(const Person& p)
- {
- m_Age = p.m_Age;
- cout << "Person的拷贝构造函数调用" << endl;
- }
- ~Person()
- {
- cout << "Person的析构函数调用" << endl;
- }
- int m_Age;
- };
- //1、使用一个已经创建完毕的对象来初始化一个新对象
- void test01()
- {
- Person p1(20);
- Person p2(p1);
- }
- //2、以值传递的方式给函数参数传值
- void doWork(Person p) //由实参传往形参的时候,会有一个临时的拷贝
- {
-
- }
- void test02()
- {
- Person p;
- doWork(p);
- }
- //3、值的方式返回,返回的是一个拷贝
- Person doWork2()
- {
- Person p1; //并不会直接返回这个对象,返回的是这个对象的拷贝
- cout << (int*)&p1 << endl;
- return p1;
- }
- void test03()
- {
- Person p = doWork2();
- cout << (int*)&p << endl; //p的地址和p1的地址不一样
- }
- int main()
- {
- //test01();
- //test02();
- test03();
- return 0;
- }
默认情况下,C++会给一个类添加至少3个函数:默认构造函数(函数体为空),析构函数(函数体为空)和默认拷贝构造函数(对属性进行值拷贝)。如果用户谢了有参构造函数,则C++不再提供默认无参构造函数,但会提供默认拷贝构造函数。如果用户定义拷贝构造函数,C++不会再提供其他构造函数。
浅拷贝是简单的赋值拷贝操作,通过“=”号就可实现。深拷贝是指在堆区重新申请空间,进行拷贝操作。
例如当我们使用浅拷贝复制对象,如果被拷贝的对象中存在堆区数据,则在执行析构函数时会释放两次堆区的数据,带来异常,如下:
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- Person()
- {
- cout << "Person的默认构造函数调用" << endl;
- }
- Person(int age, int height)
- {
- m_Age = age;
- new int(height); //堆区开辟内存
- cout << "Person的有参构造函数调用" << endl;
- }
- ~Person()
- {
- if (m_Height != NULL) //释放堆区数据
- delete m_Height;
- m_Height = NULL; //防止出现野指针
- cout << "Person的析构构造函数调用" << endl;
- }
- int m_Age;
- int* m_Height; //将指针创建在堆区
- };
- void test01()
- {
- Person p1(18,160);
- cout << "p1的年龄为:" << p1.m_Age<<"身高为" <<p1.m_Height<< endl;
- Person p2(p1); //浅拷贝操作,把指针也拷贝过去了
- cout << "p2的年龄为:" << p2.m_Age<<"身高为" <<p2.m_Height<< endl;
- //p2执行析构函数时会释放一次堆区数据,p1也会执行一次。
- //因此使用浅拷贝,将会重复释放堆区内存。
- }
- int main()
- {
- test01();
- return 0;
- }
使用深拷贝可以解决上述问题。即从新找一块堆区空间存放new的数据。
- #include<iostream>
- using namespace std;
- class Person
- {
- public:
- Person()
- {
- cout << "Person的默认构造函数调用" << endl;
- }
- Person(int age, int height)
- {
- m_Age = age;
- new int(height); //堆区开辟内存
- cout << "Person的有参构造函数调用" << endl;
- }
- //自己定义拷贝构造函数,解决浅拷贝的问题
- Person(const Person& p)
- {
- cout << "Person拷贝构造函数的调用" << endl;
- m_Age = p.m_Age;
- //m_Height = p.m_Height; //编译器默认的浅拷贝操作
- m_Height = new int(*p.m_Height); //深拷贝
- }
- ~Person()
- {
- if (m_Height != NULL) //释放堆区数据
- delete m_Height;
- m_Height = NULL; //防止出现野指针
- cout << "Person的析构构造函数调用" << endl;
- }
- int m_Age;
- int* m_Height; //将指针创建在堆区
- };
- void test01()
- {
- Person p1(18,160);
- cout << "p1的年龄为:" << p1.m_Age<<"身高为" <<p1.m_Height<< endl;
- Person p2(p1); //浅拷贝操作,把指针也拷贝过去了
- cout << "p2的年龄为:" << p2.m_Age<<"身高为" <<p2.m_Height<< endl;
- //p2执行析构函数时会释放一次堆区数据,p1也会执行一次。
- //因此使用浅拷贝,将会重复释放堆区内存。
- }
- int main()
- {
- test01();
- return 0;
- }
C++类中的成员可以是另一个类中的对象,我们称该成员为对象成员。例如
clase A{}
clase B
{
A a;
}
B类中有对象A作为成员,因此A为对象成员,那么创建B对象时,A的构造会先于B。即当其他类的对象作为本类的数据成员时,构造的时候先构造其他类的对象,然后再构造自身。
对B进行析构时,B先析构,然后A再析构。即析构的顺序与构造相反。
- #include<iostream>
- #include<string>
- using namespace std;
- class Phone
- {
- public:
- string m_Pname;
- Phone(string pName)
- {
- m_Pname = pName;
- cout << "Phone的构造" << endl;
- }
- };
- class Person
- {
- public:
- Person(string name, string pName) :m_name(name), m_Phone(pName) {
- cout << "Person的构造" << endl;
- }
- //相当于Phone m_Phone=pName,隐式转换法
- string m_name;
- Phone m_Phone;
- };
- void test01()
- {
- Person p("Zhang", "iphone");
- cout << p.m_name << " has " << p.m_Phone.m_Pname << endl;
- }
- int main()
- {
- test01();
- return 0;
- }
在C++中,类内的成员变量和成员函数分开存储,只有非静态成员变量才属于类的对象。静态成员,非静态成员函数,静态成员函数都不在类的对象上。
成员变量和成员函数是分开存储的。
case 1:
空对象占用的内存空间为1个字节
原因:C++编译器会给每个空对象也分配1个字节的空间,是为了区分空对象占内存的位置。例如说连续两个空对象,它们需要占用不同的内存空间,因此需要一个字节来区分。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
-
- };
- void test01()
- {
- Person p;
- cout << "size of p = " << sizeof(p) << endl;
- }
- int main()
- {
- test01();
- return 0;
- }
case 2:
对象将占用4个字节,当不是空的时,就按照int来分配4个字节的内存。
也就是说,非静态成员属于类的对象的内存。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- int m_A;//非静态成员变量
- };
- void test02()
- {
- Person p;
- cout << "size of p = " << sizeof(p) << endl;
- }
- int main()
- {
- test02();
- return 0;
- }
case 3:
对象仍然占用4个字节,说明静态成员变量不在类的对象上。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- int m_A;//非静态成员变量
- static int m_B;//静态成员变量
- };
- int Person::m_B = 10; //静态数据成员只能在类外进行初始化
- void test02()
- {
- Person p;
- cout << "size of p = " << sizeof(p) << endl;
- }
- int main()
- {
- test02();
- return 0;
- }
case 4:
仍然占用4个字节,说明非静态成员函数也不再类的对象上。
静态成员函数更不会在对象上。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- int m_A;//非静态成员变量
- static int m_B;//静态成员变量
- void func();//非静态成员函数
- };
- int Person::m_B = 10;
- void test02()
- {
- Person p;
- cout << "size of p = " << sizeof(p) << endl;
- }
- int main()
- {
- test02();
- return 0;
- }
C++成员变量和成员函数是分开存储的。每一个非静态成员函数只会产生一份函数实例,也就是说多个同类型的对象会公用一块代码。
C++通过提供特殊的对象指针,即this指针来区分成员函数被哪些对象调用。this指针指向被调用的成员函数所属的对象。即如果对象p1调用func(),则this指针指向p1;如果对象p2调用func(),则this指针指向p2。
this指针是隐含每一个非静态成员函数内,不需要定义。
This指针的作用:1、当函数的形参和成员变量同名时,可以用this来区分;2、在类的非静态成员函数中返回对象本身,可用return *this。
this指针作用1:解决名称冲突
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- public:
- Person(int age)
- {
- //age = age;
- this->age=age;//this指针指向的是被调用的成员函数所属的对象,此时构造函数的this指向p1。
- }
- int age; //成员名称和传入的形参名称应该不同
- };
- void test01()
- {
- Person p1(18);
- cout << "age of p1 is" << p1.age << endl;
- }
- int main()
- {
- test01();
- return 0;
- }
this指针作用2:
返回对象本身用*this(链式编程思想)
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- public:
- Person(int age)
- {
- this->age = age;
- }
- Person PersonAddAge(Person& p)
- {
- this->age += p.age; //把对象p的年龄加到自己上面来
- //this是一个指向p2的指针,*this就是p2这个对象的本体,因此函数返回值应该是一个引用
- return *this;//指向本体
- }
- int age;
-
- };
- void test02()
- {
- Person p1(10);
- Person p2(10);
- //p2.PersonAddAge(p1);
- //如果我想多加几次呢?
- p2.PersonAddAge(p1).PersonAddAge(p1);
- cout << "age of p2 is " << p2.age << endl;
- }
- int main()
- {
- test02();
- return 0;
- }
C++的空指针也可以调用成员函数,但是要注意有没有用到this指针。如果用到this指针,需要加以判断以保证代码正确性。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- public:
- void showClassName()
- {
- cout << "this is Person class" << endl;
- }
- void showPersonAge()
- {
- cout << "age = " << m_Age << endl;
- //该句等同于:cout << "age = " <<this-> m_Age << endl;
- }
- int m_Age;
- };
- void test01()
- {
- Person* p = NULL;
- p->showClassName();
- p->showPersonAge();//由于this没有指向确切的对象,因此用this指针访问数据成员出错
- //即如果我们用this指针时,不能用空指针
- }
- int main()
- {
- test01();
- return 0;
- }