Perform following using virtual functions :
i) Build a master table ii) List a table iii) Insert a new entry
iv) Delete old entry v) Edit an entry vi) Search for a record
Description :
Virtual functions : Its concept is same as a function but it does not really exists but it appears in a program.The functionality of virtual function can be overridden in its derived class.
Syntax: class class_name {
public:
virtual void member_function_main(){ }
};
Code :
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class student
{
protected:
char name[15],sub[15];
int rollno;
};
class derived1 : public virtual student
{
protected:
int sub_code,asses_mark;
};
class derived2 : public virtual student
{
protected:
int uni_marks;
};
class derived3 : public derived1 , derived2
{
public:
derived3();
void accept();
void display();
void update();
int search(int key);
};
derived3 :: derived3()
{
strcpy(name,”aaaaa”);
rollno=-999;
strcpy(sub,”sssss”);
sub_code=-999;
asses_mark=-99;
uni_marks=-99;
}
void derived3 :: accept()
{
cout<<”\n\nEnter ROLL NO : “;
cin>>rollno;
cout<<”\n\nEnter NAME : “;
gets(name);
cout<<”\n\nEnter SUBJECT NAME : “;
gets(sub);
cout<<”\n\nEnter SUBJECT CODE :”;
cin>>sub_code;
cout<<”\n\nEnter INTERNAL ASSESSMENT MARKS :”;
cin>>asses_mark;
cout<<”\n\nEnter UNIVERSITY EXAM MARKS :”;
cin>>uni_marks;
}
void derived3 :: display()
{
cout<<”\n\n\tROLL NO:”<<”\t\t”<<rollno;
cout<<”\n\n\tNAME:”<<”\t\t\t”<<name;
cout<<”\n\n\tSUBJECT:”<<”\t\t”<<sub;
cout<<”\n\n\tSUBJECT CODE:”<<”\t\t”<<sub_code;
cout<<”\n\n\tINTERNAL ASSESS MARKS:”<<” “<<asses_mark;
cout<<”\n\n\tUNIVERSITY EXAM MARKS:”<<”\t”<<uni_marks;
}
void derived3 :: update()
{
int ch;
char c;
do
{
cout<<”\n\t1.ROLL NO”;
cout<<”\n\t2.NAME”;
cout<<”\n\t3.SUBJECT”;
cout<<”\n\t4.SUBJECT CODE”;
cout<<”\n\t5.INTERNAL ASSESSMENT MARKS”;
cout<<”\n\t6.UNIVERSITY EXAM MARKS”;
cout<<”\n\nEnter your choice for modifying the fields : “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\nEnter new ROLL NO:”;
cin>>rollno;
break;
case 2:
cout<<”\nEnter new NAME:”;
cin>>name;
break;
case 3:
cout<<”\nEnter new SUBJECT NAME:”;
cin>>sub;
break;
case 4:
cout<<”\nEnter new SUBJECT CODE:”;
cin>>sub_code;
break;
case 5:
cout<<”\nEnter new INTERNAL ASSESSMENT MARKS:”;
cin>>asses_mark;
break;
case 6:
cout<<”\nEnter UNIVERSITY EXAM MARKS:”;
cin>>uni_marks;
break;
}
cout<<”\n\nDo you want to modify more fields?: “;
cin>>c;
}while(c==’Y’ || c==’y');
}
int derived3 :: search(int key)
{
int i,flag=0;
if(key==rollno)
{
flag=1;
}
return flag;
}
void main()
{
int i,ch,rec_no,key,flag;
derived3 d[20];
clrscr();
do
{
cout<<”\n\n Menu: ”
<<”\n\n\t1.Insert a Record”<<”\n\t2.Display Record”
<<”\n\t3.Delete a Record”<<”\n\t4.Search for a Record”
<<”\n\t5.Update”<<”\n\t6.EXIT”
<<”\n\nEnter your choice: “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].accept();
break;
case 2:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].display();
break;
case 3:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
for(int i=rec_no-1;i<=20;i++)
{
d[i]=d[i+1];
}
break;
case 4:
cout<<”\n\nEnter ROLL NO:”;
cin>>key;
for(i=0;i<20;i++)
{
flag=d[i].search(key);
if(flag==1)
break;
}
if(flag==0)
cout<<”\n\nRecord is not present.”;
else
cout<<”\n\nRecord is present at “<<i+1<<” location.”;
break;
case 5:
cout<<”\n\nEnter Record Number: “;
cin>>rec_no;
d[rec_no-1].update();
break;
case 6: exit(0);
}
} while(ch!=6);
getch();
}
i) Build a master table ii) List a table iii) Insert a new entry
iv) Delete old entry v) Edit an entry vi) Search for a record
Description :
Virtual functions : Its concept is same as a function but it does not really exists but it appears in a program.The functionality of virtual function can be overridden in its derived class.
Syntax: class class_name {
public:
virtual void member_function_main(){ }
};
Code :
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class student
{
protected:
char name[15],sub[15];
int rollno;
};
class derived1 : public virtual student
{
protected:
int sub_code,asses_mark;
};
class derived2 : public virtual student
{
protected:
int uni_marks;
};
class derived3 : public derived1 , derived2
{
public:
derived3();
void accept();
void display();
void update();
int search(int key);
};
derived3 :: derived3()
{
strcpy(name,”aaaaa”);
rollno=-999;
strcpy(sub,”sssss”);
sub_code=-999;
asses_mark=-99;
uni_marks=-99;
}
void derived3 :: accept()
{
cout<<”\n\nEnter ROLL NO : “;
cin>>rollno;
cout<<”\n\nEnter NAME : “;
gets(name);
cout<<”\n\nEnter SUBJECT NAME : “;
gets(sub);
cout<<”\n\nEnter SUBJECT CODE :”;
cin>>sub_code;
cout<<”\n\nEnter INTERNAL ASSESSMENT MARKS :”;
cin>>asses_mark;
cout<<”\n\nEnter UNIVERSITY EXAM MARKS :”;
cin>>uni_marks;
}
void derived3 :: display()
{
cout<<”\n\n\tROLL NO:”<<”\t\t”<<rollno;
cout<<”\n\n\tNAME:”<<”\t\t\t”<<name;
cout<<”\n\n\tSUBJECT:”<<”\t\t”<<sub;
cout<<”\n\n\tSUBJECT CODE:”<<”\t\t”<<sub_code;
cout<<”\n\n\tINTERNAL ASSESS MARKS:”<<” “<<asses_mark;
cout<<”\n\n\tUNIVERSITY EXAM MARKS:”<<”\t”<<uni_marks;
}
void derived3 :: update()
{
int ch;
char c;
do
{
cout<<”\n\t1.ROLL NO”;
cout<<”\n\t2.NAME”;
cout<<”\n\t3.SUBJECT”;
cout<<”\n\t4.SUBJECT CODE”;
cout<<”\n\t5.INTERNAL ASSESSMENT MARKS”;
cout<<”\n\t6.UNIVERSITY EXAM MARKS”;
cout<<”\n\nEnter your choice for modifying the fields : “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\nEnter new ROLL NO:”;
cin>>rollno;
break;
case 2:
cout<<”\nEnter new NAME:”;
cin>>name;
break;
case 3:
cout<<”\nEnter new SUBJECT NAME:”;
cin>>sub;
break;
case 4:
cout<<”\nEnter new SUBJECT CODE:”;
cin>>sub_code;
break;
case 5:
cout<<”\nEnter new INTERNAL ASSESSMENT MARKS:”;
cin>>asses_mark;
break;
case 6:
cout<<”\nEnter UNIVERSITY EXAM MARKS:”;
cin>>uni_marks;
break;
}
cout<<”\n\nDo you want to modify more fields?: “;
cin>>c;
}while(c==’Y’ || c==’y');
}
int derived3 :: search(int key)
{
int i,flag=0;
if(key==rollno)
{
flag=1;
}
return flag;
}
void main()
{
int i,ch,rec_no,key,flag;
derived3 d[20];
clrscr();
do
{
cout<<”\n\n Menu: ”
<<”\n\n\t1.Insert a Record”<<”\n\t2.Display Record”
<<”\n\t3.Delete a Record”<<”\n\t4.Search for a Record”
<<”\n\t5.Update”<<”\n\t6.EXIT”
<<”\n\nEnter your choice: “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].accept();
break;
case 2:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].display();
break;
case 3:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
for(int i=rec_no-1;i<=20;i++)
{
d[i]=d[i+1];
}
break;
case 4:
cout<<”\n\nEnter ROLL NO:”;
cin>>key;
for(i=0;i<20;i++)
{
flag=d[i].search(key);
if(flag==1)
break;
}
if(flag==0)
cout<<”\n\nRecord is not present.”;
else
cout<<”\n\nRecord is present at “<<i+1<<” location.”;
break;
case 5:
cout<<”\n\nEnter Record Number: “;
cin>>rec_no;
d[rec_no-1].update();
break;
case 6: exit(0);
}
} while(ch!=6);
getch();
}