inheritance

I am having troubles to make the constructors of my derived class, I believe that I should call twice the constructor to create a Sline object, but I should call the cosntructor twice referenced to p1 and p2( supossing that It's the solution) I can't imagine how to do it...Thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <stdio.h>
#include <iostream>

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){};
};

class Sline: public point{
	protected:
		point p1,p2;
		
	public:
		Sline():p1.x(0),p1.y(0),p1.z(0),p2.x(0),p2.y(0),p2.z(0){};
		Sline(point a,point b):p1.x(a.x),p1.y(b.x),p1.z(c.x),p2.x(b.x),p2.y(b.y),p2.z(b.z){};
		
		
		
};
First off, Sline should not inherit from point. Sline is NOT a point. Sline HAS two points.

Line 19: you don't need to initialize each x,y,z to zero. point's default constructor already does that.
1
2
   Sline()
   {}

In fact this constructor isn't really needed, since the compiler will provide a default constructor automatically.

Line 20 can be written more concisely by using point's implicit copy constructor:
1
2
  Sline (point a, point b) : p1(a), p2(b)
  {}






Their relation should me more friendship than inheritance, arent they=
It should be composition, actually.
A composition is just a system class where some classes have objects of other classes,isnt it?
Yes. Your Sline have two Points. That it. It is not a Point itself.
Edit: reading back up the thread I see I am restating what AbstractionAnon has already said...

But you do need to provide a default constructor if you declare other sorts of constructors (e.g. point(int a,int b,int c) stops the automatic generation of the default constructor, but not the copy constructor.) So need either:

1
2
3
4
5
6
7
class point {
    private:
        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){};
};


or

1
2
3
4
5
6
7
class point {
    private:
        int x,y,z;
    public:
        point() = default; // for C++11
        point(int a,int b,int c):x(a),y(b),z(c){};
};


A composition is just a system class where some classes have objects of other classes,isnt it?

A composite (or composite class) is one which has members which are classes. That is, it is composed of other classes.

Not sure what you mean by "system class"?

Andy

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
class point {
    private: // why was it 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){};
        // could add copy constructor, but not needed...
        //point(const point& p):x(p.x),y(p.y),z(p.z){};
};

class Sline { // no longer incorrectly inherits from point
    protected:
        point p1,p2;
        
    public:
        Sline() {}; // point constructors already init point members
        Sline(point a,point b):p1(a), p2(b){}; // now using point copy constructor
};

int main()
{
    point p1(1, 2, 3);
    point p2(4, 5, 6);

    point p3;
    point p4(7, 8, 9);

    Sline s1(p1, p2);
    Sline s2(p3, p4);
    Sline s3;

    return 0;
}
Last edited on
I meant with system class a set of class....thanks!!
Topic archived. No new replies allowed.