Beruflich Dokumente
Kultur Dokumente
#include<iostream>
using namespace std;
class person
{
string n;
int c;
public:
void pdata()
{
cout<<"Enter name : ";
cin>>n;
cout<<"Enter code : ";
cin>>c;
}
void pdisplay()
{
cout<<"\nName : "<<n;
}
void pcode()
{
cout<<"\nCode : "<<c;
}
};
class account: virtual public person
{
double p;
public:
void adata()
{
cout<<"Enter pay : ";
cin>>p;
}
void adisplay()
{
cout<<"\nPay : "<<p;
}
};
class admin: virtual public person
{
int e;
public:
void addata()
{
cout<<"Enter experience : ";
cin>>e;
}
void addisplay()
{
cout<<"\nExperience : "<<e;
}
};
class master: public account, public admin
{
public:
void getdata()
{
pdata();
addata();
adata();
}
void display()
{
pdisplay();
pcode();
addisplay();
adisplay();
}
};
int main()
{
master m;
int a,t=1;
do
{
cout<<"\nEnter 1 for create";
cout<<"\nEnter 2 for update";
cout<<"\nEnter 3 for display";
cin>>a;
if(a==1)
{
if(t==1)
{
m.getdata();
t++;
}
else
{
cout<<"\Cannot Create!";
}
}
if(a==2)
{
if(t==1)
{
cout<<"\nCreate object first!";
}
else
{
m.getdata();
t++;
}
}
if(a==3)
{
if(t==1)
{
cout<<"\nCreate object first!";
}
else
{
m.display();
t++;
}
}
}
while(a>=1 && a<=3);
return 0;
}
Q2:Explain the advantage of virtual base class with the help of suitable
example.
SOLUTION:
The virtual base class is used when a derived class has multiple copies of the base
class.
Example:
#include<iostream>
using namespace std;
class student
{
int rollno;
public:
void getnumber()
{
cout << "Enter Roll No:";
cin>>rollno;
}
void putnumber() {
cout << "\n\n\tRoll No:" << rollno << "\n";
}
};
class test : virtual public student
{
public:
int mst1, mst2;
void getmarks()
{
cout << "Enter Marks\n";
cout << "Mst1:";
cin>>mst1;
cout << "Mst2:";
cin>>mst2;
}
void putmarks()
{
cout << "\tMarks Obtained\n";
cout << "\n\tMst1:" << mst1;
cout << "\n\tMst2:" << mst2;
}
};
class sports : public virtual student {
public:
int score;
void getscore()
{
cout << "Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout << "\n\tSports Score is:" << score;
}
};
class result : public test, public sports
{
int total;
public:
void display()
{
total = mst1 + mst2 + score;
putnumber();
putmarks();
putscore();
cout << "\n\tTotal Score:" << total;
}
};
int main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0;
}
Q3: Create a class Employee with a name and salary. Create a class Manager
inherit from Employee. Add an instance variable, named department, of type
string. Supply a method to toString that prints the manager’s name,
department and salary. Make a class Executive inherits from Manager. Supply
a method to String that prints the string “Executive” followed by the
information stored in the Manager Superclass object. Supply a test program
that tests these classes and methods.
SOLUTION:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class employee
{
protected:
char name[30];
int sal;
};
class manager: public employee
{
protected:
char dep[50];
public:
void input();
void disp();
};
void manager::input()
{
cout<<"Enter name: ";
gets(name);
cout<<"Enter department: ";
gets(dep);
cout<<"Enter salary: ";
cin>>sal;
}
void manager::disp()
{
cout<<"\nManager's name: "<<name;
cout<<"\nSalary: "<<sal;
cout<<"\nDepartment: "<<dep<<endl;
}
class executive:public manager
{
public:
void disp(manager);
};
void executive::disp(manager m)
{
cout<<"\n\nExecutive";
m.disp();
}
int main()
{
manager m1;
m1.input();
executive e;
m1.disp();
e.disp(m1);
getch();
return 0;
}
Q4: Consider the following class hierarchy. Create a base class employee
(empcode emp name). Derive the class manager (designation clubdues),
scientist (department name ,publication) and laborer from employee class.
Write C++ menu driven program
a) To accept the detail of ‘n’ employee.
b) To display the information.
c) To display all the scientist from “chemical department”.
SOLUTION: -
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class employee
{
int ecode;
char name[20];
public:
void get()
{
cout<<"\nEnter EmpCode & Name: \n";
cin>>ecode>>name;
}
void put()
{
cout<<"\n"<<ecode<<"\t\t";
cout<<name<<"\t";
}
};
class manager:virtual public employee
{
char desig[20];
int clubd;
public:
void get_m()
{
cout<<"\nEnter designation & Clubdub:\n ";
cin>>desig>>clubd;
}
void put_m()
{
cout<<desig<<"\t";
cout<<"\t"<<clubd<<"\t";
}
};
class scientist:virtual public employee
{
public:
char dname[20];
char publ[20];
public:
void get_s()
{
cout<<"\nEnter Department name & publication name : \n";
cin>>dname>>publ;
}
void put_s()
{
cout<<"\t"<<dname;
cout<<"\t\t"<<publ;
}
};
#include<iostream>
#include<string>
using namespace std;
int main()
{
float num1, num2, ans;
char Operator;
cout<<"\n Perform Arithmetic Operations on Two Numbers";
try
{
cout<<"\n Enter First Number:";
cin>>num1;
if(num1==0)
throw 0;
cout<<"\n Enter Operator:";
cin>>Operator;
if(Operator != '+' && Operator != '-' &&
Operator != '*' && Operator != '/')
throw Operator;
cout<<"\n Enter Second Number:";
cin>>num2;
switch(Operator)
{
case '+':
ans = num1 + num2;
break;
case '-':
ans = num1 - num2;
break;
case '*':
ans = num1 * num2;
break;
case '/':
ans = num1 / num2;
break;
}
if(num2 == 0)
throw 0;
cout<<"\n Answer : "<<num1<<" "<<Operator<<" "<<num2<<" = "<<ans;
}
catch(const char c)
{
cout<<"\n Exception Caught \n Bad Operator : "<<c<<" is not a Valid Operator";
}
catch(const int n)
{
cout<<"\n Error : Bad Operation";
}
return 0;
}