基類和派生類 c#
Rectangle rect =new Rectangle();
rect.SetWidth(5);
rect.SetHeight(7);
Report.Info("矩形面積:"+rect.getArea());
class shape{
public void SetWidth(int W){
Width=W;
}
public void SetHeight(int H){
Height=H;
}
protected int Width;
protected int Height;
}
class Rectangle:shape{
public int getArea(){
return (Width*Height);
}
}
多繼承 c++
#include <iostream>
using namespace std;
class Shape {
public :
void SetHeight(int H) {
Height = H;
}
void SetWidth(int W) {
Width = W;
}
protected:
int Height;
int Width;
};
class PointCost {
public:
int GetAreaCost(int Area) {
return (Area * 70);
}
};
class Rectangle : public Shape, public PointCost {
public:
int GrtArea() {
return (Height*Width);
}
};
int main(void) {
Rectangle Rect;
Rect.SetHeight(4);
Rect.SetWidth(9);
int Area = Rect.GrtArea();
cout << "輸出面積爲:" << Rect.GrtArea() << endl;
cout << "輸出總費用爲:" << Rect.GetAreaCost(Area) << endl;
system("pause");
return 0;
}