Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by Class MyQueue
初识C++ · 类和对象(下)-CSDN博客
目录 1 再谈构造函数 先看一段代码: class Date { public : Date(int year, int month, int day) { _year = year; _month = month; _day = day; } private: int _year; int _month; int _day; }; int main() { Date d1; return 0; } 当我们生成解决方案的时候,系统会报错:没有默认的构造函数,因为我们显式调用了构造函数,也没有默认构造,我们没有给缺省值,参数也没有缺省值,调用的时候就会报错。 C++引入了一个概念叫做初始化列表,以冒号开始,逗号分割,括号给值: Date() :_year(2024) ,_month(4) ,_day(30) {} 也就是我们将构造函数写成这样,这样我们不传参也是可以成功的。 可能看起来没什么用? class Stack { public: Stack(int n) :_size(10) , _capacity(n) , arr(nullptr) { //…… } private:...
【C++】:类和对象(下)-CSDN博客
一,再谈构造函数 这个语法其实是对前面的构造函数的补充。最重要的是初始化列表的使用,及其特性,隐式类型转换,还有当前阶段需要了解的 explicit 关键字。 初始化列表:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个"成员变量"后面跟一个放在括号中的初始值或表达式。 1.1 初始化列表的使用 当我们实现一个MyQueue(用两个栈模拟一个队列)时,如果栈不具备默认构造,MyQueue也无法生成默认构造,此时MyQueue要显示实现默认构造。 如何实现呢?必须用初始化列表。 初始化列表的本质:可以理解为每个对象中成员定义的地方。 typedef int DataType; class Stack { public: Stack(size_t capacity ) { _array = (DataType*)malloc(sizeof(DataType) * capacity); if (NULL == _array) { perror("malloc申请空间失败!!!"); return; } _capacity = capacity; _size = 0; }...
c++初阶------类和对象(六大默认构造函数的揭破)-CSDN博客
作者前言 🎂 ✨✨✨✨✨✨🍧🍧🍧🍧🍧🍧🍧🎂 🎂 作者介绍: 🎂🎂 🎂 🎉🎉🎉🎉🎉🎉🎉 🎂 🎂作者id:老秦包你会, 🎂 简单介绍:🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂 喜欢学习C语言、C++和python等编程语言,是一位爱分享的博主,有兴趣的小可爱可以来互讨 🎂🎂🎂🎂🎂🎂🎂🎂 🎂个人主页::小小页面🎂 🎂gitee页面:秦大大🎂 🎂🎂🎂🎂🎂🎂🎂🎂 🎂 一个爱分享的小博主 欢迎小可爱们前来借鉴🎂 默认成员函数 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。 类的空类 我们在写类的成员变量和成员函数,我们实例化一个对象,有时候就会忘记实例化,直接使用成员函数,这个在C语言会报错,在c++就不会报错,但是值是随机的 using std::cout; using std::endl; using std::cin; class Stru { public: void Fun() { cout << _a << endl; cout << _b << endl; } private: int* _a; int _b; }; int main() { Stru...
(c++)类和对象中篇_道_可的博客-CSDN博客
目录 1. 类的 6 个默认成员函数 如果一个类中什么成员都没有,简称为空类。 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。 2.
[C++] 类与对象(中)类中六个默认成员函数(1)
目录 1、类的六个默认成员函数 2、构造函数 2.1 构造函数的概念 2.2 特性 2.2.1 构造函数的重载: 2.2.2 全缺省的构造函数: 3、析构函数 3.1 析构函数的概念 3.2 特性 4、拷贝构造函数 4.1 拷贝构造函数的概念 4.2 特征 如果一个类中什么成员都没有,简称为空类。空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 2.1 构造函数的概念 我们这里来看看日期类的初始化: 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, 7, 5);...
史上最全类和对象 ,只要你认真看完C++类和对象,分分钟钟都吊打面试官【 C++】
面向过程和面向对象 类的引入 struct Test { int Add(int x, int y) { return x + y; } int a; double b; }; class Test { int Add(int x, int y) { return x + y; } int a; double b; }; #include<iostream> struct Queue { void Init() { } }; struct Stack { void Init(int defaultcapacity =4) { a = (int * )malloc(sizeof(int)*capacity); if (a == nullptr) { printf("malloc fail"); exit(-1); } capacity = defaultcapacity; top = 0; } void Push(int x) { a[top++] = x; } void Destory() { free(a); a = nullptr; top = capacity; }...
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with Class
Contact Class, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack