Best solution to solve problem on inheritance?

HI all,

I would like to write a programme that consists of a base class and two derived classes similar to the example file under "Friendship and Inheritance - inheritance between classes" (http://www.cplusplus.com/doc/tutorial/inheritance/).

However, what differs from my programme that I would like to create and the example is that I would like to inherit the exact same calculation step for both derived classes with different values of variables.

Modifying from the example...

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

class CPerimeter
{
  protected:
    int width, length, perimeter;
  public:
    void calc_perimeter();
};

void CPerimeter::calc_perimeter()
{
  perimeter = width + length + width + length;
}

class CRectangle: public CPerimeter 
{
  public:
    void UserInput_rect()
};

void CRectangle::UserInput_rect()
{
  cin >> width >> endl;    // width = 3;    different value of width compared to CSquare
  cin >> length >> endl;   // length = 5;   different value of length compared to CSquare
}

class CSquare: public CPerimeter
{
  public:
    void UserInput_square();
};
  
void CSquare::UserInput_square()
{
  cin >> width >> endl;    // width = 10;    different value of width compared to CRectangle
  cin >> length >> endl;   // length = 20;   different value of length compared to CRectangle
}

int main () 
{
  // Step 1: Input for rectangle.... 
  UserInput_rect(); 

  // Step 2: Input for square....
  UserInput_square();
  
  // Step 3: Best solution to initialise "width = 3" and "length = 5" with variables from CRectangle????
  initialise_rect()???
  
  // Step 4: Calculating perimeter of rectangle...
  calc_perimeter(); // of rectangle
  
  // Step 5: cout results for perimeter of rectangle...
  cout << "Perimter for rectangle is = " << perimeter << " m." << endl;
  
  // Step 6: Best solution to initialise "width = 10" and "length = 20" with variables from CSquare???
  initialise_square()???
  
  // Step 7: Calculating perimeter of square...
  calc_perimeter(); // of square
  
  // Step 8: cout results for perimeter of square...
  cout << "Perimter for square is = " << perimeter << " m." << endl;
}


Unfortunately the sequence/program flow must be kept this way and it is fixed. Therefore I would like to get some advice on the possible best programming practice to initialise the appropriate values for variables right before calculation begins.

I have tried using a global array of "Square_parameters[2]/Rectangle_parameters[2]" or even structs to initialise but I was told by my peers that I am duplicating data within the class which is not memory efficient (in my program I have 22 double variables).

Also, I also have problems calling the function say in Step 1. Shall I be adding "CRectangle::" before the function?

Many thanks!

Cheers

Jawsh
Last edited on
1
2
3
4
CRectangle an_instance_or_object_of_class_rectangle;
an_instance_or_object_of_class_rectangle.UserInput();
int perimeter = an_instance_or_object_of_class_rectangle.calc_perimeter();
cout << "The perimeter of the rectangle is " << perimeter << " m." << endl;
You seem to be trouble with the concept of class and instance of a class.

You will also notice that I changed the prototype of calc_perimeter so it returns the perimeter. That's because you don't have any way of accessing it from outside class methods

By the way, in squares width == length
I'm not completely sure what you're trying to do... It looks like you're making a program to calculate the perimeters of squares and rectangles.

The way you have the square and rectangle objects defined right now, I don't see any real difference between them so I would think you'd just use one class for both.

Why not change the square initialization and perimeter calculation to use only one side, and assume each of the sides is equal?

Will this program do anything beside calculate perimeters?
Thank you both for your comments!

Yes I am aware that width and length of a square are both the same thing.

I think I didnt quite clearly explain myself. I used "calculating a perimeter" of a square an rectangle merely as an example but it seems that it has confused people! Sorry for that!
Topic archived. No new replies allowed.