类:
class Triangle {
protected:
static int nou;
double length=0;
double width=0;
public:
Triangle(double l, double w) :length(l), width(w) {
cout << "Tri con..." << endl;
nou++;
}
~Triangle() { cout << "Tri des..." << endl; }
void setle(double le) { length = le; }
void setw(double wi) { width = wi; }
double getl() { return length; }
double getw() { return width; }
double getA() { return length * width; }
double getc() { return 0; }
void display() {
cout << length << " " << width << endl;
}
};
class Cube :public Triangle {
private:
double high=0;
public:
~Cube() { cout << "Cube des..." << endl; };
Cube(double l, double w, double h) :Triangle(l, w), high(h) {
cout << "Cube con..." << endl;
nou++;
};
double getA() { return length * high * 2 + length * width * 2 + high * width * 2; }
double getc() { return Triangle::getA()*high; }
void seth(double h) { high = h; }
double geth() { return high; }
void display() {
cout << length << " " << width << " " << high << " " << endl;
cout << getA() << " " << getc() << endl << nou << endl;
}
static int getn() { return nou; }
};
int Triangle::nou = 0;
主函数部分:
Cube c(3, 4, 5);
c.display();
c.seth(2);
c.setw(2);
c.display();
}
亮点自己找吧!