c构造函数和析构函数

Program 1:

程序1:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
public:
Sample()
{
X = 0;
}
void set(int x)
{
X = x;
}
void print()
{
cout << X << endl;
}
};
int main()
{
Sample S[2] = { Sample(), Sample() };
S[0].set(10);
S[1].set(20);
S[0].print();
S[1].print();
return 0;
}

Output:

输出:

10
20

Explanation:

说明:

In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.

在上面的程序中,我们创建了带有数据成员 X , 默认构造函数 , set()print()成员函数的类Sample

Now coming to the main() function, here we created an array of objects that instantiated by default constructor. And then called set() and print() function using an array of objects then it will print above output.

现在转到main()函数 ,这里我们创建了一个对象数组,这些对象默认由构造函数实例化。 然后使用对象数组调用set()print()函数,然后它将在输出上方进行打印。

Program 2:

程式2:

#include <iostream>
#include <stdlib.h>
using namespace std;
class Sample {
private:
int X;
public:
Sample()
{
X = 0;
cout << "Constructor called";
}
void set(int x)
{
X = x;
}
void print()
{
cout << X << endl;
}
};
int main()
{
Sample* S;
S = (Sample*)malloc(sizeof(Sample));
S.set(10);
S.print();
return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:32:7: error: request for member ‘set’ in ‘S’,
which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
S.set(10);
^~~
main.cpp:33:7: error: request for member ‘print’ in ‘S’,
which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
S.print();
^~~~~

Explanation:

说明:

The above program will generate an error, because, S is a pointer, and we used Dot (.) Operator to call set() and print() member functions instead of referential operator (->).

上面的程序将产生错误,因为S是指针,并且我们使用Dot(。)运算符而不是引用运算符(->)来调用set()print()成员函数。

Program 3:

程式3:

#include <iostream>
#include <stdlib.h>
using namespace std;
class Sample {
private:
int X;
public:
Sample()
{
X = 0;
cout << "Constructor called";
}
void set(int x)
{
X = x;
}
void print()
{
cout << X << endl;
}
};
int main()
{
Sample* S;
S = (Sample*)malloc(sizeof(Sample));
(*S).set(10);
(*S).print();
return 0;
}

Output:

输出:

10

Explanation:

说明:

In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.

在上面的程序中,我们创建了带有数据成员X ,默认构造函数, set()print()成员函数的类Sample

Coming to the main() function, here we declared a pointer of the class Sample and instantiate by malloc() function and then called set() and print() functions using the pointer. But, when we use malloc() function it will not call the constructor.

来到main()函数,这里我们声明了Sample类的指针,并通过malloc()函数实例化,然后使用该指针调用set()print()函数。 但是,当我们使用malloc()函数时 ,它将不会调用构造函数。

Recommended posts

推荐的帖子

  • C++ Constructor and Destructor | Find output programs | Set 1

    C ++构造函数和析构函数| 查找输出程序| 套装1

  • C++ Constructor and Destructor | Find output programs | Set 2

    C ++构造函数和析构函数| 查找输出程序| 套装2

  • C++ Constructor and Destructor | Find output programs | Set 4

    C ++构造函数和析构函数| 查找输出程序| 套装4

  • C++ Constructor and Destructor | Find output programs | Set 5

    C ++构造函数和析构函数| 查找输出程序| 套装5

  • C++ Default Argument | Find output programs | Set 1

    C ++默认参数| 查找输出程序| 套装1

  • C++ Default Argument | Find output programs | Set 2

    C ++默认参数| 查找输出程序| 套装2

  • C++ Arrays | Find output programs | Set 1

    C ++数组| 查找输出程序| 套装1

  • C++ Arrays | Find output programs | Set 2

    C ++数组| 查找输出程序| 套装2

  • C++ Arrays | Find output programs | Set 3

    C ++数组| 查找输出程序| 套装3

  • C++ Arrays | Find output programs | Set 4

    C ++数组| 查找输出程序| 套装4

  • C++ Arrays | Find output programs | Set 5

    C ++数组| 查找输出程序| 套装5

  • C++ Class and Objects | Find output programs | Set 1

    C ++类和对象| 查找输出程序| 套装1

  • C++ Class and Objects | Find output programs | Set 2

    C ++类和对象| 查找输出程序| 套装2

  • C++ Class and Objects | Find output programs | Set 3

    C ++类和对象| 查找输出程序| 套装3

  • C++ Class and Objects | Find output programs | Set 4

    C ++类和对象| 查找输出程序| 套装4

  • C++ Class and Objects | Find output programs | Set 5

    C ++类和对象| 查找输出程序| 套装5

翻译自: https://www.includehelp.com/cpp-tutorial/constructor-and-destructor-find-output-programs-set-3.aspx

c构造函数和析构函数

c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装3相关推荐

  1. c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  2. c ++查找字符串_C ++类和对象| 查找输出程序| 套装3

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; publi ...

  3. c ++查找字符串_C ++类和对象| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  4. c ++查找字符串_C ++类和对象| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private int ...

  5. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装1

    c构造函数和析构函数 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private: in ...

  6. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装2

    c构造函数和析构函数 Program 1: 程序1: #include<iostream> using namespace std; class Sample { private: int ...

  7. c构造函数和析构函数_C ++构造函数,析构函数能力问题和答案(第2组)

    c构造函数和析构函数 C ++构造函数和析构函数能力问题列表 (List of C++ Constructor and Destructor Aptitude Questions & Answ ...

  8. c ++ 继承_C ++继承| 查找输出程序| 套装1

    c ++ 继承 Program 1: 程序1: #include <iostream> #include <string.h> using namespace std; cla ...

  9. c语言指针++_C ++此指针| 查找输出程序| 套装1

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; int main() { int A = 10; this ...

最新文章

  1. 知乎热议:985 计算机视觉研究生找不到工作怎么办?
  2. ylb:SQL 常用函数
  3. concat mysql sql注入_sql注入-mysql注入基础及常用注入语句
  4. bzoj 1026: [SCOI2009]windy数 数位DP算法笔记
  5. SpringBoot系列: Redis基础
  6. mysql雨凇_Unity3D研究院之Unity中连接本地或局域网MySQL数据库(五十九) | 雨松MOMO程序研究院...
  7. C#里内置的DateTime基本功能
  8. 数风·数林 | 炉石传说中的概率(声控篇)
  9. 为什么大学感觉学编程很难?原因有这三点。
  10. Springboot+Dubbo+Nacos 注解方式实现微服务调用
  11. 数据--第49课 - 线性索引查找
  12. ByPass Mode(略过模式或旁路模式)
  13. Matlab 工作区变量和 MAT 文件
  14. java学习笔记 - 集合类综合案例 斗地主
  15. 周老师,李记者,朱老师
  16. 21版本FL Studio水果音乐制作软件下载
  17. BPM流程引擎功能对比
  18. 牛客网项目——前置技术(八):Kafka
  19. ROS2编程基础课程--DDS
  20. 看完这篇文章再也不用担心代码格式不对无法合并了

热门文章

  1. unity 彩带粒子_iOS动画开发----粒子系统---彩带效果
  2. linux用户和用户组
  3. 日常问题——pdsh localhost Connection refused
  4. ubuntu7.10 apache+php+mysql配置
  5. 谈谈我对MYSQL乱码的解决办法
  6. MIP 支付组件,支付流程:
  7. windows下jenkins常见问题填坑
  8. RxJS + React hooks
  9. 127.0.0.1与localhost的区别
  10. 索引(转载自百度百科)