Lab3_class_vector
mysliutchka
plain_text
3 years ago
2.0 kB
11
Indexable
#include <iostream>
using namespace std;
class Vector{
private:
int vector_size;
double *p_vector;
public:
/*Vector(int &rows,int &cols){
this->vector_size=0;
this->p_vector= nullptr;
}*/
Vector(int &cols){
cout<<"Enter vector"<<endl;
this->vector_size=cols;
this->p_vector = new double[this->vector_size];
for(int i=0; i<this->vector_size;i++){
double k;
cin>>k;
this->p_vector[i]=k;
}
}
void Print(){
for(int i=0; i< this->vector_size;i++){
cout<<this->p_vector[i]<<"\t";
}
cout<<endl;
}
double Sum_of_elements(){
double sum=0;
for(int i=0;i< this->vector_size;i++){
sum=sum+this->p_vector[i];
}
cout<<sum;
cout<<endl;
return sum;
}
~Vector(){
cout<<"Vector destructor"<<endl;
delete [] this->p_vector;
}
};
class Table {
private:
int table_rows;
int table_cols;
Vector **p_table;
public:
Table(int& cols, int& rows) {
this->table_rows = rows;
this->table_cols = cols;
this->p_table = new Vector * [this->table_rows];
for (int i = 0; i < table_rows; i++) {
this->p_table[i] = new Vector(cols);
}
}
void PrintTable() {
for (int i = 0; i < this->table_rows; i++) {
this->p_table[i]->Print();
}
}
~Table() {
cout<<"Table destructor"<<endl;
for (int i = 0; i < this->table_rows; i++) {
delete [] this->p_table[i];
}
delete[] this->p_table;
}
};
int main() {
int rows,cols;
cout<<"Enter number of rows in the table: "<<endl;
cin>>rows;
cout<<"Enter number of columns in the table: "<<endl;
cin>>cols;
Table table_1(cols,rows);
table_1.PrintTable();
return 0;
}
Editor is loading...