Like Tree1Likes
  • 1 Post By Salem

diamond inheritance problem

This is a discussion on diamond inheritance problem within the C++ Programming forums, part of the General Programming Boards category; hello everyone. i'm trying to do a diamond inheritance problem. Code: #include<iostream.h> #include<string.h> #include<conio.h> class STUDENT { protected : char ...

  1. #1
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    diamond inheritance problem

    hello everyone.

    i'm trying to do a diamond inheritance problem.

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<conio.h>
    
    class STUDENT
    {
        protected :
    
            char *name;
            int age;
    
        public :
    
            STUDENT()
            {
                name=new char[strlen("No Name")];
                strcpy(name,"NO Name");
                age=5;
            }
    
            STUDENT(char *str,int no)
            {            
                name=new char(strlen(str));
                strcpy(name,str);
                age=no;
            }
    
            virtual void getDetails()
            {
                cout<<"Name : "<<name<<"\nAge : "<<age;
            }
    
            virtual char * getName()
            {
                return name;
            }
    
            virtual int getAge()
            {
            return age;
        }
    };
    
    class SSC : virtual public STUDENT
    {
        protected :
    
            int ssc_marks;
    
        public :
    
            SSC() : STUDENT()
            {
                ssc_marks=1;
            }
    
            SSC(char * str, int n1, int n2) : STUDENT(str,n1)
            {
                ssc_marks=n2;
            }
    
            void getDetails()
            {
                STUDENT :: getDetails();
                cout<<"\nSSC Marks : "<<ssc_marks;
            }
    
            int getSSCMarks()
            {
                return ssc_marks;
            }
    
    };
    
    class HSC : virtual public SSC
    {
        protected :
    
            int hsc_marks;
    
        public :
    
            HSC() : SSC()
            {
                hsc_marks=1;
            }
    
            HSC(char * str, int n1, int n2, int n3) : SSC(str,n1,n2)
            {
                hsc_marks=n3;
            }
    
            void getDetails()
            {
                SSC :: getDetails();
                cout<<"\nHSC Marks : "<<hsc_marks;
            }
    
            int getHSCMarks()
            {
                return hsc_marks;
            }
    };
    
    class CET : virtual public SSC
    {
        protected :
    
            int cet_marks;
    
        public :
    
            CET() : SSC()
            {
                cet_marks=1;
            }
    
            CET(char * str, int n1, int n2, int n3) : SSC(str,n1,n2)
            {
                cet_marks=n3;
            }
    
            void getDetails()
            {
                SSC :: getDetails();
                cout<<"\nCET Marks : "<<cet_marks;
            }
    
            int getCETMarks()
            {
                return cet_marks;
            }
    };
    
    class ENGINEERING : public HSC, public CET
    {
        protected :
        
            double cpi;
    
        public :
        
            ENGINEERING() : HSC(),CET()
            {
                cpi=1.0;
            }
    
            ENGINEERING(char * str, int n1, int n2, int n3, int n4, double n5) : HSC(str,n1,n2,n3), CET(str,n1,n2,n4)
            {
                cpi=n5;
            }
    
            void getDetails()
            {
                HSC :: getDetails();
                cout<<"\nCET Marks : "<<CET :: getCETMarks()<<"\nCPI : "<<cpi;
            }
    
            int getCPI()
            {
                return cpi;
            }
    };
    
    int main()
    {
        clrscr();
        ENGINEERING s2("gaurav",19,85,72,135,8.5);
        s2.getDetails();
    
        getch();
    
        return 0;
    }
    i get output :

    *********************************
    Name : NO Name
    Age : 5
    SSC Marks : 1
    HSC Marks : 72
    CET Marks : 135
    CPI : 8.5
    *********************************

    the values in the bold are for the default constructor which i'm not expecting. I think i'm missing the concept. please help me.

    P.S. : I used turbo c++ compiler if it matters in this case.

    thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > P.S. : I used turbo c++ compiler if it matters in this case.
    Only that it causes brain rot.

    > name=new char[strlen("No Name")];
    > strcpy(name,"NO Name");
    You're already in trouble, because you didn't count the \0 at the end of the string.
    So when you copied it, it's all gone wrong.

    Use a better compiler, drop the conio.h, and use std::string instead of char arrays.
    Elysia likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You didn't happen to read through the Virtual Inheritance tutorial found on this site before posting your question did you?

    (HINT: I am telling you to read the article)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,499
    Along with what Salem said (GET RID OF TURBO C++ and get a good compiler to learn standard C++) :

    You should read [25] Inheritance -- multiple and virtual inheritance, C++ FAQ
    It basically says; you need to pass on the constructor arguments to CET or STUDENT directly from ENGINEERING.

    Also, there is no need to inherit STUDENT as virtual, doing so to SSC is enough.

    _________________________________________
    And (following Salem's example) : "For further reference: http://www.cplusplus.com/forum/beginner/48544/ " !!
    Last edited by manasij7479; 08-11-2011 at 02:38 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    thanks everyone. it helped lot.

    one more question - can you recommend me a free lightweight compiler for c++ ? I have Pelles C and it's really good(for C). I have NetBeans IDE 7.0 but it asks to add external c/c++ compiler.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    Quote Originally Posted by gaurav9991
    I have NetBeans IDE 7.0 but it asks to add external c/c++ compiler.
    Assuming that you are using Windows, you can use the MinGW port of g++. There is an old Netbeans article related to this: Introducing Netbeans C/C++ Pack. You may wish to go to the MinGW site to obtain an updated version of MinGW.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    thanks laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me with this diamond problem
    By 843 in forum C Programming
    Replies: 5
    Last Post: 10-17-2010, 03:35 AM
  2. Diamond problem
    By kollurisrinu in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2010, 07:15 AM
  3. Inheritance Problem
    By audinue in forum C++ Programming
    Replies: 18
    Last Post: 08-15-2009, 02:49 PM
  4. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 11:14 AM
  5. A diamond problem
    By loser in forum C++ Programming
    Replies: 6
    Last Post: 02-21-2002, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21