Class inheritence



  • Hello.
    First of all I would like to apologise you because I am speaking english, but I can't speak dutch, althought I can understand a while.
    anyway... I am constructing a class inheritence but I have a problem whitch I ask you if you can help me.
    The lowest class needs to increment a value in the upper one.. how can I do it?
    The below code is class declaration 😉
    can you help me please? 😉 thank you

    struct StructINIDetail{
        unsigned int IDDetail;
        unsigned int IDSection;
        unsigned int Type;
        AnsiString Name;
        AnsiString Value;
    };
    
    class INIDetail {
        private:
        	unsigned int _IDSection[MAX_DETAILS];
        	unsigned int _IDDetail[MAX_DETAILS];
        	unsigned int _Type[MAX_DETAILS];
        	AnsiString _Name[MAX_DETAILS];
        	AnsiString _Value[MAX_DETAILS];
        public:
            unsigned int Count;                 //Count only returns the total of used spaces in the String
            unsigned int This;
    
            //Methods
            INIDetail();
            void Add(struct StructINIDetail);
            void Remove(int); //Int Using is This
            void First(unsigned int);
            void Last(int);
            unsigned int IDDetail(unsigned int);
            unsigned int IDSection(unsigned int);
            unsigned int Type(unsigned int);
            AnsiString Name(AnsiString);
            AnsiString Value(AnsiString);
            struct StructINIDetail GetStructure();
    };
    
    class Detail : protected INIDetail{
        public: INIDetail* Details = new INIDetail();
    };
    struct StructINISection{
        int IDSection;
        unsigned int TotalDetails;
        AnsiString Name;
    };
    
    class INISection : public Detail{
        private:
          	int _IDSection[MAX_SECTIONS];
            unsigned int _TotalDetails[MAX_SECTIONS];
          	AnsiString _Name[MAX_SECTIONS];
    
        public:
            unsigned int Count;
            unsigned int This;
    
            //Methods
            INISection();
            void Add(struct StructINISection);
            void Remove(int); //Int Using is This
            void Last(int);
            void First(unsigned int);
            AnsiString Name(AnsiString);
            unsigned int TotalDetails(int);
            unsigned int IDSection(int);
            struct StructINISection GetStructure();
    };
    

    thank you



  • First of all, please name the classes which are relevant for the question (I see five classes of which three are inherited from each other).

    Second: INISection can access any public and protected data members inherited from Detail (and indirectly from INIDetail), so it's no problem to update its Count and This members (since you've redefined them, you should use the scope operator as in "INIDetail::Count" for this access).

    Third: Are you sure, this class hierarchie is correct - i.e. are you sure, there is a "is-a" relationship between INISection and Detail?

    PS: And nearly none of us is understanding Dutch - since this is a German board 😃



  • oh ... sorry
    I changed the code I took out the Details class and I add it as a property in INISections
    so the bellow code is now in use:

    struct StructINIDetail{
        unsigned int IDDetail;
        unsigned int IDSection;
        unsigned int Type;
        AnsiString Name;
        AnsiString Value;
    };
    
    class INIDetail {
        private:
        	unsigned int _IDSection[MAX_DETAILS];
        	unsigned int _IDDetail[MAX_DETAILS];
        	unsigned int _Type[MAX_DETAILS];
        	AnsiString _Name[MAX_DETAILS];
        	AnsiString _Value[MAX_DETAILS];
        public:
            unsigned int Count;                 //Count only returns the total of used spaces in the String
            unsigned int This;
    
            //Methods
            INIDetail();
            void Add(struct StructINIDetail);
            void Remove(int); //Int Using is This
            void First(unsigned int);
            void Last(int);
            unsigned int IDDetail(unsigned int);
            unsigned int IDSection(unsigned int);
            unsigned int Type(unsigned int);
            AnsiString Name(AnsiString);
            AnsiString Value(AnsiString);
            struct StructINIDetail GetStructure();
    };
    
    struct StructINISection{
        int IDSection;
        unsigned int TotalDetails;
        AnsiString Name;
    };
    
    class INISection {
        private:
          	int _IDSection[MAX_SECTIONS];
            unsigned int _TotalDetails[MAX_SECTIONS];
          	AnsiString _Name[MAX_SECTIONS];
    
        public:
            unsigned int Count;
            unsigned int This;
            INIDetail* Details = new INIDetail();
    
            //Methods
            INISection();
            void Add(struct StructINISection);
            void Remove(int); //Int Using is This
            void Last(int);
            void First(unsigned int);
            AnsiString Name(AnsiString);
            unsigned int TotalDetails(int);
            unsigned int IDSection(int);
            struct StructINISection GetStructure();
    };
    

    I'm sorry about the imprecise words.
    When Add() function was called, it should increment a INISection::TotalDetails ...

    for example

    INISection* sec = new INISection();
    unsigned int total = sec->TotalDetails(); //total = 0
    sec->Details->Add("bla bla bla");
    total = sec->TotalDetails(); //total = 1
    

    but it actualy doesn't do it 😕



  • Before we continue - how ist your knowledge of C++ and object orientated programming? Because as far as I see, your program isn't even syntactical correct C++ code (this starts by initialising members on declaration).

    Next - instead of working with plain pointers and new/delete you should use simple variables and vector<>s for storing your data (they also remember their actual size and can grow as nessesary).



  • My Object Oriented progamming experience is not longer at all...
    I have some "brein conflits" with pointers or vectors usage, because with C I always use pointer for strings, for example char* str, and it worked...

    now I am trying, more usualy to use vectors instead of pointers, I try to use it always when necessary, but :s for sure there are situations where it shouldn't be there and it is...
    anyway ... so, declaration, INIDetail* Detail = new INIDetail(); shouldn't exist so?



  • Guilherme schrieb:

    My Object Oriented progamming experience is not longer at all...
    I have some "brein conflits" with pointers or vectors usage, because with C I always use pointer for strings, for example char* str, and it worked...

    Yes, it works for pointers, but it's far more complicated not to run into problems like SegFault.

    anyway ... so, declaration, INIDetail* Detail = new INIDetail(); shouldn't exist so?

    Not at this position - if you want to initialize some members of your class, use its constructor to do this work.



  • something like this?

    INISection::INISection(){
        Count = 0;
        This = 0;
        Details->INIDetail();
    }
    


  • Guilherme schrieb:

    something like this?

    INISection::INISection(){
        Count = 0;
        This = 0;
        Details->INIDetail();
    }
    

    Nearly - the last line should be "Details = new INIDetail;" if you really need to use a pointer here. I'd prefer to use a simple Member "INIDetail Details;" instead.



  • Why?
    I think I don't need pionter because I won't use it like a vector (and there is only one, no other to point to :x ... whow problems with concepts)

    so when INIDetail Details = new INIDetail it will call the constructor. yeh.. :x..... need to study :x lool



  • Guilherme? schrieb:

    Why?
    I think I don't need pionter because I won't use it like a vector (and there is only one, no other to point to :x ... whow problems with concepts)

    If you don't need a pointer, just declare it as a plain variable - so what's the problem?

    so when INIDetail Details = new INIDetail it will call the constructor. yeh.. :x..... need to study :x lool

    new creates an object and calls its ctor - but it creates it on the heap and gives you a pointer to its location. When you use "INIDetail Detail;" it simply creates an object on stack or inside your class and still calls its ctor.
    And the good thing on this local objects is - they are fully controlled by the programm and are automatically destroyed when their function ends (objects on the heap stay alive until you destroy them explicitely by calling delete - possibly forever if you loose their adress).



  • hum ... thanks...

    instead of every code ... 😛 which part should I change? ...
    actualy I took out the INIDetail * Details declaration, so as I could understand it will call the constructor anyhow and manage it automaticly, this is good because not only I don't know working with memory, I don't need it at all (explicit work) in this program, it simply will read .ini files and change it.



  • Next you should replace the Arrays inside your classes by vector<StructINIDetail> resp. vector<StructINISection> and call their methods inside the Add(), Remove() etc.

    (btw, how is the desired relation between INI-Section and INI-Detail?)



  • supostly a section can have MAXDETAILS details in it.. :x



  • I'm sorry for this borring question, but are you refering to

    unsigned int _IDSection[MAX_DETAILS];
        	unsigned int _IDDetail[MAX_DETAILS];
        	unsigned int _Type[MAX_DETAILS];
        	AnsiString _Name[MAX_DETAILS];
        	AnsiString _Value[MAX_DETAILS];
    

    change it to vector<StructINIDetail> ???? instead of Arrays?



  • Guilherme schrieb:

    supostly a section can have MAXDETAILS details in it.. :x

    In this case, there should be more then only one INIDetail object inside your INISection class (at the moment, a section has one Detail and (up to) MAXDETAIL Section-data Elements:

    struct sIniDetail
    {
      ...
    }
    
    class INIDetail
    {
      sIniDetail data;//one Detail has only one Detail-data part
    ...
    };
    
    struct sIniSection
    {
      ...
    };
    
    class INISection
    {
      sIniSection data;//dito - one data part for the section
      vector<INIDetail> details;//unlimitied Detail elements
    public:
      void Add(const INIDetail& element)
      {
        details.push_back(element);
      }
    ...
    };
    

    (and maybe you can remove the two struct's completely and store their members direct inside the corrosponding classes)



  • Hum 🙂 ... this is cool...
    I didn't know vectors untill we start talking about it, I'll study how do they work and so implement it.
    That way it means there is only a section for infinit details. so ... if I want to create more than a section I'll need to create that number of objects right?! but there isn't allowd to use vector that case.
    so ... I was wondering this way.
    I'm outter code declaire a new Section for each [SECTION] in file, but how do I access them? how can I know how much objects from a sertain class do I have?



  • Guilherme schrieb:

    Hum 🙂 ... this is cool...
    I didn't know vectors untill we start talking about it, I'll study how do they work and so implement it.
    That way it means there is only a section for infinit details. so ... if I want to create more than a section I'll need to create that number of objects right?! but there isn't allowd to use vector that case.
    so ... I was wondering this way.
    I'm outter code declaire a new Section for each [SECTION] in file, but how do I access them? how can I know how much objects from a sertain class do I have?

    One INISection object represents one section from your INI file - if you need more than one, you could still use a vector<INISection> somewhere in your programm (or a class 'INIFile' which encapsulates all sections like the INISection encapsulates all details).

    btw, maybe you could use a map<> to have a direct corrospondence between a section's name and its contained data (and between a detail's name and its value).



  • hum... I though vector<> didn't allowd to access classes only DataTypes 🙂 ... if a class a DataType?! 😕 lool

    well 😉 map<> so will be a nice idea refering to what you said 🙂

    thank you a lot.

    By the way, do you know a nice book whitch go through these subjects?
    thank you once more 😉



  • if you want to learn about the C++ Library, you could use this one:
    The C++ Standard Library | ISBN: 0201379260
    (for general concepts (syntax, class design etc) you should search the web for some tutorials and books - maybe you also could follow the link "Bücher" here (on top of the page), even if most of the books are written in German)

    edit: C++ containers allow any kind of data inside them, as long as they follow some preconditions (copieable and sometimes comparable) - even your classes.



  • One more, thank you a lot.
    I think I'll borrow you many more times 🙂 ...


Anmelden zum Antworten