构造函数和初始化表

📅 2026/7/18 10:17:23
构造函数和初始化表
构造函数构造函数的特点函数名必须与类名相同且没有返回值类型构造函数调用时间在定义对象同时自动被调用且仅被调用一次对象定义语句new操作符构造函数的作用定义对象的各个成员变量并赋初值。设置对象的初始状态在对象定义之初想实现的任何操作对象定义过程1. 为整个对象分配内存空间2.以构造实参调用构造函数--定义成员变量--执行构造代码代码演示// 构造函数:函数名必须与类名相同,没有返回值类型 #include iostream #include cstring using namespace std; class Human { public: Human( /* Human* this */ int age0, const char* name无名 ) { // 在this所指向的内存空间中 定义m_age,初值为随机数 // 在this所指向的内存空间中 定义m_name,初值为随机数 cout Human类的构造函数被调用 endl; m_age age; strcpy( m_name, name ); } // void setinfo( /* Human* this */ int age0, const char* name无名 ) { // this-m_age age; // strcpy( this-m_name, name ); // } void getinfo( /* Human* this */ ) { cout 姓名: this-m_name , 年龄: this-m_age endl; } private: int m_age; // 声明 char m_name[256]; // 声明 }; // 以上的代码模拟类的设计者(C标准库提供,第三方提供,自己设计的) // ------------------------------------------------ // 以下的代码模拟类的使用者 int main( void ) { Human h(22,张飞); // 定义h(给h分配内存空间),利用h.Human(22,张飞) cout h对象创建完毕 endl; // h.setinfo( 22, 张飞 ); h.getinfo(); return 0; }声明和定义的分开实现Human.h#ifndef __HUMAN_H__ #define __HUMAN_H__ class Human{ public: Human(int age 0,const char* name 无名); void getInfo(); private: int m_age; char m_name[256]; }; #endifHuman.cpp#include Human.h #includeiostream #includecstring using namespace std; Human::Human(int age,const char* name ){ int m_age age; strcpy(m_name,name); } void Human::getInfo(){ cout 姓名: m_name 年龄: m_age endl; }main.cpp#include Human.h int main( void ) { Human h(22,张飞); // 定义h(给h分配内存空间),利用h.Human(22,张飞) h.getInfo(); return 0; }定义对象的11种方法// 定义对象的 11 种方法 #include iostream #include cstring using namespace std; class Human { public: Human( /* Human* this */ int age0, const char* name无名 );//声明 void getinfo( /* Human* this */ ); // 声明 private: int m_age; // 声明 char m_name[256]; // 声明 }; Human::Human( /* Human* this */ int age, const char* name ) {//定义 // 在this所指向的内存空间中 定义m_age,初值为随机数 // 在this所指向的内存空间中 定义m_name,初值为随机数 cout Human类的构造函数被调用 endl; m_age age; strcpy( m_name, name ); } void Human::getinfo( /* Human* this */ ) { // 定义 cout 姓名: m_name , 年龄: m_age endl; } // 以上的代码模拟类的设计者(C标准库提供,第三方提供,自己设计的) // ------------------------------------------------ // 以下的代码模拟类的使用者 int main( void ) { Human(32,马超).getinfo(); //定义 匿名Human类对象,利用 匿名Human类对象.Human(..) Human h(22,张飞); // 定义h(给h分配内存空间),利用h.Human(22,张飞) h.getinfo(); Human h2; // 定义h2,利用h2.Human() h2.getinfo(); Human h3[3]; // 定义了3个Human类对象,并分别利用这3个Human类对象.Human() for( int i0; i3; i ) { h3[i].getinfo(); } Human h4[3] { Human(22,张飞), Human(20,赵云), Human(25,关羽) }; for( int i0; i3; i ) { h4[i].getinfo(); } Human h5[]{Human(22,张飞), Human(20,赵云), Human(25,关羽)}; for( int i0; isizeof(h5)/sizeof(h5[0]); i ) { h5[i].getinfo(); } Human* phnew Human; // 定义Human类堆对象,利用Human类堆对象.Human() (*ph).getinfo(); delete ph; ph NULL; Human* ph2new Human(); // 定义Human类堆对象,利用Human类堆对象.Human() (*ph2).getinfo(); delete ph2; ph2 NULL; Human* ph3 new Human(45,黄忠); //定义Human类堆对象,利用Human类堆对象.Human(45,黄忠) (*ph3).getinfo(); delete ph3; ph3 NULL; Human* ph4 new Human[3]; //定义了3个Human类堆对象,分别利用这3个Human类堆对象.Human() for( int i0; i3; i ) { ph4[i].getinfo(); } delete[] ph4; ph4 NULL; Human *ph5 new Human[3]{ Human(18,武松), Human(19,李逵), Human(20,鲁达)}; for( int i0; i3; i ) { ph5[i].getinfo(); } delete[] ph5; ph5 NULL; return 0; }string类代码演示#include iostream using namespace std; // C标准库设计的string类 // 以上的代码模拟类的设计者(C标准库提供,第三方提供,自己设计的) // ------------------------------------------------ // 以下的代码模拟类的使用者 int main( void ) { string s1(hello); //定义s1,利用s1.string(hello)-s1维护的字符串为hello cout s1: s1 endl; // 如果在做初始化,并且两边类型完全一致,那么xxx和(xxx)无差别 string s2 s1; //(s1) //定义s2,利用s2.string(s1)-s2维护的字符串 和 s1维护的字符串 内容相同 cout s2: s2 endl; string s3; // 定义s3,利用s3.string()-s3维护的字符串为\0 cout s3: s3 endl; // 如果在做赋值,并且两边类型完全一致,将触发operator函数的调用 s3 s2; // s3.operator(s2)--s3维护的字符串 和 s2维护的字符串 内容相同 cout s3: s3 endl; // 无论初始化还是赋值,只要两边类型不一致,首先都要类型转换 string s4hello; // 定义 匿名string类对象,利用 匿名string类对象.string(hello)-匿名string类对象维护的字符串为hello // string s4 匿名string类对象;--s4维护的字符串 和 匿名string类对象维护的字符串 内容相同 // --- s4维护的字符串为hello string s5; s5 hello; //定义 匿名string类对象,利用 匿名string类对象.string(hello)-匿名stri ng类对象维护的字符串为hello //s5匿名string类对象;--s5维护的字符串 和 匿名string类对象维护的字符串内容相同 //---s5维护的字符串为hello return 0; }