Inherit and calculate dot and cross Product

I do not know how to correctly print the cross product. Im supposed to get 3 numbers but doesn't work. If you see any improvement i can make in my code, please let me know i will appreciate any help!

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
#include <iostream>
using namespace std;

class Vector2D		//Base
{
	protected:
		int x, y, a, b, c;
	public:
		Vector2D(int, int, int, int, int);
		void print();
};
class Vector3D:public Vector2D	//
{
	private:
		int z;
	public:
		Vector3D(int, int, int, int, int, int);
		int	DotProduct();
		int CrossProduct();
		void print();
};
Vector2D::Vector2D( int x1, int y1, int a1, int b1, int c1)
{
	x = x1;
	y = y1;
	a = a1;
	b = b1;
	c = c1;
}
Vector3D::Vector3D(int x2, int y2,int z2, int a2,int b2, int c2)
:Vector2D(a2, b2, c2, x2, y2)
{
	x = x2;
	y = y2;
	z = z2;
	a = a2;
	b = b2;
	c = c2;
}
int Vector3D::DotProduct()
{
    double dp = (x*a + y*b + z*c);
    return dp;
}
int Vector3D::CrossProduct()
{
	int d = ((y*c) - (z*b));
	int e = ((z*a) - (x*c));
	int f = ((x*b) - (y*a));
	return *this;
}
void Vector3D::print()
{
	cout << "\n\nThe dot product of < " << x <<", "<<y<<", "<< z 
	<<"> and <" << a <<", "<<b <<", "<<c <<"> is <" << DotProduct() <<">"<<endl;
	cout << "\nThe cross product of <" << x <<", "<<y<<", "<< z 
	<<"> and <" << a <<", "<<b <<", "<<c <<"> is <" << d <<", "<< e <<", "<< f <<">";
//This line above i need help with i need to print 3 ints
}
int input(int&, int&, int&, int&, int&, int&);
int main ()
{
	int x, y, z, a, b, c;
	input(x, y, z, a, b, c);
	Vector3D S2(x, y, z, a, b, c);
	
	S2.print();
	
	return 0;
}
int input(int& x, int& y, int& z, int& a, int& b, int& c)
{
	cout << "Enter values for x, y, and z: ";
	cin >> x >> y >> z;
	cout << "\n\nEnter values for a, b, and c: ";
	cin >> a >> b >> c;
	
	return x, y, z, a, b, c;
}
What you could do is just save the 3 numbers in an array. And then display it like a vector.

int crossProduct[3] = {d,e,f};

Print it out like this maybe?

cout << "(" << crossProduct[0] << ", " << crossProduct[1] << ", " << crossProduct[2] << ")";
Yup that is the only option i have lol

now do you know if i should put the destructor for both classes or just Vector3D?
You got it all wrong

1) Class Vector2D, line 7

vector 2D should have 2 data members of type double, not 5 data members of type int.

2) Class Vector3D should not be a subclass of Vector2D (because it is not true that every 3D vector is [a kind of] a 2D vector)

3) vector 3D should have 3 data members of type double
Last edited on
So how would you write it? You mean 2 arrays of type double ? Wait why double does not type work similarly? I mean I'm not using decimals so what is the point?
About the 3D you mean to switch the base class to the derived class and vice versa.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Vector2D		
{
	protected:
		double x, y;
	public:
		Vector2D(int p_x, int p_y);
		double X() { return x;}
		double Y() { return y;}
};

Vector2D::Vector2D(int p_x, int p_y)
{
	x = p_x;
	y = p_y;
}

double DotProduct(Vector2D a, Vector2D b)
{
//...
}
Last edited on
Also, Classes Vector2D and Vector3D should be unrelated (no inheritance). Neither one should be a base.
Last edited on
Oh my bad I forgot to mention I'm supposed to inherit bro so I have to use Vector2D as a base but don't mind the name bro I know it doesn't make sense but that's the name my professor told me to use even tho I'm doing only 3D.
Listen, bro, you should really doubt the expertise of your professor who tells you to inherit Vector3D from Vector2D. It is a pure nonsense. He is providing you bad examples.

So, I'm waiting for you to make an attempt at writing DotProduct function which I have outlined in my code above.
lml i would not say that tho because my professor is the best qualified and bro he had so many good jobs so i have faith in him lol.

and i already did it works thanks you guys gave me an ideas so i fix my code and improved it
Topic archived. No new replies allowed.