Problem 1 Inheritance OOP
Problem:
You have to make three classes:
- Person class
- Student class
- Employee class
Criteria:
Person class is the parent class or base class. Student class is derived or child of Person class and Employee class is also derived or child of Person class.
Member variables and methods/functions:
Person class:
Person class must have 3 private member variables
- Name
- CNIC
- Address
And two public member functions:
- get
- set
To get and set values of member variables.
Student class:
Student class must have 4 private member variables:
- RollNo
- Sub1
- Sub2
- Sub3
Three public member functions/methods:
- get1
- set1
- calcGPA ( To calculate GPS of student )
For GPA simply find average if average is greater than 80 GPA is 4 else 3 no need to do anything else.
Employee class:
Make two private member variables:
- EmpId
- EmpSalary
And a member function/method:
- CalIncre
CallIncre function must add 20% bonus to the salary of the Employee. Calculate 20% bonus and then add it to the salary of Employee to have total salary after bonus.
Main method:
Create object of the Employee class and set all the attributes of Employee class and then calculate increment in Employee salary and print the resulted salary.
Similarly create object of Student class and calculate GPA. To implement inheritance use Person class functions for name of student and employee.
Similar Example:
#include <iostream>
using namespace std;
class A{
private:
int a;
string b;
public:
void setData(int a1,string b1){
a=a1;
b=b1;
}
void getData(){
cout<<a<<endl<<b<<endl;
}
};
class B:public A{
private:
string name;
int sub1,sub2,sub3; public: void set(string Name,int s1,int s2,int s3){ name=Name; sub1=s1; sub2=s2; sub3=s3; } void get(){ cout<<name<<endl<<sub1<<endl<<sub2<<endl<<sub3<<endl; } void calGpa(){ float avg; int sum; sum=sub1+sub2+sub3; avg=sum/3;
cout<<avg<<endl;
if(avg>80){
cout<<"gpa is 4"<<endl;
}
else{
cout<<"gpa is 3"<<endl;
}
}
};
class C:public A{
private:
int id,salary;
public:
void setEmp(int I,int S){
id=I;
salary=S;
}
void getEmp(){
cout<<id<<endl<<salary<<endl;
}
void incS(){
salary=salary+((salary*20)/100);
cout<<"incremented salary is:"<<salary<<endl;
}
};
int main() {
C obj;
obj.setEmp(1342,40000);
obj.incS();
return 0;
}
Output:
incremented salary is:48000
- For multiple and multilevel inheritance:
Best explain 👍
ReplyDeleteExcellent👍
ReplyDelete