inheriance

How can we access to private attributes in base or parent class from child class ?

A class contains data and provides an interface through which data can be manipulated.

1
2
3
4
class child : public map<string,BASE>
{
  // ...
};

Note: The child inherits map. What private bits of map you would need to access (but can't)?
If something does not compile, then it surely does not have valid syntax. (Note that logically wrong code could compile just fine.)

When something does not compile, the compiler does give an explanation -- an error message.
We can't explain you the explanation, if you don't show it.

However, lets look at:
1
2
std::map<std::string,Base> abd;
abd.insert( "banana", "mongo" );

Is that how one inserts to map?
Lets see: http://www.cplusplus.com/reference/map/map/insert/
The answer is clearly "no".

Now, lets look at other details:
1
2
3
4
5
6
7
8
9
class child : public map<string,BASE>
{
  // ...
  void Add( Base b ) {
    map<string,Base> abd;
    abd.insert( "banana", "mongo" );
  }
  // ...
};

BASE is not Base. Could be just a rushed typo. Those are nasty.

The function takes parameter b. Why? It is not used in the function.

The function creates a local map variable abd. That map is destroyed at the end of the function Add. The function does not change the state of child object in any way.

The child is a map. If child is a map, then why doesn't Add() add elements to child? One could have:

1
2
3
child example;
Renseignements sample;
example.insert( std::pair<string,BASE>( "banana", BASE("mongo", sample) ) );

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: You can edit your post and add code tags.
Considering:

1
2
3
4
multimap < Abn,const string> abo;
    typedef pair < Abn,const string> duo;
    typedef multimap < Abn,const string> ::iterator iter;
   abo[Abn(a.nom,a.info)]=a.nom;


This says that the "Abn" type is the key, but how is it compared?

I think you intend

multimap< string, Abn > abo;

so that

abo[ a.nom ] = Abn( a.nom, a.info );

means that "a.nom" is the key to find an Abn stored in the map.

That is to say, a string object already has a known comparison which will be assumed by the multimap.

There is no known comparison for an Abn, unless you make such a comparator and provide it as a parameter the multimap template (see multimap documentation for the comparison paramter).


Also:

1
2
3
class child :public map<BASE,string>{

}


I seriously doubt you intend to derive from a map. It is more likely that "child" should own a map.

Unless I misunderstand what you intend for "child"
Last edited on
it does not work

There is no such thing in programming. Something can either produce expected result or fail to produce expected result. In the latter case there must be clear description of how result differs from expected.

In the case of compiling source code into executable one typical failure case is that compiler writes error message (the description) and aborts.

You must either post the verbatim error messages or post code that we can compile.

Earlier, you had:
1
2
3
class child :public map<BASE,string>{

}

Now you did show:
1
2
3
class child :public map<string,BASE>{

}

Why the change?

Why does class child inherit std::map at all? Why not:
1
2
3
4
class child {

  map<BASE,string> data_member;
}


What is the mystery typename Abn?
THIS IS THE ERROR MESSAGE :
../src/child.cpp:32:15: error: binding reference of type ‘std::string&’ {aka ‘std::__cxx11::basic_string<char>&’} to ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} discards qualifiers
   32 |     abo[BASE(a.nom,a.info )] = a.nom;
      |      

AND THIS IS THE IMPLIMENTATION TRIED
abo[BASE(a.nom,a.info )] = a.nom;

AND THIS IS MY BASE CLASE CONSTRUCTOR
1
2
3
4
5
6
7
8
9
10
11
BASE::BASE( std::string& name,Renseignements& r)
// Algorithme :
//
{


  this->nom =name;

   info=r.prenom+"|"+r.adresse+"|"+r.noTelephone;

}

and Renseignements is struct which contain three string Prenom,Adresse,noTelephone.with a constractor
1
2
3
struct Renseignements {
string Prenom,Adresse,noTelephone;
Renseignements (p1="MARK",p2="21 VEGAS",p3="0987765544"): Prenom(p1),Adresse(p2),noTelephone(p3){}}

and for my BASE class
1
2
3
4
5
6
7
8
class BASE{
public:
BASE( std::string& name,Renseignements& r);
friend class child;
private
renseignement info;
string nom;
}
Last edited on
Topic archived. No new replies allowed.