当前位置:网站首页>向量1(类和对象)
向量1(类和对象)
2022-06-22 14:09:00 【SZU治愈系BUG】
目录
题目描述
n个有序数a1,a2,...,an组成的数组称为n维向量。 为n维向量定义CVector类,包含私有数据成员:
int *data;//存储n维向量
int n; //向量维数。
方法有:无参构造函数,设置n=5,data的数据分别为0,1,2,3,4;构造函数,用虚参n1和数组a初始化n和data的数据;输出函数,按格式输出n维向量的值;析构函数。
主函数输入数据,生成CVector对象并调用输出函数测试。
输入
输入n
输入n维向量
输出
分别调用无参和带参构造函数生成2个CVector对象,输出它们的值。
输入样例1
6
10 1 2 3 4 5
输出样例1
0 1 2 3 4
10 1 2 3 4 5
AC代码
#include<iostream>
using namespace std;
class CVector
{
int * data;
int n;
public:
CVector(){
data=new int[5];
for(int i=0;i<5;i++)
data[i]=i;
n=5;
}
CVector(int n1,int *a){
n=n1;
data=new int[n1];
for(int i=0;i<n1;i++)
data[i]=a[i];
}
void display(){
int i;
for(i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[i]<<endl;
}
~CVector(){
if(data)
delete[] data;
data=NULL;
}
};
int main() {
int n,i;
cin>>n;
int *p=new int [n];
CVector a;
a.display();
for(i=0;i<n;i++)
cin>>p[i];
CVector b(n,p);
b.display();
if(p)
delete[] p;
p=NULL;
}边栏推荐
- 树结构二叉树
- Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
- U++ iterative Sorting Query learning notes
- Thoroughly understand the builder mode (builder)
- Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
- 多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
- I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!
- [Zhejiang University] information sharing of the first and second postgraduate entrance examinations
- Good wind relies on strength – code conversion practice of accelerating SQL Server Migration with babelfish
- How to use the concat() function of MySQL
猜你喜欢

NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
![[Software Engineering] acquire requirements](/img/52/01320eee5e519910a62f48d604b180.png)
[Software Engineering] acquire requirements

晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦

求求了,别被洗脑了,这才是90%中国人的生存实况

Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan

得物技术复杂 C 端项目的重构实践

Thoroughly understand the factory mode

PowerPoint 教程,如何在 PowerPoint 中添加水印?

Please, don't be brainwashed. This is the living reality of 90% of Chinese people

Once, I had 5 part-time jobs just to buy a new earring for my girlfriend
随机推荐
Struggle, programmer -- Chapter 48 A thousand gold coins are bought like Fu. Who can complain about this situation
极致效率,云原生数据库TDSQL-C安身立命的根本
RealNetworks vs. Microsoft: the battle in the early streaming media industry
Struggle, programmer -- Chapter 46 this situation can be recalled, but it was at a loss at that time
Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
Struggle, programmer chapter 45 tenderness is like water and a good time is like a dream
After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!
Using virtual serial port to debug serial port in keil MDK
网站存在的价值是什么?为什么要搭建独立站
阿里云发布CIPU,对于企业客户意味着什么?
What does Alibaba cloud's cipu release mean for enterprise customers?
Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
2022年失业的人多吗?今年是不是特别难找工作?
flutter video_player实现监听和自动播放下一首歌曲
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
U++编程 移动 学习笔记
PowerPoint tutorial, how to add watermarks in PowerPoint?
全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer