在自己电脑上建设网站模板云网站建设
- 作者: 五速梦信息网
- 时间: 2026年04月20日 06:51
当前位置: 首页 > news >正文
在自己电脑上建设网站,模板云网站建设,系统网站建设,特产网站建设方案面向过程/面向对象 C语言是面向过程#xff0c;关注过程#xff0c;分析出求解问题的步骤#xff0c;通过函数调用逐步解决问题 C是基于面对对象的#xff0c;关注的是对象——将一件事拆分成不同的对象#xff0c;依靠对象之间的交互完成 引入 C语言中结构体只能定义…面向过程/面向对象 C语言是面向过程关注过程分析出求解问题的步骤通过函数调用逐步解决问题 C是基于面对对象的关注的是对象——将一件事拆分成不同的对象依靠对象之间的交互完成 引入 C语言中结构体只能定义变量但是在C中结构体不仅可以定义变量也能定义函数 以栈为例,C里对于栈的函数可以这样子写: //C struct Stack {//成员变量int* a;int top;int capacity;//成员函数——直接将函数与成员变量放在一起定义void Init(){a nullptr;top capacity 0;}void Push(int x){} };//C void StackInit(struct Stack* ps) {} void StackPush(struct Stack* ps,int x) {} 类的定义 class className {//类体由成员函数和成员变量组成};//有分号 类体中内容称为类的成员类中的变量称为类的属性或成员变量; 类中的函数称为类的方法或者 成员函数。 两种定义方式 1.声明和定义全部放在类体中 class Date { public:void Init(int year, int month, int day){//域搜索一个是类域的year一个是函数局部域的year//所以需要加上_来区分_year year;_month month;_day day;} private:int _year;//属于声明int _month;int _day; }; 2.类声明放在.h中成员函数定义在.cpp文件中且成员函数名前需要加类名 //.h class Stack { public://成员函数void Init();void Push(int x);int Top(); private://访问限定符访问到下一个访问限定符或者到函数末尾才会结束//这样在主函数里就无法访问int* a;int top;int capacity; };//.cpp void Stack::Init()//全局函数需要在其他文件里去找,而不是到类域里去找 {a nullptr;top capacity 0; } void Stack::Push(int x)//需要有访问符 {if (top capacity){size_t newcapacity capacity 0 ? 4 : capacity * 2;a (int*)realloc(a, sizeof(int) * newcapacity);capacity newcapacity;}a[top] x; } int Stack::Top() {return a[top - 1]; } 访问限定符 C实现封装的方式用类将对象的属性与方法结合在一块让对象更加完善通过访问权限选 择性的将其接口提供给外部的用户使用。 说明
- public修饰的成员在类外可以直接被访问
- protected和private修饰的成员在类外不能直接被访问(此处protected和private是类似的) 3. 访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止
- 如果后面没有访问限定符作用域就到 } 即类结束。
- class的默认访问权限为privatestruct为public(因为struct要兼容C)
访问
以Date类为例
class Date
{
public:void Init(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout _year ;cout _month ;cout _day ;cout endl;}
//private:int _year;//因为此处是声明不是定义//声明特点只有一个躯体不能访问没有分配空间int _month;int _day;
};
1.由于private中对于成员变量是声明而不是定义——故不能直接访问需要先建立个对象
int main()
{Date d;d._year1//errreturn 0;
} 无法访问——因为类中_year被private访问限定符限制住了所以不能在类外访问 如何解决——将 private 注释掉 2.类外调用函数 若是想调用函数,首先就需要定义一个对象d——然后在用d.函数名()去访问 例如此处的打印函数想打印类的内容 int main() {Date d;d1.Init(2022,2,22);//先初始化再打印d1.Print();return 0; } 类的大小(存储方式) 同理我们对于日期类Date进行讨论 class Date { public:void Init(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout _year ;cout _month ;cout _day ;cout endl;} private:int _year;int _month;int _day; }; 进行大小的打印 int main() {Date d1;coutsizeof(d1)endl;coutsizeof(Date)endl;return 0; } 类的大小仅取决于成员变量成员函数存放在公共代码区中不会计入类的大小仅对象的成员变量会算入大小 若成员变量不为同类型呢 class A4 { public:void f1() {}private:char _c;int _a; }; 为8——同理按照编译器内存对齐的规律来进行的 类的实例化 用类类型创建对象的过程称为类的实例化 - 类是对对象进行描述的是一个模型一样的东西限定了类有哪些成员定义出一个类并没有分配实际的内存空间来存储它
- 一个类可以实例化出多个对象实例化出的对象才占用实际的物理空间存储类成员变量 int main() {Date._year1;//errreturn 0; } this指针 现在对于一个日期的类Date class Date { public:void Init(int year,int month,int day){_year year;_month month;_day day;}void Print(){cout _year / _month / _day endl;} private:int _year;//声明不是定义int _month;int _day; };int main() {Date d1;d1.Init(2022,3,15);Date d2;d2.Init(2023,3,15);return 0; } 对于Init 和 Print 函数看起来分别有3个和0个参数但事实并非如此——分别有4个和1个为什么 这两个函数内部还有一个隐含参数——this指针会将函数进行一个修改 同理对于主函数里的初始化函数 建立联系 所以函数可以写成 void Init(int year,int month,int day){this-_year year;this-_month month;this-_day day;}void Print(){cout this-_year / this-_month / this-_day endl;} 注意 this指针在实参和形参位置不能写但是在类里可以直接写出来使用但没必要-指向当前对象 若在函数里嵌套另一个成员函数呢指向两个Print函数的this指针是否一样 void Init(int year,int month,int day){this-_year year;this-_month month;this-_day day;Print();}void Print(){cout this-_year / this-_month / this-_day endl;} 不一样——一个对象中两个this指针是各自的不同的指针变量但是指向的地方是一样的。 若是两个不同的对象那么这两个this指针不仅不同指向的空间也不同 特性
- this指针的类型类类型* const即成员函数中不能给this指针赋值。
- 只能在“成员函数”的内部使用
- this指针本质上是“成员函数”的形参当对象调用成员函数时将对象地址作为实参传递给 this形参。所以对象中不存储this指针。
- this指针是“成员函数”第一个隐含的指针形参一般情况由编译器通过ecx寄存器自动传 递不需要用户传递 两个例题 1 有以下代码 // 1.下面程序编译运行结果是 A、编译报错 B、运行崩溃 C、正常运行 class A { public:void Print(){cout Print() endl;} private:int _a; }; int main() {A* p nullptr;p-Print();return 0; }由于定义了一个对象p,但是为空指针要去调用成员函数——需要在对象里找吗 不成员函数定义在公共函数区域里所以可以直接调用 2 // 1.下面程序编译运行结果是 A、编译报错 B、运行崩溃 C、正常运行 class A { public:void PrintA() {cout_aendl;} private:int _a; }; int main() {A* p nullptr;p-PrintA();return 0; }此处运行崩溃——因为p是空指针且要访问_a需要用到this指针去访问this-_a 但此处this指针为空所以会导致运行崩溃 综上类的访问都是先传递this指针再去找到函数的地址并且使用函数 this指针指向的是需要作用的对象 类的6个默认成员函数 若有 class Date{}; 那么这个类成为空类——但是这个类里什么都没有吗 实际上编译器会自动生成以下6个默认成员函数 构造函数 构造函数负责初始化函数名就是类名 特征
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
class Date
{
public://正常初始化void Init(int year, int month, int day){_year year;_month month;_day day;}//构造函数Date()//初始化{_year1;_month1;_day1;}Date(int year, int month, int day)//直接输入,用缺省参数{_year year;_month month;_day day;}
private:int _year;int _month;int _day;
}; 为了方便我们可以直接将两个构造函数合并 Date(int year1, int month1, int day1)//直接输入,用全缺省参数减少了代码量{_year year;_month month;_day day;} 且考虑到其他函数内部会没有定义的值所以可以在声明成员变量的时候给予其缺省值 故类可以修改为 class Date { public:Date(int year1, int month1, int day1)//直接输入,用全缺省参数减少了代码量{cout Date(int year1, int month1, int day1) endl;_year year;_month month;_day day;}void Print()//实际上为1个参数{cout _year / _month / _day endl;//cout this endl;} private:int _year;int _month;int _day; }; 如果用户显式定义了构造函数那么编译器就不会生成默认的构造函数 构造函数的出现减少了代码量 int main() { //非构造Date d1;d1.Init(2022,2,22);Date d2(2022,2,22);return 0; } 一行代码就能实现对象的创建和初始化 注意 不实现构造函数的情况下编译器会生成默认的构造函数虽然日期类Date对象d调用了默认构造函数但是其成员变量_year/_month/_day仍然为随机值这是为什么 ——C把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类 型如int/char…自定义类型就是我们使用class/struct/union等自己定义的类型。 因此为了解决对于内置类型成员不初始化的缺陷我们可以在成员变量声明时给予默认值 private:int _year1;int _month1;int _day1; 析构函数 与构造函数相反析构函数是用于对于对象中资源的清理 特征 - 析构函数名是在类名前加上字符 ~。
- 无参数无返回值类型。
- 一个类只能有一个析构函数。若未显式定义系统会自动生成默认的析构函数。注意析构 函数不能重载
- 对象生命周期结束时C编译系统系统自动调用析构函数。 同理以Date为例 class Date { public:Date(int year1, int month1, int day1)//直接输入,用全缺省参数减少了代码量{cout Date(int year1, int month1, int day1) endl;_year year;_month month;_day day;}void Print()//实际上为1个参数{cout _year / _month / _day endl;//cout this endl;}~Date(){cout ~Date() endl;} private:int _year1;//声明默认会用给的缺省值进行初始化内置类型不作处理int _month1;//作为缺省值会补充其他函数里没有定义的值int _day1; }; 若不写析构函数那么编译器就会默认生成析构函数来进行资源的清理 以栈来展现构造和析构函数 class Stack { public:/Stack(){a nullptr;top capacity 0;}/Stack(size_t n 4)//不传入值则就默认4个空间{cout Stack(size_t n 4) endl;if (n 0){a nullptr;top capacity 0;}else{a (int*)malloc(sizeof(int) * n);if (a nullptr){perror(realloc fail);exit(-1);}top 0;capacity n;}}~Stack(){cout ~Stack() endl;free(a);a nullptr;top capacity 0;}void Push(int x){if (top capacity){size_t newcapacity capacity 0 ? 4 : capacity * 2;int* tmp (int*)realloc(a, sizeof(int) * newcapacity);if (tmp nullptr){perror(realloc fail);exit(-1);}if (tmp a){cout capacity 原地扩容 endl;//原地扩容还是异地扩容呢?//若打印就原地否则就异地}else{cout capacity 异地扩容 endl;}a tmp;capacity newcapacity;}a[top] x;}int Top(){return a[top - 1];} void Pop(){assert(top 0);–top;}void Destroy(){free(a);a nullptr;top capacity 0;}bool Empty(){if (top 0)return 1;return 0;} private:int* a;int top;int capacity; }; 在构造函数处我们选择用了缺省参数这样可以随机初始化自己想要的栈的大小 int main() {Stack st1;Stack st2(10);return 0; } 那么对于类中这种定义的函数我们可以这样子调用 int main() { Stack st2(10);st2.Push(1);st2.Push(2);st2.Push(3);st2.Push(4);st2.Push(5);st2.Push(6);st2.Push(7);st2.Push(8);st2.Push(9);st2.Push(10);while (!st2.Empty()){cout st2.Top() ;st2.Pop();}cout endl;return 0; } 也可以直接传入1000的量这样子无需要扩容 Stack st2(1000); for (size_t i 0; i 1000; i){st2.push(i);} 注意 1.会自动构造 2.后定义先析构若没有写析构函数那么编译器就会默认生成一个析构函数 3.内置类型成员不处理但是会自定义类型成员会调用在这个成员的析构函数 但是栈里一定要写析构函数——析构函数是浅处理就是处理像int /char 这种普通类型的变量随着函数的结束就会销毁但是栈里有定义——int* a,也就是数组,需要手动处理不然会导致内存泄漏的问题 特殊 class MyQueue { private:Stack _pushst;//初始化的时候已经生成Stack _popst;}; 对于此类尽管有int* a指针定义也需要写——因为定义Stack的时候就已经生成了相关的构造和析构函数此时就不需要我们去写 除此之外构造函数和析构函数的调用就类似于栈先构造的最后析构 拷贝构造函数 拷贝构造函数只有单个形参该形参是对本类类型对象的引用(一般常用const修饰)在用已存 在的类类型对象创建新对象时由编译器自动调用。 特征
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须是类类型对象的引用使用传值方式编译器直接报错 因为会引发无穷递归调用
无穷递归错误情况代码
class Date
{
public:Date(Date d) // 错误写法编译报错会引发无穷递归{_year d._year;_month d._month;_day d._day;}
private:int _year;int _month;int _day;
};同理在自定义函数里也不能直接传值传入
void func2(Stack s)
{s.Push(1);s.Push(2);
}
这里直接传参的话那么就是用形参s接收一个对象此时对象和形参都指向一个空间那么函数结束后形参指向的空间会进行销毁那么原本对象指向的空间将会不存在会导致报错析构两次 那怎么解决——用引用获取对象的地址 改为如下
class Date
{
public:Date(Date d) // 错误写法编译报错会引发无穷递归{_year d._year;_month d._month;_day d._day;}
private:int _year;int _month;int _day;
};void func2(Stack s)
{s.Push(1);s.Push(2);
}
如何定义
——有 () 也有 两种形式
int main()
{Date d1(2022,2,22);Date d2(d1);Date d3d1;d1.Print();d2.Print();d3.Print();return 0;
} 先构造d1然后直接将d1拷贝构造给d2和d3,最后再逐一析构
若不写拷贝构造函数呢
—— 编译器会自己生成
class Date
{
public://Date(Date d) //{// _year d._year;//_month d._month;//_day d._day;}
private:int _year;int _month;int _day;
};int main()
{Date d1(2022,2,22);Date d2d1;//passreturn 0;
} 但是对于栈若不写拷贝构造函数——则会报错err
Stack s1;
Stack s2s1;所以对于栈一定要进行自定义拷贝函数
Stack(const Stack s)//用const防止代码赋值写反导致改变了原对象的内容{a (int*)malloc(sizeof(int) * s.capacity);if (a NULL){perror(malloc申请失败);return;}memcpy(a, s.a, sizeof(int) * s.top);capacity s.capacity;top s.top;}
总结
1.内置类型,值拷贝 2.自定义类型调用他的拷贝 总结日期类Date 不需要我们自己实现拷贝构造默认生成就可以用 栈类Stack 需要我们自己实现深拷贝的拷贝构造默认生成会出现问题 因为Stack需要释放空间防止栈溢出内存泄漏但是Date里的变量会随着空间的销毁而销毁。
运算符重载
C为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数也具有其 返回值类型函数名字以及参数列表其返回值类型与参数列表与普通的函数类似。
函数原型返回值类型 operator操作符(参数列表)
再次以日期类Date为例
实现日期类的比较——两个不同日期的大小区分
全局
bool operator(const Date d1, const Date d2)//用一个函数去比较,有别名接收
{//1.访问对象将private注释掉if (d1._year d2._year){return true;}else if (d1._year d2._year d1._month d2._month){return true;}else if (d1._year d2._year d1._month d2._month d1._day d2._day){return true;}elsereturn false;
}
此处需要用到拷贝构造函数用两个形参承载并且进行比较此时不能用private进行限定
如果写入类里面呢
注意
1.不能通过连接其他符号来创建新的操作符比如operator
2.重载操作符必须有一个类类型参数
3.用于内置类型的运算符其含义不能改变例如内置的整型不 能改变其含义
4.作为类成员函数重载时其形参看起来比操作数数目少1因为成员函数的第一个参数为隐 藏的this
5. . :: sizeof ?: .这五个运算符不能重载
类内部定义运算符重载函数
bool operator(const Date d)//用一个函数去比较,用别名接收,参数个数为运算数-1{//1.访问对象将private注释掉if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}elsereturn false;}
在主函数里调用
int main()
{//自定义类型怎么比较大小——本身不支持比较需要通过运算符重载才可以进行比较Date d1(2023, 7, 21);Date d2(2022, 8, 21);cout (d1 d2) endl;cout (operator(d1, d2)) endl;cout (d1.operator(d2)) endl;return 0;
}
需要注意的是,左操作数是this,指向调用函数的对象
bool operator(Date this,const Date d)
实现日期是否相同
bool operator(const Date d)//用一个函数去比较,用别名接收{//1.访问对象将private注释掉if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}elsereturn false;}
bool operator(const Date d)
{return _year d._year _month d._month _day d._day;
}
this指针的复用
对于判断日期是否大于可以通过对两个函数的复用来实现
bool operator(const Date d)//此时d1作为operatorthis指向d1然后d2作为d{return *this d || *this d;//直接用this指针实现复用其它函数
}bool operator(const Date d)
{return !(*this d);
}
日期天数的改变
因位需要改变天数也就是在原对象上进行数据修改所以要拷贝构造
int GetMonthDay(int year, int month)
{int monthArray[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month2)(year % 4 0 year % 100 ! 0)||(year%4000)){return 29;}return monthArray[month];
}Date operator(int day)//传值返回需要用拷贝构造所以用引用返回
{_day day;while (_dayGetMonthDay(_day,_month)){_day - GetMonthDay(_day, _month);_month;if (_month 13){_year;_month 1;}}return *this;//此时要返回一个日期日期存在于this指针里所以要返回*this
}//出了作用域this指针仍存在所以可以使用引用符号
日期天数的增加
由于仅进行天数的增加然后拷贝给别人所以需要先拷贝构造一个载体tmp然后对tmp进行修改最后返回载体这样子原对象的内容就得到了保证
Date operator(int day)
{Date tmp(*this);//进行对this指针的拷贝构造,需要一个载体tmp day; //this指向的就是d1里的数据,此时tmp就是this,可以实现复用//用的运算符重载函数return tmp;
}
Date ret1 d1 50;
ret1.Print();
d1.Print();//由于是所以d1也会被改变Date retd150;//赋值一个日期实际上也就是一个拷贝构造
ret.Print();
d1.Print();实现类中成员函数的声明与定义分离
以日期类Date为例子
ps:若实现分离那么缺省值由声明给定义不能有缺省值
//Date.h
class Date
{
public:Date(int year 1, int month 1, int day 1);//必须有缺省参数//为什么定义和声明不能分开——因为要先找到声明再找到定义类中会出现重定义//若想实现分开那么就让声明给出缺省值定义不用给void Print();int GetMonthDay(int year, int month);//连续赋值—返回指针Date operator(const Date d);//实际上有两个参数第一个参数就是this d1,表示作用的对象但是被隐藏了//只读函数可以加const内部不涉及修改生成—是否const都可以调用//若调用了定义里也要加上constbool operator(const Date d);bool operator(const Date d);bool operator(const Date d);bool operator(const Date d);bool operator!(const Date d);Date operator(int day);//用引用返回Date operator(int day);
private:int _year;int _month;int _day;
};//Date.cpp 定义里需要用到访问符 ::
int Date::GetMonthDay(int year, int month)//如果用这个函数时会改变其内容那么就需要用const修饰
{const static int monthArray[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month 2) (year % 4 0 year % 100 ! 0) || (year % 400 0)){return 29;}return monthArray[month];
}//Date::Date(int year 1, int month 1, int day 1)//err
Date::Date(int year, int month, int day)//pass
{_year year;_month month;_day day;if (month 1 || month12 || day1 || dayGetMonthDay(year, month)){cout 非法日期 endl;return;}}
//连续赋值—返回指针
Date Date::operator(const Date d)//实际上有两个参数第一个参数就是this* d1,表示作用的对象但是被隐藏了
{if (this ! d){this-_year d._year;this-_month d._month;this-_day d._day;}return *this;//this这里指向的是d1因为作用的对象就是d1,出了作用域this还在所以能用引用返回//自定义类型不能传值拷贝需要调用一个拷贝构造
}bool Date::operator(const Date d) //用一个函数去比较,用别名接收
{//1.访问对象将private注释掉if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}elsereturn false;
}
bool Date::operator(const Date d)
{return _year d._year _month d._month _day d._day;
}bool Date::operator(const Date d) const //此时d1作为operatorthis指向d1然后d2作为d
//再通过d1d2 和d1d2的判断 来得出的结果
{return *this d || *this d;//直接用this指针实现复用其它函数
}bool Date::operator(const Date d)
{return !(*this d); }bool Date::operator!(const Date d) {return !(*this d); }Date Date::operator(int day) {if (day 0){return *this - (-day);}_day day;while (_day GetMonthDay(_day, _month)){_day - GetMonthDay(_day, _month);_month;if (_month 13){_year;_month 1;}}return *this;//此时要返回一个日期日期存在于this指针里所以要返回*this }Date Date::operator(int day) //若不想改变原内容那么就需要一个变量来承载d1的this指针然后改变变量返回载体 {Date tmp(*this);//Date tmp *this;tmp day;return tmp; }赋值运算符重载 1.赋值运算符重载格式 参数类型const T传递引用可以提高传参效率 返回值类型T返回引用可以提高返回的效率有返回值目的是为了支持连续赋值 检测是否自己给自己赋值 返回this 要复合连续赋值的含义 倘若在类中进行了拷贝不想原对象被改变可以在定义的时候给予const进行修饰 Date (const Date d);//拷贝构造 bool operator(const Date d); bool operator(const Date d); bool operator(const Date d); bool operator(const Date d); 2.const重载 倘若定义对象的时候不想对象被改变那么就会有以下的代码 void Print();void TestDate2() {const Date d1(2023, 7, 28);//若在Print函数里就是权限的放大d1.Print(); } 但是此时Print函数只能兼容普通类型那怎么办——仅需要加个关键字 void Print() const;//于此同时定义也需要改变//void Date::Print(const Date this) void Date::Print() const//在结尾加上const这样可以自由实现权限的缩小 {cout _year 年 _month月_day日 endl; }那么为了代码的严谨并且保护原对象那么对于只读函数都可以加上const来修饰 bool operator(const Date d)const;bool operator(const Date d)const;bool operator(const Date d)const;bool operator(const Date d)const;bool operator!(const Date d)const; 运算符赋值 我们知道有拷贝构造那么对于运算符应该也能实现两个对象之间的拷贝 void operator(const Date d)//实际上有两个参数第一个参数就是this* d1,表示作用的对象但是被隐藏了 {//赋值拷贝//此处用不用? —都行为什么不会无穷递归——此处直接传入值不会再进行多余的拷贝构造//为了方便默认都用引用this-_year d._year;this-_month d._month;this-_day d._day; } 但是若是想实现多次赋值呢例如这样的 int main() {int i,j,k;k10;ijk;return 0; } 那么我们需要一个返回值也就是需要返回一个拷贝才能实现多次赋值 Date operator(const Date d)//实际上有两个参数第一个参数就是this* d1,表示作用的对象但是被隐藏了 {if (this ! d){this-_year d._year;this-_month d._month;this-_day d._day;}return *this;//this这里指向的是d1因为作用的对象就是d1,出了作用域this还在所以能用引用返回//自定义类型不能传值拷贝需要调用一个拷贝构造 } 赋值重载和拷贝构造的区别 赋值两个已经存在的对象进行拷贝 拷贝构造一个已经存在对象去初始化另一个要创建的对象 前置和后置重载 在类里由于都有操作符所以为了区分我们用 int 类型来区分 Date operator();//前置Date Date::operator()//改变自身,所以直接作用于this指针 {*this 1;return *this; }Date operator(int);//后置Date Date::operator(int)//仅对变量赋值作改变,所以需要用一个载体来进行数据的改变 {Date tmp(*this);this 1;return tmp; }顺序表类的实现及使用 struct SeqList { public:void PushBack(int x){_a[_size] x;}void CheckCapacity(){;}size_t size() const{return _size;}//打印void Print(){for (int i 0; i _size; i){cout _a[i] endl;}}//读const int operator const //operator作用于类的对象{assert(i _size);return _a[i];}//写int operator//operator作用于类的对象{assert(i _size);return _a[i];} private:int _a(int*)malloc(sizeof(int)*10);int _size0;int _capacity0; }; 在C语言中我们实现一个顺序表数据的放入会及其麻烦但是在C中有了类那么就可以极大的简化代码 int main() {SeqList st;st.PushBack(1);st.PushBack(2);st.PushBack(3);st.PushBack(4);return 0; } 访问——可以通过运算符重载实现 for (size_t i 0; i st.size(); i) {cout st[i] ;//用运算符重载这样不仅实现了特定位置数据的返回还可以进行遍历cout st.operator endl; } 通过流输出打印对象的内容 ostream operator(ostream out, const Date d)//类的流插入 {out d._year / d._month / d._day endl;return out; } 流输入输入对象的内容 istream operator(istream in, Date d) {in d._year d._month d._day;return in; } 以上两个不做介绍需要用到out相关类的知识
相关文章
-
在中国建设工程造价管理协会网站网站内容管理系统使用说明书
在中国建设工程造价管理协会网站网站内容管理系统使用说明书
- 技术栈
- 2026年04月20日
-
在郑州网站推广权威发布公众号封面图片
在郑州网站推广权威发布公众号封面图片
- 技术栈
- 2026年04月20日
-
在域名上建设网站哈尔滨做网站哪家好
在域名上建设网站哈尔滨做网站哪家好
- 技术栈
- 2026年04月20日
-
在自己网站上做销售在工商要办什么手续旅游网站开发工程师
在自己网站上做销售在工商要办什么手续旅游网站开发工程师
- 技术栈
- 2026年04月20日
-
赞叹天河网站建设公司怎么做存储网站
赞叹天河网站建设公司怎么做存储网站
- 技术栈
- 2026年04月20日
-
早教网站建设方案建设部网站监理工程师查询
早教网站建设方案建设部网站监理工程师查询
- 技术栈
- 2026年04月20日
