c构造函数和析构函数

Program 1:

程序1:

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

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:32:12: error: no matching function for call to ‘Sample::Sample()’
Sample S[2];

Explanation:

说明:

The above code will generate a syntax error because in the main() function we created an array of objects that must be instantiated by the default constructor, but in the class, the default constructor is not defined.

上面的代码将产生语法错误,因为在main()函数中,我们创建了必须由默认构造函数实例化的对象数组 ,但是在类中,未定义默认构造函数。

Program 2:

程式2:

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

Output:

输出:

10 20
30 40

Explanation:

说明:

In the above program, we created a class Sample that contains two data members X and Y, one parameterized constructor, set() and print() member functions.

在上面的程序中,我们创建了一个Sample类,其中包含两个数据成员 XY ,一个参数化的构造函数 , set()print()成员函数。

Now coming to the main() function, we created an array of objects and that will instantiated by the parameterized constructor. And created a pointer that is being initialized with the base address of array objects. And then called set() and print() functions using the pointer.

现在进入main()函数,我们创建了一个对象数组,该数组将由参数化构造函数实例化。 并创建了一个使用数组对象的基地址初始化的指针。 然后使用指针调用set()print()函数。

Program 3:

程式3:

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

Output:

输出:

main.cpp:9:14: error: constructors may not be cv-qualified
Sample() const
^~~~~

Explanation:

说明:

The above program will generate an error because we cannot create a constructor as a const member function in C++.

上面的程序将产生错误,因为我们不能在C ++中将构造函数创建为const成员函数。

Recommended posts

推荐的帖子

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

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

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

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

  • 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-2.aspx

c构造函数和析构函数

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

  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 ++构造函数和析构函数| 查找输出程序| 套装3

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

  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. MySQL事物系列:1:事物简介
  2. 《埃森哲技术展望2016》解密未来企业竞争究竟拼什么?
  3. 那些实用与颜值齐飞的桌面!
  4. android 利用类的同名方法欺骗jni调用(一)
  5. iOS 在类实现定义中声明成员变量的怪异方式
  6. redis队列缓存 + mysql 批量入库 + php离线整合
  7. 注册登录页面代码用js判断是否填入信息_php实现登录功能
  8. 《Java核心技术 卷Ⅱ 高级特性(原书第10版)》一2.2.2 如何读入文本输入
  9. C++基础06-类与对象之new和malloc
  10. Oracle Assets Additions API--Sample Script(Invoices)
  11. Notepad++官网下载
  12. 再生核希尔伯特空间:Hilbert Space与RKHS基础
  13. android在线图标生成工具,图标在线生成工具Android Asset Studio的使用
  14. Bug.Bounty.Bootcamp:(2)值得看的一本书
  15. 微信小程序开发——调用免费天气api接口(高德、天气API)
  16. java生成短网址_最新url.cn短网址缩短生成接口(API)获取方法
  17. 快速搭建个人在线书库,随时随地畅享阅读!
  18. 衡水二中2021清华北大高考成绩查询,衡水二中成为“清华大学2020年优质生源中学”...
  19. 人工智能讲师AI讲师叶梓谈人工智能的应用人工智能项目咨询应用案例-8
  20. NV欢迎Intel进入GPU市场 黄仁勋如此评价

热门文章

  1. git 切换分支_git 入门教程之分支总览
  2. 下列关于html5表单的多样输入方式,IT兄弟连 HTML5教程 HTML5表单 多样的输入类型1...
  3. oracle key的含义,v$session SERIAL#字段的含义
  4. 安装 PyTorch C++ API libtorch 及一个最小例子
  5. jenkins vue 打包特别慢_从零开始 使用VUE开发桌面客户端
  6. 运放放大倍数计算公式_19.运算放大器的特性与应用,不得不掌握的知识点(一)...
  7. 虚拟跳线软件干什么用的_疯狂刷单!用违法软件生成虚拟手机号,“骑手”半年“刷单”牟利60余万,百米内竟有万笔订单 | 申晨间...
  8. python 趣味编程课_青少年编程:Python趣味编程基础入门课程
  9. python调用窗口找到文件,使用Python在Mac OS X中查找当前活动窗口
  10. RabbitMQ安装|使用|概念|Golang开发