Classes

Is it possible to combine 2 classes, if so, how do you combine 2 classes in a C++ project?

if by combine you mean have one class have the same variables and functions as the other one, then yes. you just need to do inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/
Yeah, i am trying to create a Linear Algebra matrix. And some one helped me with it, and now i have two classes, one for The matrix, and the other one for Fraction.. when the code was completed, the code wasn't working properly, and the person told be, it could be because i'm using double, and i should create Fractions.. And i am not familiar with Fraction class or how i should replace the doubles with the. so i have been trying to figure it out since Friday, nothing seems to work. Can you please help, if it is not too much to ask
Here is the code

class Matrix {
private:
double* grid;
int size;

bool swapZeroColumn(int column);
void reduceColumn(int column);
public:
Matrix(int size);
~Matrix();

void set(int row, int column, double value);
double get(int row, int column);
void print(ostream& out);
void randomize(int min, int max);
void scaleRow(int row, double scale);
void addRows(int row1, int row2);
void swapRows(int row1, int row2);
bool solve();
};

Matrix::Matrix(int size) {
this->grid = new double[size * (size +1)];
this->size = size;
}

Matrix::~Matrix() {
delete[] this->grid;
}

void Matrix::set(int row, int column, double value) {
this->grid[this->size * row + column] = value;
}

double Matrix::get(int row, int column) {
return this->grid[this->size * row + column];
}


void Matrix::print(ostream& out) {
int r, c;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
out << setprecision(3) << this->get(r, c) << '\t';
}
out << endl;
}
}

void Matrix::randomize(int min, int max) {
srand(time(NULL));
int r, c;
int range = max - min + 1;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
double random = min + rand() % range;
this->set(r, c, random);
}
}
}

void Matrix::scaleRow(int row, double scale) {
int c;
for (c = 0; c < this->size+1; c++) {
double scaledValue = this->get(row,c) * scale;
this->set(row, c, scaledValue);
}
}

void Matrix::addRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double sum = this->get(row1, c) + this->get(row2, c);
this->set(row1, c, sum);
}
}

void Matrix::swapRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double temp = this->get(row1, c);
this->set(row1, c, this->get(row2, c));
this->set(row2, c, temp);
}
}

bool Matrix::swapZeroColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (this->get(r, column) != 0) {
this->swapRows(r, column);
return true;
}
}
return false;
}

void Matrix::reduceColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (r != column) {
double reduceFactor = - 1 / this->get(r, column);
this->scaleRow(r, reduceFactor);
this->addRows(r, column);
}
}
}

bool Matrix::solve() {
int c;
for (c = 0; c < this->size; c++) {
if (this->get(c, c) == 0) {
if (!this->swapZeroColumn(c)) {
// no non-zero elements in column, not singular
return false;
}
}
// scale diagonal element to 1
this->scaleRow(c, 1 / this->get(c, c));
// zero out other elements in column
this->reduceColumn(c);

std::cout << "After " << c + 1 << " reduction" << std::endl;
this->print(std::cout);
}
for (c = 0; c < this->size; c++) {
this->scaleRow(c, 1 / this->get(c, c));
}
return true;
}


void main(int argc, char* argv[]) {
while(1){
int n;
cout <<"Enter a number: ";
cin>> n;
Matrix m(n);
m.randomize(1, 8);
std::cout << "Random Matrix" << std::endl;
m.print(std::cout);

m.solve();
cout <<endl;
std::cout << "Reduced form" << std::endl;
m.print(std::cout);
}

}

And the fraction class

class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int n, int d = 1);
Fraction operator+(Fraction);
Fraction operator-(Fraction );
Fraction operator*(Fraction );
Fraction operator/(Fraction );
void Print(ostream& out);

};

Fraction::Fraction(int n, int d = 1) {
this->numerator = n;
this->denominator = d;
}

Fraction operator +(Fraction num, Fraction den)
{
int n, d, n1, d1, mult;
if (d==0){exit(0);}
num = (n*d1)+(n1*d);
den =(d*d1);

}
Fraction operator -(Fraction num, Fraction den){
int n, d, n1, d1;
if (d==0){exit(0);}
num = (n*d1)-(n1*d);
den =(d*d1);

}
Fraction operator*(Fraction num, Fraction den){
int n,d,n1, d1;;
if (d==0){exit(0);}
num= (n*n1);
den= (d*d1);
}
Fraction operator/(Fraction num, Fraction den){
int n,d,n1, d1;
if (d==0){exit(0);}
num= (n*d1);
den= (d*n1);
}
void main(){
int num, den;
cout<<num<<"/"<<den<<endl;
}
please use code tags
sorry, i'm new to this site
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;

class Matrix {
private:
double* grid;
int size;

bool swapZeroColumn(int column);
void reduceColumn(int column);
public:
Matrix(int size);
~Matrix();

void set(int row, int column, double value);
double get(int row, int column);
void print(ostream& out);
void randomize(int min, int max);
void scaleRow(int row, double scale);
void addRows(int row1, int row2);
void swapRows(int row1, int row2);
bool solve();
};

Matrix::Matrix(int size) {
this->grid = new double[size * (size +1)];
this->size = size;
}

Matrix::~Matrix() {
delete[] this->grid;
}

void Matrix::set(int row, int column, double value) {
this->grid[this->size * row + column] = value;
}

double Matrix::get(int row, int column) {
return this->grid[this->size * row + column];
}


void Matrix::print(ostream& out) {
int r, c;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
out << setprecision(3) << this->get(r, c) << '\t';
}
out << endl;
}
}

void Matrix::randomize(int min, int max) {
srand(time(NULL));
int r, c;
int range = max - min + 1;
for (r = 0; r < this->size; r++) {
for (c = 0; c < this->size + 1; c++) {
double random = min + rand() % range;
this->set(r, c, random);
}
}
}

void Matrix::scaleRow(int row, double scale) {
int c;
for (c = 0; c < this->size+1; c++) {
double scaledValue = this->get(row,c) * scale;
this->set(row, c, scaledValue);
}
}

void Matrix::addRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double sum = this->get(row1, c) + this->get(row2, c);
this->set(row1, c, sum);
}
}

void Matrix::swapRows(int row1, int row2) {
int c;
for (c = 0; c < this->size+1; c++) {
double temp = this->get(row1, c);
this->set(row1, c, this->get(row2, c));
this->set(row2, c, temp);
}
}

bool Matrix::swapZeroColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (this->get(r, column) != 0) {
this->swapRows(r, column);
return true;
}
}
return false;
}

void Matrix::reduceColumn(int column) {
int r;
for (r = 0; r < this->size; r++) {
if (r != column) {
double reduceFactor = - 1 / this->get(r, column);
this->scaleRow(r, reduceFactor);
this->addRows(r, column);
}
}
}

bool Matrix::solve() {
int c;
for (c = 0; c < this->size; c++) {
if (this->get(c, c) == 0) {
if (!this->swapZeroColumn(c)) {
// no non-zero elements in column, not singular
return false;
}
}
// scale diagonal element to 1
this->scaleRow(c, 1 / this->get(c, c));
// zero out other elements in column
this->reduceColumn(c);

std::cout << "After " << c + 1 << " reduction" << std::endl;
this->print(std::cout);
}
for (c = 0; c < this->size; c++) {
this->scaleRow(c, 1 / this->get(c, c));
}
return true;
}


void main(int argc, char* argv[]) {
while(1){
int n;
cout <<"Enter a number: ";
cin>> n;
Matrix m(n);
m.randomize(1, 8);
std::cout << "Random Matrix" << std::endl;
m.print(std::cout);

m.solve();
cout <<endl;
std::cout << "Reduced form" << std::endl;
m.print(std::cout);
}

}

And the fraction class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int n, int d = 1);
Fraction operator+(Fraction);
Fraction operator-(Fraction );
Fraction operator*(Fraction );
Fraction operator/(Fraction );
void Print(ostream& out);

};

Fraction::Fraction(int n, int d = 1) {
this->numerator = n;
this->denominator = d;
}

Fraction operator +(Fraction num, Fraction den)
{
int n, d, n1, d1, mult;
if (d==0){exit(0);}
num = (n*d1)+(n1*d);
den =(d*d1);

}
Fraction operator -(Fraction num, Fraction den){
int n, d, n1, d1;
if (d==0){exit(0);}
num = (n*d1)-(n1*d);
den =(d*d1);

}
Fraction operator*(Fraction num, Fraction den){
int n,d,n1, d1;;
if (d==0){exit(0);}
num= (n*n1);
den= (d*d1);
}
Fraction operator/(Fraction num, Fraction den){
int n,d,n1, d1;
if (d==0){exit(0);}
num= (n*d1);
den= (d*n1);
}
void main(){
int num, den;
cout<<num<<"/"<<den<<endl;
}
Last edited on
Topic archived. No new replies allowed.