constructor

I am making a constructor for a class which has a member of other class.I want to do a constructor which receive two arguments that are two object of a previous class, and I want to operate with them and create my an object of that class I put my whole code hear

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class Sline;
class plane_lp;
class plane_3p;

class fullvector;

class point{
	protected:
		int x,y,z;
	public:
		
			point():x(0),y(0),z(0){};
			point(int a,int b,int c):x(a),y(b),z(c){};
			point(const point& a):x(a.x),y(a.y),z(a.z){};
			point operator-(const point a);
			point operator=(const point a);
			point vector_product(const point P1,const point P2);
			friend ostream& operator<<(ostream& os,const point& a);
			int independent_term(const point a);
			friend ostream& operator<<(ostream& os,const fullvector& a);
};
		point point::operator-(const point a){
			point vector(a.x-this->x,a.y-this->y,a.z-this->z);
			return vector;
		}
		point point::operator=(const point a){
			this->x = a.x;
			this->y = a.y;
			this->z = a.z;
			return *this;
			
		}
		
		int point::independent_term(const point a){
			return (this->x*a.x+this->y*a.y+this->z*a.z)*-1;
			
		}
		
		
		
		point point::vector_product(const point P1,const point P2){
			
			return point((P1.y*P2.z)-(P1.z*P2.y),(P1.x*P2.z)-(P1.z*P2.x),(P1.x*P2.y)-(P1.y*P2.x));
			
		}
		
		ostream& operator<<(ostream& os,const point& a){
			os << a.x;
			os << "x";
			os <<" ";
			os << a.y;
			os << "y";
			os << " ";
			os << a.z;
			os << " ";
			os << "z";
			return os;
		}
		
		class Sline{
			private:
			point a;
			public:
				Sline() = default;
				Sline(point x,point y);
				Sline operator=(const Sline val){
					this->a = val.a;
					return *this;
				}
				friend ostream& operator<<(ostream& os,const Sline& val);
		};
		
		Sline::Sline(point x,point y){
				a.x = y.x-x.x;
				a.y = y.y-x.y;
				a.z = y.z-x.z; 
				}
		
		ostream& operator<<(ostream& os,const Sline& val){
			return os;
		}
		
		
		class fullvector:public point,public Sline{
			private:
				int D;
			public:
				fullvector():point(),D(0){};
				fullvector(int a,int b,int c,int d):point(a,b,c),D(d){};
				fullvector(const point& a,int d):point(a),D(d){};
				friend ostream& operator<<(ostream& os,const fullvector& a);
				void makeplane();
				friend void operator>>(istream& is,fullvector& a);
		};
		
		void operator>>(istream& is,fullvector& a){
			cout<<"Enter term x"<<"\n";
			cin>>a.x;
			cout<<"\n";
			cout<<"Enter term y"<<"\n";
			cin>>a.y;
			cout<<"\n";
			cout<<"Enter term z"<<"\n";
			cin>>a.z;
			cout<<"\n";
			cout<<"Enter term D"<<"\n";
			cin>>a.D;
			cout<<"\n";
		}
		
		
		
		void fullvector::makeplane(){
			
			cout<<"x term:"<<"\n";
			cin>>this->x;
			cout<<"\n";
			cout<<"y term:"<<"\n";
			cin>>this->y;
			cout<<"\n";
			cout<<"z term"<<"\n";
			cin>>this->z;
			cout<<"\n";
			cout<<"independent "<<"\n";
			cin>>this->D;
			cout<<"\n";
			
		}
		
		ostream& operator<<(ostream& os,const fullvector& a){
			os<<a.x;
			os<<"x";
			os<<" ";
			os<<a.y;
			os<<"y";
			os<<" ";
			os<<a.z;
			os<<"z";
			os<<" ";
			os<<a.D;
			return os;
		}
		
int main(int argc, char **argv)
{
	point p1(8,5,5);
	point p2(3,3,1);
	point p3(1,4,7);
	int independent = 0;
	Sline S1(p1,p2);
	
	point v1,v2;
	v1 = p1-p2;
	v2 = p2-p3;
	point rawplane;
	rawplane  = v1.vector_product(v1,v2);
	independent = v1.independent_term(rawplane);
	fullvector myplane(rawplane,independent);
	
	cout<<rawplane;
	cout<<"\n";
	cout<<myplane;
	cout<<"\n";
	
	fullvector plane1,plane2,plane3;
	cout<<"Creation of two planes"<<"\n";
	//plane1.makeplane();
	//plane2.makeplane();
	
	cin>>plane3;
	
	cout<<plane1<<"\n";
	cout<<plane2<<"\n";
	cout<<plane3<<"\n";
}






this is my constructor which I am having the trouble..

1
2
3
4
5
6
7

Sline::Sline(point x,point y){
				a.x = y.x-x.x;
				a.y = y.y-x.y;
				a.z = y.z-x.z; 
				}

x, y, and z are private members of class point. In order for Sline to access them, you will have to write some sort of accessor. The point class should have accessors for x, y and z (recommended) or make Sline a friend class of point.

I dont know what mean with accessor...I guess Sline can not get into members of a point because it's not a point It just has a point, but a point object can get access to his member....
An accessor is just a function that allows something to access a private or protected member. They usually take the form of getter/setter functions. e.g. If a class had a private member x, then another class would not be able to access x via the dot operator, however if you wrote functions getX() and setX(new_x), then it would be able to operate with the private/protected member.
Actually, you already have an operator-(...) function. Use it.
1
2
3
4
Sline::Sline(point x, point y)
{
    a = y - x;
}
Last edited on
yes, it works with two points, but the assignament is not working as that operator return a point and I want to do an assignament from one point to a one Sline...this si the operator-(...
1
2
3
4
5
6
7
8
9
10
//declaration
point operator-(const point a);

//definition

point point::operator-(const point a){
			point vector(a.x-this->x,a.y-this->y,a.z-this->z);
			return vector;
		}
)
shadowmouse coudl you give an example of getx() and setx(new_x)??
getx would be something like this
1
2
3
int point::getx(){
return x;
}

setx would be something like this.
1
2
3
void point::setx(int new_x){
x=new_x;
}

You then make getz, setz, gety and sety and use getx and gety instead of .x and .y
You would then change this
1
2
3
4
5
Sline::Sline(point x,point y){
				a.x = y.x-x.x;
				a.y = y.y-x.y;
				a.z = y.z-x.z; 
				}

to this
1
2
3
4
5
Sline::Sline(point x, point y) {
    a.setx(y.getx()-x.getx());
    a.sety(y.gety()-x.gety());
    a.setz(y.getz()-x.getz());
}
You should be using

1
2
3
Sline::Sline(point x,point y) : a(y - x) {
    // nothing to do here
}


You should use the constructor initializer list to initialize member variables, as you are already doing youy point class.

And you've already defined operator-

The version I give above does the same thing as your original attempt (member access aside), shadowmouse's set/get version, and this

1
2
3
4
Sline::Sline(point x, point y)
{
    a = y - x;
}


Andy

PS accessors should be const, e.g.

1
2
3
int point::getx() const { // <= as we don't need to change value
    return x;
}
Last edited on
thank you,i will try
PS A few other points:

1. I see you are using const point& for the input param of your point class' copy consructor but const point for the other methods.

Did you mean to use const point& everywhere? If so, make the correction.

Passing by value is not a big deal for point, as it's such a small class (just three ints). But bigger types should be passed by const ref.

If you do decide to pass by value you should drop the const though.

point point::operator-(const& point a){

or

point point::operator-(point a){

2. operator>> should not be using cin !!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	// NOT GOOD!
	void operator>>(istream& is,fullvector& a){
			cout<<"Enter term x"<<"\n";
			cin>>a.x;
			cout<<"\n";
			cout<<"Enter term y"<<"\n";
			cin>>a.y;
			cout<<"\n";
			cout<<"Enter term z"<<"\n";
			cin>>a.z;
			cout<<"\n";
			cout<<"Enter term D"<<"\n";
			cin>>a.D;
			cout<<"\n";
		}


It would probably be best if this input function was in a helper function, rather than in either operator>> or a member function.

3. You don't need one ostream line per <<

1
2
3
ostream& operator<<(ostream& os,const point& a){
	return os << a.x << "x " << a.y << "y " << a.z << "z ";
}


rather than

1
2
3
4
5
6
7
8
9
10
11
12
ostream& operator<<(ostream& os,const point& a){
	os << a.x;
	os << "x";
	os << " ";
	os << a.y;
	os << "y";
	os << " ";
	os << a.z;
	os << " ";
	os << "z";
	return os;
}


4. vector_product should not be a member of point, it should a stand alone function.

(As implemented it doesn't access member of the instance so this shange will be trivial.)

5. It worries me that class point has

friend ostream& operator<<(ostream& os,const fullvector& a);


Last edited on
Andywestken, sorry for repyling, have read carefully your comments, thanks a lot,It's really helpful....and basically the last two points they are the ones I can't get them properly..if vector_productor shouldn t be a member of point, how could it get the objects to use them...and the 5 point, I completely lost I dont know why I has a bad construction, I have the same kinf of friend function for others classes too, are they wrong too??
if vector_productor shouldn't be a member of point, how could it get the objects to use them...

It should work as-is. As I said before, it's already coded pretty much as a standalone function, even though it's declared as a member. But it will need to be a friend of the point class. You call it like:

rawplane = vector_product(v1,v2);

Or you could make is a static member instead, then you'd call it like

rawplane = point::vector_product(v1,v2);

But it does not make sense to call it like

rawplane = v1.vector_product(v1,v2);

as vector_product does not act on the members of the instance (though this would work with the static method.)

If it was a member function I would expect it to be called like

rawplane = v1.vector_product(v2);

and coded as

1
2
3
	point vector_product(const point P){
		return point((y*P.z)-(z*P.y),(x*P.z)-(z*P.x),(x*P.y)-(y*P.x));
	}


but I prefer the function form in this case (it looks more symmetric to me.)

and the 5 point, ...

I don't think classes should be making friends for other classes. You already have the line

friend ostream& operator<<(ostream& os,const fullvector& a);

in class fullvector. So it isn't needed in class point.

Also, while I'm what you're calling point looks like it is actually a vector (of course, this name collides with std::vector.)

And your function vector_product might be better called cross_product, as that is the more common term. And your independent_term() function is just the dot product multiplied by -1, so maybe it should be converted to it with the necessary sign adjustsments made elsewhere it your code.

Andy

PS I just spotted that your operator- is backwards. The current instance is the lefthand side, so you need

1
2
3
4
5
6
7
		point point::operator-(const point a){
			// wrong way round...
			//point vector(a.x-this->x,a.y-this->y,a.z-this->z);
			// right way round!
			point vector(this->x-a.x,this->y-a.y,this->z-a.z);
			return vector;
		}

Last edited on
With assorted adjustments...

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
#include <cstdio> // now C++ header
#include <iostream>
#include <string>

using namespace std;

class Sline;
class plane_lp;
class plane_3p;

class fullvector;

// point

class point{
    protected:
        int x,y,z;
    public:
        point():x(0),y(0),z(0){};
        point(int a,int b,int c):x(a),y(b),z(c){};
        point(const point& a):x(a.x),y(a.y),z(a.z){};
        point operator-(const point& a) const;
        point& operator=(const point& a);
        friend point cross_product(const point& P1,const point& P2);
        friend int dot_product(const point& P1, const point& P2);
        friend ostream& operator<<(ostream& os,const point& a);
};

point point::operator-(const point& a) const {
    return point(this->x-a.x,this->y-a.y,this->z-a.z); // reversed
}

point& point::operator=(const point& a){
    this->x = a.x;
    this->y = a.y;
    this->z = a.z;
    return *this;
}

int dot_product(const point& P1,const point& P2){ // replaces independent_term()
    return (P1.x*P2.x+P1.y*P2.y+P1.z*P2.z);
}

point cross_product(const point& P1,const point& P2){ // was vector_product
    return point((P1.y*P2.z)-(P1.z*P2.y),(P1.x*P2.z)-(P1.z*P2.x),(P1.x*P2.y)-(P1.y*P2.x));
}

ostream& operator<<(ostream& os,const point& a){
    return os << a.x << "x " << a.y << "y " << a.z << "z"; // compacted!
}

// Sline

class Sline{
    private:
        point a;
    public:
        Sline() = default;
        Sline(const point& x, const point& y) : a(x - y){
        }
        Sline operator=(const Sline& val){
            this->a = val.a;
            return *this;
        }
        friend ostream& operator<<(ostream& os,const Sline& val);
};

ostream& operator<<(ostream& os,const Sline& val){
    return os << val.a;
}

// fullvector

class fullvector:public point,public Sline{ // why inherit from Sline here?
    private:
        int D;
    public:
        fullvector():D(0){}; // no need to call base class default constructor explicitly
        fullvector(int a,int b,int c,int d):point(a,b,c),D(d){};
        fullvector(const point& a,int d):point(a),D(d){};
        friend ostream& operator<<(ostream& os,const fullvector& a);
};

// Not good to get input from user in the middle of operator>>
//
//void operator>>(istream& is,fullvector& a){
//    cout<<"Enter term x"<<"\n";
//    cin>>a.x;
//    cout<<"\n";
//    cout<<"Enter term y"<<"\n";
//    cin>>a.y;
//    cout<<"\n";
//    cout<<"Enter term z"<<"\n";
//    cin>>a.z;
//    cout<<"\n";
//    cout<<"Enter term D"<<"\n";
//    cin>>a.D;
//    cout<<"\n";
//}

// better solution (function which gets info
// from user and constructs object)
fullvector makeplane(){
    int x, y, z, D;
    cout<<"x term:"<<"\n";
    cin>>x;
    cout<<"\n";
    cout<<"y term:"<<"\n";
    cin>>y;
    cout<<"\n";
    cout<<"z term"<<"\n";
    cin>>z;
    cout<<"\n";
    cout<<"independent "<<"\n";
    cin>>D;
    cout<<"\n";
    return fullvector(x, y, z, D);
}

ostream& operator<<(ostream& os,const fullvector& a){
    return os << static_cast<const point&>(a) << " " << a.D << "i";
}

int main(int argc, char **argv)
{
    point p1(8,5,5);
    point p2(3,3,1);
    point p3(1,4,7);

    Sline S1(p1,p2);

    point v1 = p1-p2;
    point v2 = p2-p3;
    point rawplane  = cross_product(v1,v2);
    int independent = -dot_product(v1, rawplane); // note -
    fullvector myplane(rawplane,independent);

    cout<<rawplane<<"\n";
    cout<<myplane<<"\n";

    //cout<<"Creation of three planes"<<"\n";
    cout<<"Creation of one plane"<<"\n";

    fullvector plane1 = makeplane();
    //fullvector plane2 = makeplane();
    //fullvector plane3 = makeplane();

    cout<<plane1<<"\n";
    //cout<<plane2<<"\n";
    //cout<<plane3<<"\n";
}
I have read the message and the first thing I have thoug is:If I chose to do an static function I would have to declarate static variables too, and I don't know how It could work with the program...I guess I would have to reinitialize the variables as many times as I need otherwise I couldn't create any new point...and thanks you for your explication I'll have a better look later on...
The reason why Sline is inherited by fullvector is because a plane is technical drawing can be defined by 3 points or 1 point and a line...It's not completed yet, my idea is create a program to define plane from points and lines, and later doing operationes with diferents planes, like angle, intersections and things like that..
I have two questions related with this function

1
2
3
4
5
6


ostream& operator<<(ostream& os,const fullvector& a){
    return os << static_cast<const point&>(a) << " " << a.D << "i";
}


I'm not sure why there is a static cast there...It can be because It's type point and it must be passed to "os" in a regular type like int, char, string,etc...but I'm not completely sure...

And other thing...I know that when operator << is overloaded if that belongs two a class, the overloading must have just one argument, because operator << must have this structure alway....s variable << variable( so the current object, this in fact would being the firstr variable in operator structure), and when It's outside it must have two arguments(as It's done in our example), so If I wanted overload the operator <<( just in case, even if it's not recommedend) being the member of a class, the object should be an string to pass the value and don't get any trouble with types, shouldn't it?
Topic archived. No new replies allowed.