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;
}