Sei sulla pagina 1di 8

DANANG UNIVERSITY OF TECHNOLOGY INFORMATION TECHNOLOGY FACULTY

Object: Object Oriented Programming LAB 4 : Inheritance Lecturer: Hunh Cng Php Class: 11TCLC Student: L Quang Chnh

Exercises 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(); }

Test results

Exercises 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.
#include<iostream> #include<conio.h> using namespace std; class Shape { public: virtual float area(){} virtual float perimeter(){} }; class Rectangle : public Shape { private: float height,width; public: Rectangle(float x=0,float y=0) { height=x; width=y; } float area() { return height*width; } float perimeter() { return 2*(height+width); } void display() { cout<<"Rectangle:\n"; cout<<"area="<<area()<<"\n"; cout<<"perimeter="<<perimeter()<<"\n"; } }; class Circle : public Shape { private: float r; public: Circle(float x=0) {

r=x; } float area() { return 3.14*r*r; } float perimeter() { return 3.14*2*r; } void display() { cout<<"Circle:\n"; cout<<"area="<<area()<<"\n"; cout<<"perimeter="<<perimeter()<<"\n"; } }; main() { Rectangle R(4,8); R.display(); cout<<"----------------------\n"; Circle C(10); C.display(); }

Test results

Exercises 3:

Create a Employee class including following members

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
#include<iostream> #include<conio.h> #include<stdio.h> using namespace std; class Employee { protected: char *name; int age; float hourRate; public: Employee(char *nameVal,int age,float hourRateVal) { this->name=nameVal; this->age=age; this->hourRate=hourRateVal; } virtual float salary(float hours) { return hourRate*hours; } void display() { cout<<"\nName : "<<name; cout<<"\nAge : "<<age; cout<<"\nHour Rate: "<<hourRate; cout<<"\nSalary : "<<salary(5); } }; class Manager : public Employee

{ public: Manager(char *nameVal,int age,float hourRateVal):Employee(nameVal,age,hourRateVal) { } float salary(float hours) { return hours*hourRate*2; } void display() { cout<<"\nPosition : Manager"; Employee::display(); } }; class Clerk : public Employee { public: Clerk(char *nameVal,int age,float hourRateVal):Employee(nameVal,age,hourRateVal) { } float salary(float hours) { return hours*hourRate*1.2; } void display() { cout<<"\nPosition : Clerk"; Employee::display(); } }; main() { Manager A("Mr.Carter",19,21); Clerk B("Phil John",18,42); A.display(); cout<<"\n-----------------------------------"; B.display(); }

Test results