C++ programming analogy question: I am looking for another real life example (in
ID: 3583730 • Letter: C
Question
C++ programming analogy question:
I am looking for another real life example (in addition to employee one below) to relate object-oriented programming (i.e. C++) to something more concrete and easier to understand. One may set a plan for each object with certain specifications or limits in regards to what the object is and what it should do. Then discuss how plans can be used in defining objects with an example.
(One example is: if an employee is an object, there are many variations of an employee. For example, an employee can be hourly paid, salaried, with a commission, with bonus, etc. This can be confusing for those new to programming )
Explanation / Answer
Object is instance of a class. It represents as reference to class so that we can call or execute
methods in that class using this object.
The methods in class are written according to variables declared in class. Methods written inside
class can be accessed through instance object only. This is merely called aa encapsulation property.
Here is the example of class Employee..
#include <iostream>
using namespace std;
class Employee{
public:
// variables declared..
double hourlyRate;
double commission;
double bonus;
// constructor
Employee(double hourlyRate, double commission, double bonus);
// methods
double getSalary(void);
double getHourlyRate(void);
double getBonus(void);
};
// constructor
Employee::Employee(double hourlyRate, double commission, double bonus){
this.hourlyRate = hourlyRate;
this.commission = commission;
this.bonus = bonus;
}
double Employee::getSalary( void ){ // get salary method
return (8*hourlyRate)*31 + commission + bonus;
}
double Employee::getHourlyRate( void ){
return hourlyRate;
}
double Employee::getBonus (void){
return bonus;
}
//main
int main() {
// creating employee instance
Employee emp(60,1200,500);
// calling method using instance object
cout<<"Employee monhly salary: "<<emp.getSalary();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.