Does the polymorphism affects operators overloading?

I need some help, I must overload [] but at the same time I must use polymorphism. I wonder if using polymorphism affects operators overloading since when I modified the first class by writing "virtual":
1
2
virtual void mostrarDatos(char*, char*, char*);
virtual void calcularEdad(int);


so I can do the polymorphism, it affects the part of the code where suppose to do an addition:

1
2
3
s=student1+=student2;
t=student3+=student4;
u=s+=t;


if I do that, it shows some strange numbers instead of the right ones. What am I doing wrong?

Here is the complete code:

.h
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
#ifndef PERSON_H
#define PERSON_H

#include <iostream>
using namespace std;
class persona
{
private:
    int anio;
    int edad;
    char* direccion;
    char* nombre;
    char* carrera;
public:
    persona();
    ~persona();
    void mostrarDatos(char*, char*, char*);
    void calcularEdad(int);
};

class student:public persona
{
public:
    student();
    ~student();
    student(int&);
    student(const int&);
    void mostrarDatos(char*, char*, char*);
    void calcularEdad(int);
    int &operator[](int n);
    char* &operator[](char* n);
    friend ostream& operator<< (ostream & out,const student & a);
    friend student & operator+=(const student &a, const student &b);
    friend student & operator+=(const student & a, int o);
    friend student & operator+=(int o, const student &b);
protected:
    int c[4];
    int t;
    char* p[4];
    int aniost;
    int edadst;
    char* direccionst;
    char* nombrest;
    char* carrerast;
};
#endif 


.cpp

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
#include <iostream>
#include "person.h"
using namespace std;

persona::persona()
{
    anio=0;
    edad=0;
    direccion=new char;
    carrera=new char;
    nombre=new char;
}

persona::~persona(){}

void persona::mostrarDatos(char* p1, char* p2, char* p3 )
{
    direccion=p1;
    carrera=p2;
    nombre=p3;
    cout<<"Direccion: "<<p1<<"\nCarrera: "<<p2<<"\nNombre: "<<p3<<endl;
}

void persona::calcularEdad(int pe)
{

    edad=2013-pe;
    cout<<"Edad: "<<edad<<endl;
}

student::student()
{
    t=0;
    p[1]=new char;
c[1]=0,c[2]=0,c[3]=0,c[4]=0;
    aniost=0;
    edadst=0;
    direccionst=new char;
    carrerast=new char;
    nombrest=new char;

}

student::student(int& data)
{
    c[1]=data;
    c[2]=data;
    c[3]=data;
    c[4]=data;
}


student::~student(){}

void student::mostrarDatos(char* s1, char* s2, char* s3 )
{
    direccionst=s1;
    carrerast=s2;
    nombrest=s3;
    cout<<"Direccion alumno: "<<s1<<"\nCarrera alumno: "<<s2<<"\nNombre alumno: "<<s3<<endl;
}

void student::calcularEdad(int pe)
{

    edadst=2013-pe;
    cout<<"Edad: "<<edadst<<endl;
}

int & student::operator[](int n)
    {
        cout<<" []"<<endl;
    return c[n];
    }

char* & student::operator[](char* n)
    {
        p[1]=n;
        cout<<" []"<<endl;
    return p[1];
    }

ostream & operator<< (ostream & out,const student & a)
{

out << a.c[1];

cout<<"<<"<<endl;

    return out;
}

student & operator+=(const student & a, const student & b)
{
    student t;
    t.c[1] = b.c[1]+ a.c[1];
    cout<<"usamos operador +="<<endl;

    return t;

}

student & operator+=(const student & a, int o)
{
    student t;
    t.c[1] = a.c[1] + o;
    cout<<"usamos operador +="<<endl;
    return t;

}

student & operator+=(int o,const student & b)
{
    student t;
    t.c[1] = o + b.c[1];
    cout<<"usamos operador +="<<endl;
    return t;

}


main

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
#include <iostream>
#include "person.h"

using namespace std;

int main()
{
    persona *p;
    p=new student();
    p->mostrarDatos("street, 3237","Ing en Sistemas","Maria");
    p->calcularEdad(1992);

student a1,a2;
student student1,student2,student3,student4;
student s,t,u,v;
cout<<"Ingresa calificaciones parciales: \n";

cin>>a1[1];
cin>>a1[2];
cin>>a1[3];
cin>>a1[4];

student1=a1[1];
student2=a1[2];
student3=a1[3];
student4=a1[4];

cout<<"calf 1: "<<student1<<endl;
cout<<"calf 2: "<<student2<<endl;
cout<<"calf 3: "<<student3<<endl;
cout<<"calf 4: "<<student4<<endl;

s=student1+=student2;
t=student3+=student4;
u=s+=t;

cout<<s;
cout<<t;
cout<<u;
system ("pause");
}

Only polymorphic methods are chosen based on the class's true identity. Hence, you must make your operator[] virtual, and overload it in the derived classes.

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

struct point2
  {
  virtual const char* name() const { return "point"; }
  int x, y;
  point2(): x( 0 ), y( 0 ) { }
  point2( int x, int y ): x( x ), y( y ) { }
  virtual ~point2() { }
  virtual int& operator [] ( int index )
    {
    switch (index)
      {
      case 0: return x;
      case 1: return y;
      default: throw 1;
      }
    }
  };

struct point3: public point2
  {
  virtual const char* name() const { return "point3"; }
  int z;
  point3(): point2(), z( 0 ) { }
  point3( int x, int y, int z ): point2( x, y ), z( z ) { }
  virtual ~point3() { }
  virtual int& operator [] ( int index )
    {
    switch (index)
      {
      case 2:         return z;
      case 0: case 1: return point2::operator [] ( index );
      default:        throw 2;
      }
    }
  };

int main()
  {
  point2* pt = new point3( 2, 3, 5 );

  cout << (*pt)[ 2 ] << endl;

  delete pt;
  }

Hope this helps.
yes I finally get it.
Thanks :)
Topic archived. No new replies allowed.