Thread: Inheritance of objects and linked list

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    Inheritance of objects and linked list

    Hello everyone,recently I have been trying out a C++ program and it seems that its going beyond my thinkings.So I want your help to solve it out.This program is to create a derived class "master" from three base classes person,account and admin,and to create,update and display the objects.So I have used a linked list to make it dynamic.
    For the linked list, I have used a class "node" that contains a class master object 'm' as information part and '*link' is the linking part.
    So in the main function I have used a master object m1 and after getting the required information for m1,I am using it to assign it to object m of class node and linking it in the create() function.But the main problem(as i think) is with the assignment of m1 to m since I am using a pointer temp for it.Since it is wrong to assign directly,I have tried both overloading operator'=' and a copy() function to copy elements.But I was not successful in doing so.The program runs fine but it stops after taking the values of m1.

    So friends,please help me solve it out.
    Thanks.
    Code:
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define nm_sz 50
    #define sz 30
    using namespace std;
    class person{
            protected:
                    char name[nm_sz];
                    int code;
            public:
                    void get_person(){
                            fflush(stdin);
                            cout<<"\n\nPERSON INFO\n\nNAME:  ";
                            cin>>name;
                            cout<<"CODE:  ";
                            cin>>code;
                            return;
                    }
                    void get_name(){
                            cout<<name;
                    }
                    void show_person(){
                            cout<<"\n\n\tPERSON INFO\n\n";
                            cout<<"NAME:  "<<name;
                            cout<<"\nCODE:  "<<code;
                            return;
                    }
    };
    class account:virtual public person{
            protected:
                    int acc_no;
                    float total_pay;
            public:
                    void get_acc(){
                            cout<<"\nACCOUNT INFO\n\n";
                            cout<<"ACCOUNT NO.:  ";
                            cin>>acc_no;
                            cout<<"TOTAL PAY:  ";
                            cin>>total_pay;
                            return;
                    }
                    void show_acc(){
                            cout<<"\n\n\tACCOUNT INFO\n\n";
                            cout<<"A/C No.:  "<<acc_no;
                            cout<<"\nTOTAL PAY:  "<<total_pay;
    						return;
                    }
    };
    class admin:virtual public person{
            protected:
                    char qual[sz];
                    int age;
                    float exp;
            public:
                    void get_adm(){
    						fflush(stdin);
                            cout<<"\n\nADMIN INFO\n\n";
                            cout<<"QUALIFICATION:  ";
                            cin>>qual;
                            cout<<"AGE:  ";
                            cin>>age;
                            cout<<"YEARS OF EXPERIENCE:  ";
                            cin>>exp;
                            return;
                    }
                    void show_adm(){
                            cout<<"\n\n\tADMIN INFO\n\n";
                            cout<<"QUALIFICATION:  "<<qual;
                            cout<<"\nAGE:  "<<age;
                            cout<<"\nYEARS OF EXPR:  "<<exp;
                            return;
                    }
    };
    class master:public account,public admin{
            public:
                    void get_data(){
                            get_person();
                            get_acc();
                            get_adm();
                    }
                    void display(){
                            show_person();
                            show_acc();
                            show_adm();
                    }
                    void operator=(master mo)
                    {
                            strcpy(name,mo.name);
                            code=mo.code;
                            acc_no=mo.acc_no;
                            total_pay=mo.total_pay;
                            strcpy(qual,mo.qual);
                            age=mo.age;
                            exp=mo.exp;
                            return;
                    }
                    void copy(master m2){
                            strcpy(name,m2.name);
                            code=m2.code;
                            acc_no=m2.acc_no;
                            total_pay=m2.total_pay;
                            strcpy(qual,m2.qual);
                            age=m2.age;
                            exp=m2.exp;
                            return;
                    }
    };
    class node{
            public:
                    master m;
                    node *link;
    };
    void create(node **q,master m2);
    int count(node *q);
    void update(node **q,int x);
    void dsply(node *q);
    int main()
    {
            node *p;
            master m1;
            int res,res2;
            p=NULL;
            do{
                    cout<<"\nWHAT YOU WANT TO DO?\n";
                    cout<<"0 : END\t1 : CREATE\t2 : UPDATE\t3 : DISPLAY\n\n";
                    cin>>res;
                    switch(res){
                            case 0:
                                    cout<<"\n\nBYE BYE";
                                    //return 0;
    								break;
                            case 1:
                                    m1.get_data();
                                    create(&p,m1);
                                    break;
                            case 2:
                                    if(p==NULL)
                                            cout<<"\nTHERE ARE NO DATAS TO BE UPDATED---DATA EMPTY!!!";
                                    else{
                                            int no=count(p);
                                            cout<<"\nTHERE ARE DATAS OF "<<no<<"PERSONS\n";
                                            cout<<"\nWHICH ONE DO YOU WANT TO UPDATE?\n";
                                            cin>>res2;
                                            update(&p,res2);
                                    }
                                    break;
                            case 3:
                                    if(p==NULL)
                                            cout<<"\nNO DATA TO BE DISPLAYED\n";
                                    else
                                            dsply(p);
                                    break;
                            default:
                                    cout<<"\n\nINVALID INPUT!!!\n";
                                    break;
                    }
            }while(res!=0);
            return 0;
    
    }
    void create(node **q,master m2){
            node *temp,*r;
            if(*q==NULL){
                    temp=(node *)malloc(sizeof(node));
                    //temp->m=m2;
                    //temp->m.copy(m2);
                    temp->m=m2;
                    temp->link=NULL;
                    *q=temp;
            }
            else{
                    temp=*q;
                    while(temp->link!=NULL)
                            temp=temp->link;
                    r=(node *)malloc(sizeof(node));
                    r->m=m2;
                    r->link=NULL;
                    temp->link=r;
            }
            return;
    }
    int count(node *q){
            int i=1;
            while(q->link!=NULL){
                    cout<<endl<<i<<" : ";
                    q->m.get_name();
                    i++;
    				q=q->link;
            }
            return i;
    }
    
    void update(node **q,int x){
            int i=1;
            int r;
            while(i!=x){
                    *q=(*q)->link;
                    i++;
            }
            cout<<"\nWHICH PROFILE DO YOU WANT TO UPDATE?\n";
            cout<<"1-PERSONAL PROFILE       2-A/C PROFILE   3-ADMIN PROFILE\n";
    		cin>>r;
            switch(r){
                    case 1:
                            (*q)->m.get_person();
                            break;
                    case 2:
                            (*q)->m.get_acc();
                            break;
                    case 3:
                            (*q)->m.get_adm();
                            break;
                    default:
                            cout<<"\nAMBIGUIOUS\n";
                            break;
            }
            return;
    }
    void dsply(node *q){
            do{
                    q->m.display();
                    //q=q->link;
            }while((q=q->link)!=NULL);
            return;
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    temp=(node *)malloc(sizeof(node));
    The problem is probably just this. You just can't allocate a non-trivial object with malloc and start using it, because the object (and its members) have never been constructed. I suppose that having all the virtual inheritance is what makes the master member non-trivial.

    The correct way to allocate C++ objects is:

    Code:
    temp = new node;
    As to overloading assignment / a copy function, you shouldn't need any of those, because there are no members that wouldn't be correctly assigned using the implicit memberwise assignment.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by anon View Post
    Code:
    temp=(node *)malloc(sizeof(node));
    The problem is probably just this. You just can't allocate a non-trivial object with malloc and start using it, because the object (and its members) have never been constructed. I suppose that having all the virtual inheritance is what makes the master member non-trivial.

    The correct way to allocate C++ objects is:

    Code:
    temp = new node;
    As to overloading assignment / a copy function, you shouldn't need any of those, because there are no members that wouldn't be correctly assigned using the implicit memberwise assignment.
    Hi anon,It really worked out.And I was very happy happy with the output and it runs fine now.This was really very important to me and it was really a very great help.Thanks for your very kind help to figure it out.
    Whole day I was trying out various possibilities to correct it out but I din't have any clue about where the mistake was.And I never thought it was due to malloc().
    But why malloc() can't be used for allocating objects and what do you mean by non-trivial here???
    Also one more question,we can't use cin for taking strings with blank spaces,but we can use getline() or scanf() but in linux platform it creates some problem.What are any other option?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rakeshkool27
    But why malloc() can't be used for allocating objects and what do you mean by non-trivial here???
    malloc can be used to allocate space for objects of a POD type. The problem with using it to allocate space for objects of non-POD types is that those objects will not be properly constructed, unless you say, use placement new, but I would rather not get into that. "Non-trivial" probably means "not of POD type".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by laserlight View Post
    malloc can be used to allocate space for objects of a POD type. The problem with using it to allocate space for objects of non-POD types is that those objects will not be properly constructed, unless you say, use placement new, but I would rather not get into that. "Non-trivial" probably means "not of POD type".
    So it seems that it is better not to use malloc() in C++ for classes.Its more safer with new.I will have to study more about "new" since I am new to C++.
    But anyway is there any other option for cin in C++ to handle strings with blank spaces,like we have in C the "%[^\n]s" way of reading strings by scanf containing blank spaces.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rakeshkool27
    So it seems that it is better not to use malloc() in C++ for classes.Its more safer with new.
    Yes, and in general you might as well stick to using new and new[]... or if feasible, just use a container or smart pointer.

    Quote Originally Posted by rakeshkool27
    But anyway is there any other option for cin in C++ to handle strings with blank spaces,like we have in C the "%[^\n]s" way of reading strings by scanf containing blank spaces.
    Use std::string with its version of std::getline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by laserlight View Post
    Use std::string with its version of std::getline.
    Hmmmm...getline() can be used in place of cin.but in case suppose we give an input before getline() and after that press enter,then the getline() fn is skipped.One solution is to use fflush(stdin), but this doesn't works in linux platform although it works in codeblocks or borland.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rakeshkool27
    Hmmmm...getline() can be used in place of cin.but in case suppose we give an input before getline() and after that press enter,then the getline() fn is skipped.One solution is to use fflush(stdin), but this doesn't works in linux platform although it works in codeblocks or borland.
    Use std::cin.ignore, or be consistent in using std::getline and convert the strings to whatever else you need to convert them to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help linked list of objects
    By blaschi in forum C++ Programming
    Replies: 5
    Last Post: 10-16-2010, 01:24 PM
  2. Linked list of objects
    By rocketman50 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2009, 01:54 PM
  3. linked list problems (null values)
    By scwizzo in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2008, 06:04 PM
  4. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  5. Linked List Class with Pointers to Objects and using Polymorphism
    By CaptainMorgan in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 11:41 AM