Você está na página 1de 11

# CS202: Programming Systems LAB 4: Inheritance

Exercice 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. a.code: #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; } };

## 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(); } b.results:

Exercise 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. a.code: #include<iostream> #include<conio.h> using namespace std; class shape { public: virtual float area()=0; virtual float perimeter()=0;

}; class rectangle : public shape { private: float x,y; public: rectangle(); rectangle(float a,float b); virtual float area(); virtual float perimeter(); void display();

}; rectangle::rectangle() { x=0; y=0; } rectangle::rectangle(float a,float b) { x=a; y=b; } float rectangle::area() { return(x*y);

} float rectangle::perimeter() { return(2*(x+y)); } void rectangle::display() { cout<<" area recrangle = "<<area(); cout<<"\n petimeter recrangle = "<<perimeter();

} class circle { private: float r; public: circle(); circle(float x); virtual float area(); virtual float perimeter(); void display(); }; circle::circle() { r=0; } circle::circle(float x) { r=x; } float circle::area() { return(3.1415*r*r); } float circle::perimeter()

{ return(2*3.1415*r); } void circle::display() { cout<<"\n area circle = "<<area(); cout<<"\n perimeter cirle = "<<perimeter();

## } main() { rectangle A(3,4); A.display(); circle B(4); B.display(); getch(); } b.results:

Exercise 3: Create a Employee class including following members. a.code: #include <iostream> #include <conio.h> using namespace std;

class Employee { protected: char* name; int age; float hourRate; public: Employee(char*nameVal, int ageVal, float hourRateVal); virtual float salary (float hours){} void display (); }; Employee::Employee(char*nameVal, int ageVal, float hourRateVal) { name = nameVal; age = ageVal; hourRate = hourRateVal; } void Employee::display () { cout << "\n " << name <<":" << endl;

cout << "\n\t Age: " << age << endl; cout << "\n\t Hour rate: " <<hourRate <<endl; } class Manager: public Employee { public:

Manager(char*nameVal, int ageVal, float hourRateVal):Employee(nameVal, ageVal,hourRateVal) {} virtual float salary(float hours) { <<endl; } }; cout << "\n\t Tilte:Manager \n"; cout <<"\n\t Salary: "<< hours * hourRate * 2.0

## class Clerk: public Employee { public:

Clerk (char*nameVal, int ageVal, float hourRateVal):Employee(nameVal, ageVal,hourRateVal) {} virtual float salary(float hours) { <<endl; } }; cout << "\n\t Tilte:Clerk \n"; cout <<"\n\t Salary: "<< hours * hourRate * 1.2

int main() { Manager A("Thuan", 45, 5); Clerk B("Hoa", 30, 3); Employee* ptr; ptr = &A; ptr -> display(); ptr -> salary(7); ptr = &B; ptr -> display(); ptr -> salary(8); getch(); } b.resulfs: