CS202: Programming Systems: LAB 4: Inheritance
CS202: Programming Systems: LAB 4: Inheritance
LAB 4: Inheritance
Instructor: Hunh Cng Php Affiliation: IT Faculty, Danang University of Technology Implement the following exercises with C++ and Java 1. Declare the GreatPoint class as a class that extends MyPoint, the given class. You should declare in the GreatPoint class the method distance() that calculates the distance of the point from (0,0) and check it using the given application.
#include <stdio.h> #include <math.h> #include <conio.h> class MyPoint { private: double x; double y; public: MyPoint() { } MyPoint(double a, double b) { x=a; y=b; } double getX() { return x; } double getY() { return y; } void setX(double xVal) { x = xVal; } void setY(double yVal) { y = yVal; } }; class GreatPoint: public MyPoint { public: GreatPoint(double xVal, double yVal):MyPoint(xVal,yVal)
{ } double distance() { return sqrt(getX()*getX() + getY()*getY()); } }; main() { GreatPoint gp(12,24); printf("The distance of gp from (0,0) is %f", gp.distance()); getch(); }
2. Declare the following classes: The Shape class (abstract), that contains two abstract methods: area and perimeter The Rectangle class, that extends Shape, describes a simple rectangle. The Circle class, that extends Shape, describes a simple circle. 3. Create a Employee class including following members Data members char *name int age float hourRate Employee name Employee age Rate per hour Functions Employee(char *nameVal, int age, float hourRateVal) float salary(float hours); void display() Constructor abstract method, caculating salary for a employee display detailed employee information Description Description
Then, create the following classes: The Manager class, that extends Employee, describes a manager. The Clerk class, that extends Employee, describes a clerk. Define in each one of these classes all the needed methods and constructors and check them using the given application. Notice: The formulas calculating: salary for a Manager = hours * hourRate * 2.0 salary for a Clerk = hours * hourRate * 1.2