当前位置:网站首页>OOP vector addition and subtraction (friend + copy construction)
OOP vector addition and subtraction (friend + copy construction)
2022-06-25 04:43:00 【SZU healing system bug】
Catalog
Title Description
Define a CVector The categories are as follows :
class CVector
{
private:
int *data; // Storage n Dimension vector
int n; // Vector dimension
public:
CVector(int n1, int *a);
CVector(CVector &c);
~CVector() { delete data; }
void print();
friend CVector Add(const CVector V1, const CVector V2);
friend CVector Sub(const CVector V1, const CVector V2);
};
Requirements are as follows :
Based on input and output CVector class , And implement friend functions respectively Add、Sub To add and subtract two vectors .
Input
first line , Enter the number of tests t
The format of each group of test data is as follows :
Vector dimension n
first n Dimension vector values
the second n Dimension vector values
Output
For each group of test data , Output the results of their addition and subtraction
sample input 1
2
3
1 2 3
4 5 6
5
1 2 3 4 5
-1 2 4 6 10
sample output 1
5 7 9
-3 -3 -3
0 4 7 10 15
2 0 -1 -2 -5
AC Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
class CVector {
private:
int *data; // Storage n Dimension vector
int n; // Vector dimension
public:
CVector(int n1, int *a);
CVector(const CVector &c);
~CVector() {
delete data;
}
void print();
friend CVector Add(const CVector V1, const CVector V2);
friend CVector Sub(const CVector V1, const CVector V2);
};
CVector::CVector(int n1,int *a){
n=n1;
data=new int[n1];
for(int i=0;i<n1;i++)
data[i]=a[i];
}
CVector::CVector(const CVector &c){
n=c.n;
data=new int[c.n];
for(int i=0;i<c.n;i++)
data[i]=c.data[i];
}
void CVector::print(){
for(int i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[n-1]<<endl;
}
CVector Add(const CVector V1, const CVector V2){
CVector temp(V1);
for(int i=0;i<temp.n;i++)
temp.data[i]=V1.data[i]+V2.data[i];
return temp;
}
CVector Sub(const CVector V1, const CVector V2){
CVector temp(V1);
for(int i=0;i<temp.n;i++)
temp.data[i]=V1.data[i]-V2.data[i];
return temp;
}
int main() {
int t,n,i;
cin>>t;
while(t--){
cin>>n;
int *p=new int [n];
for(i=0;i<n;i++)
cin>>p[i];
CVector a(n,p);
for(i=0;i<n;i++)
cin>>p[i];
CVector b(n,p);
Add(a,b).print();
Sub(a,b).print();
delete[] p;
}
}边栏推荐
- A detailed summary of TCP connection triple handshake
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)
- Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
- The solution of wechat applet switchtab unable to take parameters
- 哪个编程语言实现hello world最烦琐?
- Data view for gbase 8s
- LabVIEW开发气体调节器
- Office macro virus bounce shell experiment
- Records of ros2/dds/qos/ topics
- Package for gbase 8s
猜你喜欢

重磅直播 | 相移法+多频外差之数学原理推导+实现

Gbase 8s overall architecture

Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg

Record the problem of C # print size once

领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!

Gbase 8s index b+ tree

机器学习深度学习——向量化

论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)

大话云原生数据库中的存算分离

CTF_ Web: how to recognize and evaluate a regular expression
随机推荐
第九章 APP项目测试(2) 测试工具
华为鸿蒙开发第四课
SQL injection details
Record the problem of C # print size once
js的sort()函数
Mongodb cluster
GBASE 8s的包
Structure syntaxique des procédures stockées gbase 8S
Gbase 8s parallel operation problem scenario description
Machine learning deep learning -- Vectorization
写shell脚本报错总结
Gbase 8s index R tree
记录小知识点
Why PHP is not safe
Deep learning - several types of learning
CTF_ Web: Changan cup-2021 old but a little new & asuka
Php7.2 add JPEG extension
cannot import name ‘escape’ from ‘jinja2’【成功解决】
【Flink】RocksDB增量模式checkpoint大小持续增长的问题及解决
Separation of storage and computing in Dahua cloud native database