博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++中对象数组的初始化_C ++中对象的动态初始化
阅读量:2533 次
发布时间:2019-05-11

本文共 2357 字,大约阅读时间需要 7 分钟。

c++中对象数组的初始化

C ++对象的动态初始化 (C++ Dynamic Initialization of Objects)

The Dynamic Initialization of Objects means to initialize the data members of the class while creating the object. When we want to provide initial or default values to the data members while creating of object - we need to use dynamic initialization of objects.

动态初始化对象意味着在创建对象时初始化类的数据成员。 当我们要在创建对象时为数据成员提供初始值或默认值时,我们需要使用对象的动态初始化

Now, the question is how to achieve or implement dynamic initialization of objects?

现在,问题是如何实现或实现对象的动态初始化

It can be implemented by using parameterized constructors in C++.

可以通过使用C ++中的参数化构造函数来实现。

Example:

例:

Here, we have a class named "Student" which contains two private data members 1) rNo - to store roll number and 2) perc - to store percentage.

在这里,我们有一个名为“学生”,其中包含两个私有数据成员1)RNO类-存储卷号和2)PERC -存储百分比。

Program:

程序:

#include 
using namespace std;//structure definition with private and public membersstruct Student {private: int rNo; float perc;public: //constructor Student(int r, float p) { rNo = r; perc = p; } //function to read details void read(void) { cout << "Enter roll number: "; cin >> rNo; cout << "Enter percentage: "; cin >> perc; } //function to print details void print(void) { cout << endl; cout << "Roll number: " << rNo << endl; cout << "Percentage: " << perc << "%" << endl; }};//Main codeint main(){ //reading roll number and percentage to initialize //the members while creating object cout << "Enter roll number to initialize the object: "; int roll_number; cin >> roll_number; cout << "Enter percentage to initialize the object: "; float percentage; cin >> percentage; //declaring and initialize the object struct Student std(roll_number, percentage); //print the value cout << "After initializing the object, the values are..." << endl; std.print(); //reading and printing student details //by calling public member functions of the structure std.read(); std.print(); return 0;}

Output

输出量

Enter roll number to initialize the object: 101Enter percentage to initialize the object: 84.02After initializing the object, the values are...Roll number: 101Percentage: 84.02%Enter roll number: 102Enter percentage: 87Roll number: 102Percentage: 87%

翻译自:

c++中对象数组的初始化

转载地址:http://zvvzd.baihongyu.com/

你可能感兴趣的文章
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
RotateCard(自定义旋转view)
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>