How do I overload variables in derived classes?

Is it possible to overload a variable in a derived class? Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Circle
{
  int radius() const { return r; }
private:
  int r;
}

struct Smiley : Circle
{
  // inherits the function int radius() const, but doesn't return Smiley::r
private:
  int r;
}


Thanks in advance to anyone who replies!
It is not an overloading. The derived class has two data members with the same name r one of which is accessible in it and other is not accessible directly. I do not see any sense in such a declaration. It follows that Circle has its own radius and Smiley has its own additional radius. So one figure has two radiuses.
Last edited on
To illustrate this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

struct Circle
{
  int radius() const { return r; }
  int r;
};

struct Smiley : Circle
{
  int r;
};

int main()
{
    Smiley x;
    x.r = 1;
    x.Circle::r = 2;
    std::cout << x.r << std::endl << x.radius() << std::endl << x.Circle::r;
}

1
2
2
Thanks guys, now I get it. Here's my solution:

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
#include <iostream>

using namespace std;

struct Circle
{
  Circle(int arg_radius) : r(arg_radius) { }
  int radius() const { return r; }
protected:
  int r; // not accessible to the user, but accessible to derived classes
}

struct Smiley : Circle
{
  Smiley(int arg_radius)
  {
    Circle::r = arg_radius
  }
}

int main()
{
  Circle obj_c(5);
  Smiley obj_s(10);
  cout << obj_c.radius() << "\n";
  cout << obj_s.radius() << "\n"; // show's correct radius
}
Better, would be:

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
#include <iostream>

using namespace std;

struct Circle
{
  Circle(int arg_radius) : r(arg_radius) { }
  int radius() const { return r; }
protected:
  int r; // not accessible to the user, but accessible to derived classes
};

struct Smiley : Circle
{
  Smiley(int arg_radius) : Circle(arg_radius)
  {
  }
};

int main()
{
  Circle obj_c(5);
  Smiley obj_s(10);
  cout << obj_c.radius() << "\n";
  cout << obj_s.radius() << "\n"; // show's correct radius
}
There is no need to assign variable r of class Circle protected access control because class Circle provides public interface to the variable via member function radius. So it would be better to define classes the following way

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 <iostream>

using namespace std;

struct Circle
{
  Circle(int arg_radius) : r(arg_radius) { }
  int radius() const { return r; }
private:
  int r;
};

struct Smiley : Circle
{
  Smiley(int arg_radius) : Circle( arg_radius ) {}
};

int main()
{
  Circle obj_c(5);
  Smiley obj_s(10);
  cout << obj_c.radius() << "\n";
  cout << obj_s.radius() << "\n"; // show's correct radius
}
@vlad from moscow

I modified Circle and made int r private. This is the first time I see that you can do this:

 
Smiley(int arg_radius) : Circle(arg_radius) { }


Everything works! How does the line of code above work exactly? I mean I know what it does, it's just I haven't seen it before. Thanks in advance!
@benbalach
Everything works! How does the line of code above work exactly? I mean I know what it does, it's just I haven't seen it before. Thanks in advance!

Here the base class constructor with parameter is called explicitly, Otherwise the compiler would try to call the default base class constructor which is absent,
The similar effect could be achived if to use using declaration in the drived class.

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 <iostream>

using namespace std;

struct Circle
{
  Circle(int arg_radius) : r(arg_radius) { }
  int radius() const { return r; }
private:
  int r;
};

struct Smiley : Circle
{
  using Circle::Circle;
};

int main()
{
  Circle obj_c(5);
  Smiley obj_s(10);
  cout << obj_c.radius() << "\n";
  cout << obj_s.radius() << "\n"; // show's correct radius
}


But not all compilers support this feature yet.
Last edited on
Topic archived. No new replies allowed.