Inheritance: Derived classes

I have defined the base class shape:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>

using namespace std;

class Shape{

public:
    double perimeter();
    void print();
    void println();
    void translate(double dx, double dy);
};
#endif 


And I want to construct two derived class from this base class.

Hence I define the header file of one derived class as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef TRIANGLE_H
#define TRIANGLE_H

#include "shape.h"
#include <iostream>
#include <cmath>
using namespace std;

class Triangle:public Shape{

private:
    Point p1;
    Point p2;
    Point p3;

public:
    Triangle(Point p1_set, Point p2_set, Point p3_set);
    Triangle();
    void set(int i, Point p);
    Point get(int i);
};
#endif 


However, enven though I have correctly code the functions such as 'translate' in the definition file of Triangle, the compiler does not recognize that translate has been defined in the header file of base class.

Please help me out here.
Thank you.
Did you provide implementations of the following functions?

1
2
3
4
double Shape::perimeter();
void Shape::print();
void Shape::println();
void Shape::translate(double dx, double dy);


Because with your current definition of the Shape class, it requires the implementation of each of these methods. If, however, you believe that a "Shape" is too nebulous to be defined or instantiated, then you need to make at least one of its functions a pure virtual function. This would define Shape as an abstract class. For each function you believe shouldn't be implemented at this level, you should do something like:

virtual void myFunction() = 0;

For instance, if you believe perimeter, print, println, and translate should not be implemented at the Shape level, then you need to do the following:

1
2
3
4
5
6
7
8
class Shape{

public:
    virtual double perimeter() = 0;
    virtual void print() = 0;
    virtual void println() = 0;
    virtual void translate(double dx, double dy) = 0;
};


You would then need to override each of these methods in a derived class of Shape or that class would become abstract as well.
Yes. It solves the problem.
Thank you!
Topic archived. No new replies allowed.