当前位置:网站首页>Vector 1 (classes and objects)

Vector 1 (classes and objects)

2022-06-23 01:01:00 SZU healing system bug

Catalog

Title Description

AC Code


Title Description

n An ordinal number a1,a2,...,an The array is called n Dimension vector . by n Dimension vector definition CVector class , Contains private data members :

int *data;// Storage n Dimension vector

int n; // Vector dimension .

There are methods : Parameter free constructor , Set up n=5,data The data of each group are 0,1,2,3,4; Constructors , Use virtual parameter n1 And an array a initialization n and data The data of ; Output function , Output... In format n The value of the dimension vector ; Destructor .

Main function input data , Generate CVector Object and call the output function to test .

Input

Input n

Input n Dimension vector

Output

Call the parameterless and parameterless constructors respectively to generate 2 individual CVector object , Output their values .

sample input 1

6
10 1 2 3 4 5

sample output 1

0 1 2 3 4
10 1 2 3 4 5

AC Code

#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;
}
原网站

版权声明
本文为[SZU healing system bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221409100595.html